Week 3 Constraints Lab
Week 3 Constraints Lab
Week 3 Constraints Lab
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.
Syntax:
CREATE TABLE tablename
(Columnname1 DATATYPE CONSTRAINT <constraintname1> PRIMARY KEY,
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;
Note: The NOT NULL constraint can only be applied at column level.
CONSTRAINTS
Constraints are categorized as follows.
Not null
Check
Unique
Primary key
Foreign key
1. NOT NULL
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
SQL> create table student(no number(2) , name varchar(10), marks number(3) check (marks >
300));
TABLE LEVEL
SQL> create table student(no number(2) , name varchar(10), marks number(3), check (marks >
300));
ALTER LEVEL
3. UNIQUE
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
This is used to avoid duplicates and nulls. This will work as combination of unique and
not null.
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
FOREIGN KEY
This is used to reference the parent table primary key column which allows
duplicates.
Ex:
TABLE LEVEL
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 exists.
COMPOSITE KEYS
Ex:
primary key(no,name));
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));
dept(deptno,dname);