CONSTRAINTS
CONSTRAINTS
SQL> create table A_CUSTOMERS (ID number(2) constraint nn not null, name varchar(10), marks number(3));
CREATE TABLE A_CUSTOMERS( ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL,
ADDRESS CHAR (25) , SALARY DECIMAL (18, 2));
CHECK
The following SQL creates a CHECK constraint on the "Age" column when the "Persons" table is created.
The CHECK constraint ensures that the age of a person must be 18, or older: This is used to insert the values based on
specified condition. We can add this constraint in all three levels.
Ex: COLUMN LEVEL
CREATE TABLE Persons (ID int NOT NULL,LastName varchar(255) NOT NULL,FirstName varchar(255),Age int,CHECK (Age>=180));
TABLE LEVEL
SQL> create table student (no number(2) , name varchar(10), marks number(3), check (marks > 300));
SQL> create table student (no number(2) , name varchar(10), marks number(3), constraint ch check(marks > 300));
ALTER LEVEL
ALTER TABLE Persons ADD CHECK (Age>=180);
ALTER TABLE Persons add constraint ch check (Age>=180);
UNIQUE
This is used to avoid duplicates but it allows nulls. We can add this constraint in all three levels.
COLUMN LEVEL
SQL> create table student (no number (2) unique, name varchar(10), marks number(3));
SQL> create table student (no number (2) constraint un unique, name varchar(10), marks number(3));
TABLE LEVEL
SQL> create table student (no number (2) , name varchar(10), marks number(3), unique(no));
SQL> create table student (no number (2) , name varchar(10), marks number(3), constraint un unique(no));
ALTER LEVEL
PRIMARY KEY
This is used to avoid duplicates and nulls. This will work as combination of unique and not null. Primary key always attached
to the parent table. We can add this constraint in all three levels.
create table Table_2 (Id int not null primary key, Name varchar(50), Dept varchar (50),
Mob int , Address varchar (50))
insert into Table_2 (id , name,dept,mob,address) values (1 , 'Balu', 'RD' , 1892626341,
181)
COLUMN LEVEL
SQL> create table student (no number (2) primary key, name varchar(10), marks number(3));
SQL> create table student (no number (2) constraint pk primary key, name varchar(10), marks number(3));
TABLE LEVEL
SQL> create table student (no number (2) , name varchar(10), marks number(3), primary key(no));
SQL> create table student (no number (2) , name varchar(10), marks number(3), constraint pk primary key(no));
ALTER LEVEL
FOREIGN KEY
This is used to reference the parent table primary key column which allows duplicates.
Foreign key always attached to the child table.
We can add this constraint in table and alter levels only.
TABLE LEVEL
SQL> create table emp(empno number(2), ename varchar(10), deptno number(2), primary key(empno), foreign key(deptno)
references dept(deptno));
SQL> create table emp(empno number(2), ename varchar(10), deptno number(2), constraint pk primary key(empno),
ALTER LEVEL
SQL> alter table emp add constraint fk foreign key(deptno) references dept(deptno);
Once the primary key and foreign key relationship has been created then you can not remove any parent record if the
dependent Childs exist.
USING ON DELTE CASCADE
By using this clause you can remove the parent record even it Childs exists. Because whenever you remove parent record
oracle automatically removes all its dependent records from child table, if this clause is present while creating foreign key
constraint.
TABLE LEVEL
SQL> create table emp(empno number(2), ename varchar(10), deptno number(2), primary key(empno), foreign key(deptno)
ALTER LEVEL
SQL> alter table emp add foreign key(deptno) references dept(deptno) on delete cascade;
SQL> alter table emp add constraint fk foreign key(deptno) references dept(deptno) on delete cascade;
COMPOSITE KEYS
A composite key can be defined on a combination of columns. We can define composite keys on entity integrity and
referential integrity constraints. Composite key can be defined in table and alter levels only.
UNIQUE (TABLE LEVEL)
SQL> create table student(no number(2) , name varchar(10), marks number(3), unique(no,name));
SQL> create table student(no number(2) , name varchar(10), marks number(3), constraint un unique(no,name));
UNIQUE (ALTER LEVEL)
SQL> create table emp(empno number(2), ename varchar(10), deptno number(2), dname varchar(10), primary key(empno),
SQL> alter table emp add constraint fk foreign key(deptno,dname) references dept(deptno,dname);
DEFERRABLE CONSTRAINTS
Each constraint has two additional attributes to support deferred checking of constraints.
Deferred initially immediate
Deferred initially deferred
Deferred initially immediate checks for constraint violation at the time of insert.
Deferred initially deferred checks for constraint violation at the time of commit.
Ex:
SQL> create table student(no number(2), name varchar(10), marks number(3), constraint un unique(no) deferred initially
immediate);
SQL> create table student(no number(2), name varchar(10), marks number(3), constraint un unique(no) deferred initially
deferred);
SQL> alter table student add constraint un unique(no) deferrable initially deferred;
This will enable all the constraints violations at the time of inserting.
SQL> set constraints all deferred;
This will enable all the constraints violations at the time of commit.
ENABLE
This will enable the constraint. Before enable, the constraint will check the existing data.
Ex: alter table student enable constraint un;
DISABLE
This will enforce the constraint rather than enable for future inserts or updates.
This will not check for existing data while enforcing data.
Ex: alter table student enforce constraint un;
DROP