AIM: To implement Data Constraints
THEORY:
Constraints are the business Rules which are enforced on the data being stored in a table are
called Constraints.
Types of Data Constraints
1. I/O Constraint
This type of constraint determines the speed at which data can be inserted or extracted
from an Oracle table.
I/O Constraints is divided into two different types
The Primary Key Constraint
The Foreign Key Constraint
2. Business rule Constraint
This type of constraint is applied to data prior the data being inserted into table columns.
Oracle allows programmer to define constraints at
Column level
Table level
The PRIMARY KEY defined at column level
Syntax:
CREATE TABLE tablename
(Columnname1 DATATYPE CONSTRAINT <constraintname1> PRIMARY KEY,
Columnname2 DATATYPE,
columnname3 DATATYPE,.....);
The PRIMARY KEY defined at table level
Syntax:
CREATE TABLE tablename
(Columnname1 DATATYPE,
columnname2 DATATYPE,
columnname3 DATATYPE,
PRIMARY KEY (columnname1, columnname2));
The FOREIGN KEY defined at column level
Syntax:
CREATE TABLE tablename
(Columnname1 DATATYPE REFERENCES tablename[(columnname)] [ON
DELETECASCADE],
columnname2 DATATYPE ,
columnname3 DATATYPE ,.....);
NOTE:
1. The table in which FOREIGN KEY is defined is called FOREIGN TABLE or DETAIL
TABLE.
2. The table in which PRIMARY KEY is defined and referenced by FOREIGN KEY is
called PRIMARY TABLE or MASTER TABLE.
3. ON DELETE CASCADE is set then DELETE operation in master table will trigger the
DELETE operation for corresponding records in the detail table.
The FOREIGN KEY defined at table level
Syntax:
CREATE TABLE tablename
(Columnname1 DATATYPE,
columnname2 DATATYPE,
columnname3 DATATYPE,
PRIMARY KEY (columnname1, columnname2),
FOREIGN KEY (columnname2) REFERENCES tablename2;
A CONSTRAINT can be given User Defined Name, the syntax is:
CONSTRAINT < constraint name><constraint definition>
The CHECK Constraint defined at column level
CREATE TABLE tablename
(Columnname1 DATATYPE CHECK (logical expression),
columnname2 DATATYPE,
columnname3 DATATYPE,...);
The CHECK Constraint defined at table level
CREATE TABLE tablename
(Columnname1 DATATYPE,
columnname2 DATATYPE,
columnname3 DATATYPE,
CHECK (logical expression1),
CHECK (logical expression2));
The UNIQUE Constraint defined at the column level
CREATE TABLE tablename
(Columnname1 DATATYPE UNIQUE,
columnname2 DATATYPE UNIQUE,
columnname3 DATATYPE ...);
The UNIQUE Constraint defined at the the table levelh az
CREATE TABLE tablename
(Columnname1 DATATYPE,
columnname2 DATATYPE,
columnname3 DATATYPE,
UNIQUE(columnname1));
NOT NULL constraint defined at column level :
CREATE TABLE tablename
(Columnname1 DATATYPE NOT NULL,
columnname2 DATATYPE NOT NULL,
columnname3 DATATYPE,...);
Note: The NOT NULL constraint can only be applied at column level.
CONSTRAINTS
Constraints are categorized as follows.
Domain integrity constraints
Not null
Check
Entity integrity constraints
Unique
Primary key
Referential integrity constraints
Foreign key
Constraints are always attached to a column not a table.
We can add constraints in three ways.
Column level -- along with the column definition
Table level -- after the table definition
Alter level -- using alter command
1. NOT NULL
This is used to avoid null values.
We can add this constraint in column level only.
Ex: SQL> create table student(no number(2) not null, name varchar(10), marks number(3));
SQL> create table student(no number(2) constraint nn not null, name varchar(10),
marks number(3));
2. CHECK
This is used to insert the values based on specified condition.
We can add this constraint in all three levels.
Ex: COLUMN 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));
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
SQL> alter table student add check(marks>300);
SQL> alter table student add constraint ch check(marks>300);
3. UNIQUE
This is used to avoid duplicates but it allow 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
SQL> alter table student add unique(no);
SQL> alter table student add constraint un unique(no);
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.
Ex:
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
SQL> alter table student add primary key(no);
SQL> alter table student add constraint pk primary key(no);
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.
Ex:
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),
constraint fk foreign key(deptno) references dept(deptno));
ALTER LEVEL
SQL> alter table emp add foreign key(deptno) references dept(deptno);
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 exists.
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.
Ex:
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> alter table student add unique(no,name);
SQL> alter table student add constraint un unique(no,name);
PRIMARY KEY (TABLE LEVEL)
SQL> create table student(no number(2) , name varchar(10), marks number(3),
primary key(no,name));
SQL> create table student(no number(2) , name varchar(10), marks number(3),
constraint pk primary key(no,name));
PRIMARY KEY (ALTER LEVEL)
SQL> alter table student add primary key(no,anme);
SQL> alter table student add constraint pk primary key(no,name);
FOREIGN KEY (TABLE LEVEL)
SQL> create table emp(empno number(2), ename varchar(10), deptno number(2), dname
varchar(10), primary key(empno), foreign key(deptno,dname) references dept(deptno,dname));
SQL> create table emp(empno number(2), ename varchar(10), deptno number(2), dname
varchar(10), constraint pk primary key(empno), constraint fk foreign key(deptno,dname) references
dept(deptno,dname));
FOREIGN KEY (ALTER LEVEL)
SQL> alter table emp add foreign key(deptno,dname) references dept(deptno,dname);
SQL> alter table emp add constraint fk foreign key(deptno,dname) references
dept(deptno,dname);