You are on page 1of 17

MIS 403 – Database Management Systems -II

Dr Mohamed A Sheriff

Pearson Education © 2009


MIS403 – Lecture 9

Declarative Constraints

2
Declarative Constraints

Definition:
A mechanism provided in SQL to:

 maintain the consistency & integrity of a database


 enforce certain business rules in database
applications

3
Declarative Constraints
 Five types of declarative constraints
 PRIMARY KEY
 NOT NULL
 UNIQUE
 CHECK
 FOREIGN KEY

 Defined on a column within a database table

4
The PRIMARY KEY constraint
 Used to maintain the entity integrity rule
– Column value must be unique in the table
– Column value cannot be NULL

 Example
Create table STUDENT (
SID Number (5) constraint PK_STD Primary Key,
SNAME Varchar2 (30)
DNAME Varchar2 (30)
SLEVEL Number (1));
 A column constraint
5
The PRIMARY KEY constraint

 Table Constraint
Create table STUDENT (
SID Number (5),
SNAME Varchar2 (30),
DNAME Varchar2 (30),
SLEVEL Number (1),
Constraint PK_STD Primary Key (SID));

If Primary key is composite then list all key attributes e.g. if


SID, Code is primary key for Student Record table

Constraint PK_STD Primary Key (SID, Code) 6


The NOT NULL Constraint
 Imposed on columns that MUST have a value
according to the business rules
Example

Create table STUDENT (


SID Number (5),
SNAME Varchar2 (30),
DNAME Varchar2 (30)
Constraint NN_STD_DNAME NOT NULL,
SLEVEL Number (1) NOT NULL,
Constraint PK_STD Primary Key (SID));
7
Note: it is optional to name a NOT NULL constraint
The UNIQUE Constraint

 Almost same as the Primary Key constraints


 But UNIQUE allows NULL values

Example
Create table STUDENT (
SID Number (5),
SNAME Varchar2 (30),
DNAME Varchar2 (30) NOT NULL,
SLEVEL Number (1) NOT NULL,
SEMAIL Varchar2 (40) Constraint STD_SEMAIL UNIQUE,
Constraint PK_STD Primary Key (SID)); 8
The Check Constraint
 Defines a list of values allowed in a column
Example
Create table STUDENT (
SID Number (5),
SNAME Varchar2 (30),
DNAME Varchar2 (30) NOT NULL,
SLEVEL Number (1) NOT NULL
Constraint CK-STD_LEVEL
Check ((SLEVEL >=0)
And (SLEVEL<=3)),
Constraint PK-STD Primary Key (SID));
9
The Check Constraint

 Another example (table clause)

Create table STUDENT (


SID Number (5),
SNAME Varchar2 (30),
DNAME Varchar2 (30) NOT NULL,
SLEVEL Number (1) NOT NULL
Constraint PK-STD Primary Key (SID)
Constraint CK-DNAME
Check (DNAME IN (‘Computing
Science’, ‘Information Technology’));
10
The Check Constraint
A Complex CHECK constraint - Affecting multiple columns

Create table STUDENT (


SID Number (5),
SNAME Varchar2 (30),
DNAME Varchar2 (30) NOT NULL,
SLEVEL Number (1) NOT NULL
Constraint PK-STD Primary Key (SID)

Constraint CK_STD_VALIDITY
Check (((SLEVEL>=0
And SLEVEL<=3))
And
(DNAME IN (‘Computing Science’, ‘Information Technology’));
11
The FOREIGN KEY Constraint
 Enforces Referential Integrity

Example
Create table STD_RECORD (
SID Number (5),
Code Varchar2 (6),
Mark number (3)
Constraint PK_STD_RECORD Primary Key (SID, Code),

Constraint FK_STD_RECORD Foreign Key (SID)


References STUDENT
Foreign Key (Code) References MODULE);
12
Maintaining Referential Integrity
Child Table:
During INSERT and UPDATE, DBMS will only allow values
matching key values in parent table

Parent table:
DBMS takes appropriate actions on UPDATE or DELETE of
key values that are referenced by child table

Actions taken include:


CASCADE
SET NULL
SET DEFAULT
NO ACTION
13
CASCADE
 Triggered by a DELETE or UPDATE operation
 A delete or update in the parent table is propagated through
all child tables referencing that column

Example
Create table STD_RECORD (
SID Number (5),
Code Varchar2 (6),
Mark number (3)
Constraint PK_STD_RECORD Primary Key (SID, Code),
Foreign Key (SID) references STUDENT
On Delete Cascade,
Foreign Key (Code) References MODULE); 14

 For Update use On Update Cascade,


SET NULL
 Triggered by a DELETE or UPDATE operation
 A delete or update in the parent table is propagated through
all child tables referencing that column

Example
Create table Patient (
PID Number (5),
Sname Varchar2 (15),
Fname Varchar2(15),
GPID Number (3)
Constraint PK_Patient Primary Key (PID),
Foreign Key (GPID) references GP
On Delete SET NULL, 15

For Update use On Update SET NULL,


SET DEFAULT & NO ACTION

 SET DEFAULT : Will set foreign key value to a


specified default value on delete or update of parent
value

 NO ACTION: If no other option is specified, then the


DBMS will take no action to Delete or Update a
parent column value being referenced by child
columns

Syntax is the same as in examples for cascade and set


default 16
Class Exercise
 Write SQL codes to create the following tables with the
constraints indicated. Also include Primary key and
Foreign Key constraints.

BRANCH(BranchNo, Street, City, Postcode)

STAFF(StaffNo, fNname, lName, DOB, Sex, Salary,


BranchNo)
Constraints:
1. Salary should be between 9000 and 30000
2. Sex should be either M or F
3. DOB field is obligatory 17

You might also like