Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
118 views19 pages

Chapter 6 - Database Implementation and Use

DATABASE MANAGEMENT

Uploaded by

Marah Irshedat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views19 pages

Chapter 6 - Database Implementation and Use

DATABASE MANAGEMENT

Uploaded by

Marah Irshedat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

For exclusive use with:

CHAPTER 6 - Database Implementation and Use


Copyright (c) 2020 Nenad Jukic and Prospect Press
REFERENTIAL INTEGRITY CONSTRAINT

 Referential integrity constraint - In each row of a relation


containing a foreign key, the value of the foreign key EITHER
matches one of the values in the primary key column of the
referred relation OR the value of the foreign key is null (empty).
• Regulates the relationship between a table with a foreign key and a table
with a primary key to which the foreign key refers

Jukić, Vrbsky, Nestorov, Sharma – Database Systems Copyright (c) 2020 Nenad Jukic and Prospect Press Chapter 6 – Slide 2
REFERENTIAL INTEGRITY CONSTRAINT
Referential integrity constraint compliance example

Two relations and a


referential integrity
constraint

Data records in
compliance with the
referential integrity
constraint

Jukić, Vrbsky, Nestorov, Sharma – Database Systems Copyright (c) 2020 Nenad Jukic and Prospect Press Chapter 6 – Slide 3
REFERENTIAL INTEGRITY CONSTRAINT: DELETE AND UPDATE IMPLEMENTATION OPTIONS

 Referential integrity constraint - delete and update


implementation options
• Delete options
o DELETE RESTRICT

o DELETE CASCADE

o DELETE SET-TO-NULL

o DELETE SET-TO-DEFAULT

• Update options
o UPDATE RESTRICT
o UPDATE CASCADE
o UPDATE SET-TO-NULL
o UPDATE SET-TO-DEFAULT

Jukić, Vrbsky, Nestorov, Sharma – Database Systems Copyright (c) 2020 Nenad Jukic and Prospect Press Chapter 6 – Slide 4
REFERENTIAL INTEGRITY CONSTRAINT: DELETE AND UPDATE IMPLEMENTATION OPTIONS

DELETE RESTRICT example

Jukić, Vrbsky, Nestorov, Sharma – Database Systems Copyright (c) 2020 Nenad Jukic and Prospect Press Chapter 6 – Slide 5
REFERENTIAL INTEGRITY CONSTRAINT: DELETE AND UPDATE IMPLEMENTATION OPTIONS

DELETE CASCADE example

Jukić, Vrbsky, Nestorov, Sharma – Database Systems Copyright (c) 2020 Nenad Jukic and Prospect Press Chapter 6 – Slide 6
REFERENTIAL INTEGRITY CONSTRAINT: DELETE AND UPDATE IMPLEMENTATION OPTIONS

DELETE SET-TO-NULL example

Jukić, Vrbsky, Nestorov, Sharma – Database Systems Copyright (c) 2020 Nenad Jukic and Prospect Press Chapter 6 – Slide 7
REFERENTIAL INTEGRITY CONSTRAINT: DELETE AND UPDATE IMPLEMENTATION OPTIONS

DELETE SET-TO-DEFAULT example

Jukić, Vrbsky, Nestorov, Sharma – Database Systems Copyright (c) 2020 Nenad Jukic and Prospect Press Chapter 6 – Slide 8
REFERENTIAL INTEGRITY CONSTRAINT: DELETE AND UPDATE IMPLEMENTATION OPTIONS

UPDATE RESTRICT example

Jukić, Vrbsky, Nestorov, Sharma – Database Systems Copyright (c) 2020 Nenad Jukic and Prospect Press Chapter 6 – Slide 9
REFERENTIAL INTEGRITY CONSTRAINT: DELETE AND UPDATE IMPLEMENTATION OPTIONS

UPDATE CASCADE example

Jukić, Vrbsky, Nestorov, Sharma – Database Systems Copyright (c) 2020 Nenad Jukic and Prospect Press Chapter 6 – Slide 10
REFERENTIAL INTEGRITY CONSTRAINT: DELETE AND UPDATE IMPLEMENTATION OPTIONS

UPDATE SET-TO-NULL example

Jukić, Vrbsky, Nestorov, Sharma – Database Systems Copyright (c) 2020 Nenad Jukic and Prospect Press Chapter 6 – Slide 11
REFERENTIAL INTEGRITY CONSTRAINT: DELETE AND UPDATE IMPLEMENTATION OPTIONS

UPDATE SET-TO-DEFAULT example

Jukić, Vrbsky, Nestorov, Sharma – Database Systems Copyright (c) 2020 Nenad Jukic and Prospect Press Chapter 6 – Slide 12
REFERENTIAL INTEGRITY CONSTRAINT: DELETE AND UPDATE IMPLEMENTATION OPTIONS

 Implementing delete and update options


 Example - DELETE RESTRICT and UPDATE RESTRICT

CREATE TABLE employee


( empid CHAR(4),
empname CHAR(20),
deptid CHAR(2),
PRIMARY KEY (empid),
FOREIGN KEY (deptid) REFERENCES department);

Jukić, Vrbsky, Nestorov, Sharma – Database Systems Copyright (c) 2020 Nenad Jukic and Prospect Press Chapter 6 – Slide 13
REFERENTIAL INTEGRITY CONSTRAINT: DELETE AND UPDATE IMPLEMENTATION OPTIONS

 Implementing delete and update options


 Example - DELETE CASCADE

CREATE TABLE employee


( empid CHAR(4),
empname CHAR(20),
deptid CHAR(2),
PRIMARY KEY (empid),
FOREIGN KEY (deptid) REFERENCES department
ON DELETE CASCADE);

Jukić, Vrbsky, Nestorov, Sharma – Database Systems Copyright (c) 2020 Nenad Jukic and Prospect Press Chapter 6 – Slide 14
REFERENTIAL INTEGRITY CONSTRAINT: DELETE AND UPDATE IMPLEMENTATION OPTIONS

 Implementing delete and update options


 Example - UPDATE SET-TO-NULL

CREATE TABLE employee


( empid CHAR(4),
empname CHAR(20),
deptid CHAR(2),
PRIMARY KEY (empid),
FOREIGN KEY (deptid) REFERENCES department
ON UPDATE SET NULL);

Jukić, Vrbsky, Nestorov, Sharma – Database Systems Copyright (c) 2020 Nenad Jukic and Prospect Press Chapter 6 – Slide 15
REFERENTIAL INTEGRITY CONSTRAINT: DELETE AND UPDATE IMPLEMENTATION OPTIONS

 Implementing delete and update options


 Example - DELETE CASCADE and UPDATE SET-TO-NULL

CREATE TABLE employee


( empid CHAR(4),
empname CHAR(20),
deptid CHAR(2),
PRIMARY KEY (empid),
FOREIGN KEY (deptid) REFERENCES department
ON DELETE CASCADE
ON UPDATE SET NULL);

Jukić, Vrbsky, Nestorov, Sharma – Database Systems Copyright (c) 2020 Nenad Jukic and Prospect Press Chapter 6 – Slide 16
IMPLEMENTING USER-DEFINED CONSTRAINTS

 CHECK
• Used to specify a constraint on a particular column of a relation

Jukić, Vrbsky, Nestorov, Sharma – Database Systems Copyright (c) 2020 Nenad Jukic and Prospect Press Chapter 6 – Slide 17
CHECK – Example 1

A relation with a
user-defined
constraint

CREATE TABLE employee


SQL code (empid CHAR(4),
salary NUMBER(6) CHECK (salary >= 50000 AND salary <= 200000),
PRIMARY KEY (empid));

INSERT INTO employee VALUES ('1234', 75000);


INSERT INTO employee VALUES ('2345', 50000);
INSERT INTO employee VALUES ('3456', 55000);
INSERT INTO employee VALUES ('4567', 70000);
INSERT INTO employee VALUES ('9876', 270000);
INSERT INTO employee VALUES ('1010', 30000);

Four inserts
accepted, two
inserts rejected
Jukić, Vrbsky, Nestorov, Sharma – Database Systems Copyright (c) 2020 Nenad Jukic and Prospect Press Chapter 6 – Slide 18
CHECK – Example 2

A relation with a
user-defined
constraint

CREATE TABLE student


SQL code (studentid CHAR(4),
yearenrolled INT,
yearofgraduation INT,
PRIMARY KEY (studentid),
CHECK (yearenrolled <= yearofgraduation));

INSERT INTO student VALUES ('1111', 2012, 2016);


INSERT INTO student VALUES ('2222', 2013, 2017);
INSERT INTO student VALUES ('3333', 2013, 2017);
INSERT INTO student VALUES ('4444', 2013, 2012);

Three inserts
accepted, one
insert rejected

Jukić, Vrbsky, Nestorov, Sharma – Database Systems Copyright (c) 2020 Nenad Jukic and Prospect Press Chapter 6 – Slide 19

You might also like