0% found this document useful (0 votes)
8 views

2.5 - Relational Model 2 - Integrity Constraints-1

Uploaded by

Hùng Trần
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

2.5 - Relational Model 2 - Integrity Constraints-1

Uploaded by

Hùng Trần
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Relational Model - Integrity

constraints
ISYS3414 – Practical Database Concepts
Database Design Process

Hardware and
Operating System
Information about the Problem
Characteristics

Requirement Conceptual Logical Physical


Specification Schema Schema Schema
(Text) (ER / UML) (Relational / SQL) (…)

Requirements Conceptual Physical


Logical
Gathering and Design Design
Design
Analysis
Integrity Constraints

• Conditions that must be satisfied by the data


oPrevent the storage of incorrect data in tables and save storage space
oA table (must) have a set of integrity constraints
•They are defined in the respective relation schema

• Automatically enforced by the relational DBMSs


oAn operation is rejected if, as a result, it makes data incorrect

• Types of constraints:
oColumn; Key and Domain; Referential; User-defined 3
4
Domain and Column Constraints

• Domain Constraints
oEach column of a table has a data domain
oEvery value of a column has to belong to the respective data domain
oEx.: Student’s sid and age are Integers.
CREATE TABLE students (
• Column Constraints sid INTEGER,
oMore restrictive than Domain Constraints gname VARCHAR(30),
fname VARCHAR(30),
oEx.: Student’s sid is a positive Integer age INTEGER,
oEx.: Student’s gname is a string hold up to gpa REAL,
CHECK (sid > 0),
30 characters CHECK (gpa > 0.0)
);
CHECK constraints does
not work on MySQL 5
Key Constraints

• A table (must) have one or more candidate keys


oEach row is identified by the value of any candidate keys.
oThe values in the columns of any candidate key must exist (NOT NULL)

• A candidate key is a set of columns that is:


oUnique: two rows in a table have different values on these columns
•Ex.: Two students can not have the same sid
oMinimal: every column is required in order to be unique
•Ex.: A phone number is composed by the country code and the local number.

6
Key Constraints

• Primary Key CREATE TABLE employee(


id INTEGER PRIMARY KEY,
oOne of the candidate keys is selected as taxid INTEGER UNIQUE NOT
primary NULL,
...
oEach row is referenced by its values in the );
primary key
Note: By definition, a primary key is
• Candidate Keys are defined by already unique and not null.

UNIQUE NOT NULL


CREATE TABLE phone(
oUNIQUE requires that every row has a country_code INTEGER,
different value in a given column number INTEGER,
CONSTRAINT pk_phone
oNOT NULL requires a value in a given PRIMARY KEY(country_code, number)
);
column
• Examples Note: In this case the primary key is composed
of two columns (constraint named “pk_phone”)
7
8
Foreign keys

• Two ways to
specify
oWhen declaring
the column
oAs constraint

9
Referential Constraints

• Foreign Key
oColumns whose values are from a primary key of another table.
oEx.: working hours are only registered for existing employees
CREATE TABLE employee( CREATE TABLE work_hours_per_day(
id INTEGER PRIMARY KEY, id_employee INTEGER,
... day DATE,
); hours INTEGER,
CONSTRAINT pk_work_hours_per_day
The column id_employee of the table PRIMARY KEY (id_employee, day),
work_hours_per_day references the column CONSTRAINT fk_work_hours_per_day
id of the table employees. FOREIGN KEY (id_employee)
Each value of id_employee is a value that REFERENCES employee(id)
exists in the column id of the table employee. );

10
Referential Constraints

Example: students and respective grades


students(sid, gname, fname, age, gpa)
enrolled(cid, grade, sid)
sid gname fname age gpa
cid grade sid
682634 John Smith 20 3.0
MATH2239 D 123456
632461 Phu Nguyen 21 1.0
MATH2240 D 123456
612352 Thong Nguyen 19 2.7
ISYS2077 A 682634
603111 Tam Quach 20 0.8
ISYS2040 C 632461
123456 Donald Trump 23 0.1

• What is the primary key of the table enrolled?


• Is column sid the only foreign key of the table enrolled?

11
Referential Constraints

• Deleting or updating rows of referenced tables


oIt may cause problems with the foreign constraints
oEx.: What would happen if the student 123456 is deleted?

• Ways to preserve the referential integrity:


oFOREIGN KEY … REFERENCES ... ON DELETE (or UPDATE) NO ACTION
oFOREIGN KEY … REFERENCES ... ON DELETE (or UPDATE) CASCADE
oFOREIGN KEY … REFERENCES ... ON DELETE SET NULL

• Foreign Keys can be NULL (unknown / not applicable)


oEx.: person(id, name, spouse)
oA single person does not have a spouse.
12
ON DELETE - Cascade

• Allows you to
delete data from
child tables
automatically when
you delete the
data from the
parent table.
• Example:

13
User-Defined Constraints

•Constraints not covered by the previous types

•Triggers
oProcedural code that is automatically executed when a
(particular) change is made in the database.

•Assertions
oDeclarative constraints that may involve many tables. 14
Integrity Constraints Violations - Running Example

• Every Integrity Constraint violation produces a SBMSs error


students(sid, gname, fname, age, gpa)
enrolled(cid, grade, sid)
sid gname fname age gpa
cid grade sid
682634 John Smith 20 3.0
MATH2239 D 123456
632461 Phu Nguyen 21 1.0
MATH2240 D 123456
612352 Thong Nguyen 19 2.7
ISYS2077 A 682634
603111 Tam Quach 20 0.8
ISYS2040 C 632461
123456 Donald Trump 23 0.1

• Check file “3 - SQL-Integrity-Constraints.SQL”

15
Q&A

16

You might also like