slides9_constraint_trigger
slides9_constraint_trigger
Learning objectives
1
Outline
• Constraints
• Triggers
1. Introduction
Teachers
Student
DBMS Server
Clazz
Enrollment
Studying Subject
…
Integrity of the
Students
information?
Applications
2
1. Introduction: Database Schema
student(student_id, first_name,last_name, dob, gender,
address, note, email, clazz_id)
clazz(clazz_id, name, lecturer_id, monitor_id, number_students)
subject(subject_id, name, credit, percentage_final_exam)
enrollment(student_id, subject_id, semester, midterm_score, final_score)
lecturer(lecturer_id, first_name, last_name, dob, gender, address, email)
teaching(subject_id, lecturer_id)
grade(code, from_score, to_score)
3
2. Constraints
2.1. Keys: PRIMARY KEY vs. UNIQUE
2.2. Foreign keys
2.3. Attribute-based checks
2.4. Tuple-based checks
2.5. Assertions
• Keys
• Foreign-key, or referential-integrity
• Value-based constraints
• Constrain values of a particular attribute.
• Tuple-based constraints
• Relationship among components
• Assertions: any SQL boolean expression
4
2.1. Keys: PRIMARY KEY vs. UNIQUE
• Declaring: similar syntax as primary key
• Example:
CREATE TABLE student (
student_id CHAR(8) NOT NULL,
first_name VARCHAR(20) NOT NULL,
last_name VARCHAR(20) NOT NULL,
...
email varchar(50) UNIQUE,
clazz_id CHAR(8),
CONSTRAINT student_pk PRIMARY KEY (student_id));
10
10
5
2.2. Expressing Foreign Keys
11
11
12
12
6
2.2. Foreign keys: Enforcing constraint
• An insert or update to student that introduces a non-existent
clazz_id (clazz_id value is not found in clazz)
Reject
• A deletion or update to clazz that removes a clazz_id value found
in some tuples of student?
• Default: reject the modification
• Cascade: make the same changes in student
• Set NULL: change clazz_id in student to NULL
13
13
14
14
7
2.3. Attribute-based checks
• Declaring
• Constraints on the value of a particular attribute
• Add CHECK(<condition>) to the declaration for the attribute or add as
relation-schema element
• The condition may use the name of the attribute, but any other relation or
attribute name must be in a subquery
• Example:
CREATE TABLE student (
student_id CHAR(8) NOT NULL PRIMARY KEY, ...,
gender CHAR(1),
clazz_id CHAR(8) CHECK (clazz_id IN (SELECT clazz_id FROM clazz)),
CONSTRAINT student_chk_gender CHECK (gender = 'F' OR gender = 'M'));
15
15
• Timing of checks
• Only when a value for that attribute is inserted or updated
16
16
8
2.4. Tuple-based checks
17
17
2.5. Assertions
• Declaring
• Database-schema elements, like relations or views
• Defined by:
18
18
9
2.5. Assertions: Example
19
19
2.5. Assertions
20
20
10
3. Triggers
3.1. Motivation
3.2. Trigger
3.3. Using trigger
3.4. Examples
21
21
3.1. Motivation
• Assertions
• powerful,
• but the DBMS often can’t tell when they need to be checked
• Attribute- and tuple-based checks
• checked at known times,
• but are not powerful
• Triggers let the user decide when to check for any condition
22
22
11
3.1. Motivation: ECA Rules
23
23
Event
CREATE TRIGGER clazz_changes_tg
AFTER INSERT ON student
REFERENCING NEW ROW AS nnn
Condition
FOR EACH ROW
WHEN (nnn.clazz_id IS NOT NULL)
BEGIN
update clazz Action
set number_students = number_students + 1
where clazz_id = nnn.clazz_id;
END;
24
24
12
3.2. Trigger: Definition syntax
• Creating a trigger:
CREATE [OR REPLACE] TRIGGER <trigger_name>
{BEFORE | AFTER | INSTEAD OF }
{INSERT | DELETE | UPDATE [OF <attribute_name>]}
ON <table_name>
REFERENCING {NEW | OLD} {ROW | TABLE} AS <name>
[FOR EACH ROW ]
[WHEN (<condition>) ]
BEGIN
<trigger body goes here >
END;
• Dropping a trigger:
DROP TRIGGER <trigger_name>;
25
25
26
26
13
3.2. Trigger: Level
• Row-level trigger:
• Indicated by option FOR EACH ROW
• Trigger executes once for each modified tuple
• Statement-level trigger:
• Without option FOR EACH ROW or with FOR EACH
STATEMENT
• Trigger execute once for a SQL statement, regardless how
many
tuples are modified
27
27
28
28
14
3.2. Trigger: Condition
29
29
30
30
15
3.3. Using triggers
• Auditing data modification (keeping history of data), providing
transparent
event logging
• Validation and business security checking if so is desired
• Eg. column formatting before and after inserts into database
• Enforcing complex integrity constraints
• Enforcing complex business rules
• Maintaining replicate tables
• Building complex views that are updatable
•…
31
31
32
32
16
3.4. Examples: Oracle
33
33
34
34
17
Summary
• Constraints, Assertions, Triggers:
• How to declare
• Timing of checks
• Differences
• Only use them if you really need to, especially triggers
• Each DBMS has its own variation in implementation:
• Options
• Syntax: triggers as an example
• Reading documentation for each DBMS installed
35
35
References
• Hector Garcia-Molina, Jeffrey D. Ullman, Jennifer Widom.
Database Systems: The Complete Book. Pearson Prentice Hall.
the 2nd edition. 2008: Chapter 7
• Nguyen Kim Anh, Nguyên lý các hệ cơ sở dữ liệu, NXB Giáo
dục. 2004: Chương 7
37
37
18