1. Understand basic concepts of SQL*Plus overview and Oracle
views
Managing constraints
1. What Does "Creating Constraints" Mean?
Creating constraints means defining rules at the time of table creation or later using SQL
commands. These rules restrict the kind of data that can be inserted into the table to
protect the quality of data.
Common Types of Constraints:
2. Adding Constraints:
A. At Table Creation:
CREATE TABLE employees (emp_id INT PRIMARY KEY, name VARCHAR(50) NOT NULL, salary
INT CHECK (salary > 0), dept_id INT, FOREIGN KEY (dept_id) REFERENCES
departments(dept_id));
B. After Table Creation (Using ALTER):
ALTER TABLE employees ADD CONSTRAINT fk_dept FOREIGN KEY (dept_id) REFERENCES
departments(dept_id);
2. Understand basic concepts of SQL*Plus overview and Oracle
views
Managing constraints
2. Dropping Constraints:
You can remove a constraint using its name:
ALTER TABLE employees DROP CONSTRAINT fk_dept;
Note: In MySQL, you may need to use DROP FOREIGN KEY instead of DROP CONSTRAINT.
3. Enabling/Disabling Constraints:
These options are mostly available in Oracle and some other RDBMS:
Disable a constraint:
ALTER TABLE employees DISABLE CONSTRAINT fk_dept;
Enable it again:
ALTER TABLE employees ENABLE CONSTRAINT fk_dept;
4. Renaming Constraints:
Some systems allow renaming constraints:
ALTER TABLE employees RENAME CONSTRAINT old_name TO new_name;
3. Understand basic concepts of SQL*Plus overview and Oracle
views
Managing constraints
Deferring constraint check
In Database Management Systems (DBMS), particularly in relational databases like Oracle, deferring constraint
checks refers to postponing the enforcement of certain constraints (like foreign key constraints) until the end of
a transaction, rather than checking them immediately after each DML (Data Manipulation Language) operation
(INSERT, UPDATE, DELETE).
Key Concepts:
1. Constraints:
Constraints are rules enforced on data in a table. Common types include:
• PRIMARY KEY
• FOREIGN KEY
• UNIQUE
• NOT NULL
• CHECK
By default, most constraints are checked immediately after a DML statement.
2. Immediate vs Deferred Constraints:
• Immediate Constraint: Checked right after the DML operation.
• Deferred Constraint: Checked only at the end of the transaction, during COMMIT.
4. Understand basic concepts of SQL*Plus overview and Oracle
views
Managing constraints
Deferring constraint check
How to Defer Constraints (Oracle Example):
1. Declare a Constraint as Deferrable
CREATE TABLE employee (emp_id NUMBER PRIMARY KEY, mgr_id NUMBER, CONSTRAINT fk_mgr
FOREIGN KEY (mgr_id) REFERENCES employee(emp_id) DEFERRABLE INITIALLY IMMEDIATE);
2. Defer it at Runtime
SET CONSTRAINTS fk_mgr DEFERRED;
3. Defer All Constraints
SET CONSTRAINTS ALL DEFERRED;
4. Transaction-Level Defer
BEGIN;
SET CONSTRAINTS ALL DEFERRED;
--- DML operations here
COMMIT;
5. Understand basic concepts of SQL*Plus overview and Oracle
views
Managing views
Managing views in a DBMS (Database Management System) is an essential part of database administration,
as views help provide security, simplicity, and abstraction over complex queries and data.
What is a View?
A view is a virtual table based on the result of a SELECT query. It does not store data itself but displays data
from one or more base tables.
CREATE VIEW view_name ASSELECT column1, column2 FROM table_name WHERE condition;
Managing Views as a DBA
1. Creating Views
CREATE VIEW emp_view AS SELECT emp_id, emp_name, salary FROM employee WHERE dept = 'Sales';
2. Viewing Existing Views
SELECT view_name FROM user_views;
3. Modifying a View
You can’t use ALTER VIEW to change the SELECT logic. Instead:
CREATE OR REPLACE VIEW emp_view AS SELECT emp_id, emp_name FROM employee WHERE dept = 'HR';
4. Dropping a View
DROP VIEW emp_view;
6. Understand basic concepts of SQL*Plus overview and Oracle
views
Managing views
5. Updating a View
You can’t directly update a view using ALTER VIEW in most DBMS when you want to change
the query logic. Instead, you replace the existing view using:
1. CREATE OR REPLACE VIEW
This is the standard way to update a view’s definition.
Syntax
CREATE OR REPLACE VIEW view_name AS SELECT columns FROM table_name WHERE
condition;
If the view exists, it will be replaced. If the view does not exist, it will be created.
Example
--- Original view
CREATE VIEW emp_view ASSELECT emp_id, emp_name FROM employee;
-- Updated view (adds salary column)
CREATE OR REPLACE VIEW emp_view AS SELECT emp_id, emp_name, salary FROM employee;
7. Understand basic concepts of SQL*Plus overview and Oracle
views
Managing views
6. Inserting into a View
Inserting into a view means using the INSERT statement on a view instead of directly on a base table.
This is only possible if the view is updatable.
Syntax
INSERT INTO view_name (column1, column2, ...)
VALUES (value1, value2, ...);
Example
Base Table
CREATE TABLE employee (emp_id INT PRIMARY KEY, emp_name VARCHAR(50), salary NUMBER, dept
VARCHAR(30));
View
CREATE VIEW emp_view AS SELECT emp_id, emp_name, salary FROM employee;
Insert Through View
INSERT INTO emp_view (emp_id, emp_name, salary)VALUES (105, 'Sara', 5000);
This will insert into the employee table via the view.