Docsity

Database Constraints: Types, Importance, and Enforcement, Lecture notes of Database Management Systems (DBMS)

Partial preview of the text

Download Database Constraints: Types, Importance, and Enforcement and more Lecture notes Database Management Systems (DBMS) in PDF only on Docsity!

Constraints of

several types

Constraints and Triggers

 For relational databases

 SQL standard; systems vary considerably

(Integrity) Constraints

constrain allowable database states

Triggers

monitor database changes,

check conditions and initiate actions

Integrity Constraints

Impose restrictions on allowable data, beyond those

imposed by structure and types

Why use them?

Data entry error (during INSERT) Correctness (during UPDATE) Enforce consistency Tell system about the data (etc. uniqueness)

Integrity Constraints

Impose restrictions on allowable data, beyond those

imposed by structure and types

 Non-null constraints

 Key constraints

 Referential Integrity (foreign key) constraints

 Attribute-based and tuple-based constraints

 General assertions

sID sName GPA HS sID^ uName^ major^ dec uName city enr University Student Apply Referential Integrity Referential integrity from R.A to S.B Each value in column A of table R must appear in column B of table S In our example database Each value in column sID of table Apply must appear in column sID of table Student

Simple Example Database

sID sName GPA HS sID^ uName^ major^ dec uName city enr Student Apply University Referential Integrity Referential integrity from R.A to S.B Each value in column A of table R must appear in column B of table S  (^) A is called the “foreign key”  (^) B is usually required to be the primary key for table S or at least unique  (^) Multi-attribute foreign keys are allowed

Referential Integrity Referential Integrity Enforcement ( R.A to S.B ) Special actions:  (^) Delete from S Restrict (default), Set Null , Cascade  (^) Update S.B Restrict (default), Set Null , Cascade sID sName GPA HS sID^ uName^ major^ dec uName city enr Student Apply University

Constraints Demo

Simple university admissions database

University ( uName , city , enrollment ) Student ( sID , sName , GPA , sizeHS ) Apply ( sID , uName , major , decision )

PRIMARY KEYS

Student IDs are unique create table Student(sID int primary key, sName text, GPA real, sizeHS int);

Simple university admissions database

University ( uName , city , enrollment ) Student ( sID , sName , GPA , sizeHS ) Apply ( sID , uName , major , decision )

ONLY ONE PRIMARY KEY ALLOWED

create table Student(sID int primary key, sName text primary key, GPA real, sizeHS int); BUT ANY NUMBER OF UNIQUE KEYS create table Student(sID int primary key, sName text unique, GPA real, sizeHS int unique);

Simple university admissions database

University ( uName , city , enrollment ) Student ( sID , sName , GPA , sizeHS ) Apply ( sID , uName , major , decision )

ATTRIBUTE-BASED CONSTRAINTS

GPA and sizeHS are in range create table Student(sID int, sName text, GPA real check(GPA <= 4.0 and GPA > 0.0), sizeHS int check(sizeHS < 5000));

Simple university admissions database

University ( uName , city , enrollment ) Student ( sID , sName , GPA , sizeHS ) Apply ( sID , uName , major , decision )

TUPLE-BASED CONSTRAINTS

No CS admits to NUST create table Apply(sID int, uName text, major text, decision text, check(decision = 'N' or uName != ‘Comsats' or major != 'CS'));

Simple university admissions database

University ( uName , city , enrollment ) Student ( sID , sName , GPA , sizeHS ) Apply ( sID , uName , major , decision )

CASCADE AND SET NULL OPTIONS

create table Apply(sID int references Student(sID) on delete set null, uName text references University(uName) on update cascade, major text, decision text);

Simple university admissions database

University ( uName , city , enrollment ) Student ( sID , sName , GPA , sizeHS ) Apply ( sID , uName , major , decision )