Professional Documents
Culture Documents
Outline
Table definition
Tuple insertion/deletion/update
Triggers
2
Overview (review)
Table creation involves two tasks
defining the schema
• using statements like type definitions in C
3
Creating a table (review)
CREATE TABLE STUDENTS(
stu-id INTEGER,
name CHAR(20),
cname CHAR(20),
gpa REAL,
PRIMARY KEY (stu-id))
Other constraints
UNIQUE
FOREIGN KEY
4
Null values
Attribute values in a tuple are sometimes unknown (e.g., gpa not
assigned yet) or inapplicable (e.g., no Chinese name).
NULL is provided for such situations
SELECT acc-id
FROM DEPOSIT
WHERE cust-id IS NOT NULL
SELECT acc-id
FROM DEPOSIT
WHERE cust-id < 8
Null values (cont.)
3-valued logic (true, false and unknown)
Expression Result
cust-id < 8 NULL
cust-id = 8 NULL
cust-id = 8 OR amount > 100 TRUE
cust-id = 8 OR amount < 100 NULL
cust-id = 8 AND amount > 100 NULL
cust-id = 8 AND amount < 100 FALSE
cust-id IS NULL TRUE
cust-id IS NOT NULL FALSE
NOT (cust-id < 8), i.e., NOT NULL NULL
6
Null values (cont.)
We have a 3-valued logic (true, false and unknown).
WHERE clause eliminates tuples that don’t evaluate to true (i.e.,
discards tuples with false and NULL values).
SELECT acc-id
FROM DEPOSIT
WHERE cust-id < 8
8
Check – Example 2
We already have a table CLUB, recording the GPA
of all members in the dancing club.
9
Check – Example 2
We require that every panel member should have
a GPA at least 1.7.
10
Outline
Table definition
Tuple insertion/deletion/update
Triggers
11
Insertion
To insert a single tuple
INSERT INTO DEPOSIT
VALUES('A1', 1, 20000) use '... ' to
quote a string
13
Delete tuples – Example 2
Delete the tuple corresponding to
the account with the largest balance.
14
Update tuples
Update the balance of account A1
to 15k.
UPDATE ACC
SET balance = 15000
WHERE acc-id = ‘A1’
For all the accounts with balance at least 10k, give them an interest
of 5%.
UPDATE ACC
SET balance = balance * 1.05
WHERE balance > 10000
15
Outline
Table definition
Tuple insertion/deletion/update
Triggers
16
Overview
We have learned to write the following constraints.
Primary/candidate key (unique)
Foreign key
CHECK
Trigger.
A trigger can be regarded as a procedure automatically executed by
the database, whenever a certain table is modified.
17
Example 1
STU (stu-id, name, score);
CREATE OR REPLACE TRIGGER STU_score_constraint
BEFORE INSERT OR UPDATE ON STU
indicates that the trigger will
FOR EACH ROW inspect the affected row.
BEGIN
IF (:new.score < 0 OR :new.score > 100) THEN
RAISE_APPLICATION_ERROR(-20000, ‘Invalid score’);
END IF;
END;
terminate the insertion
.
20
STU (stu-id, name, dept-id); DEPT-CNT (dept-id, stu-cnt);
CREATE OR REPLACE TRIGGER STU_stat_insert
AFTER INSERT ON STU the trigger will be executed
after a successful insert
FOR EACH ROW
DECLARE
c INTEGER; Count # occurrences of new record’s dept-id
BEGIN in table DEPT-CNT and save it into c
SELECT COUNT(*) into c FROM DEPT-CNT
WHERE dept-id = :new.dept-id;
IF (c = 0) THEN If this dept-id doesn’t exist, add a record
INSERT INTO DEPT-CNT VALUES(:new.dept-id, 1);
ELSE Otherwise, increment the student count
UPDATE DEPT-CNT SET stu-cnt = stu-cnt + 1
WHERE dept-id = :new.dept-id;
END IF;
END;
.
21
A Common Mistake
STU (stu-id, name, dept-id); DEPT-CNT (dept-id, stu-cnt);
CREATE OR REPLACE TRIGGER STU_stat_insert
AFTER INSERT ON STU
FOR EACH ROW
DECLARE
c INTEGER;
BEGIN
SELECT COUNT(*) into c FROM STU
WHERE dept-id = :new.dept-id;
24
A Specific Example
After insertions
STU DEPT-CNT
stu-id name dept-id dept-id stu-cnt
1 Bobby COMP COMP 3
2 Sherry COMP PHYS 1
3 Timmy PHYS MATH 1
4 Jerry COMP
5 Tony MATH
25
A Specific Example
After deletions
STU DEPT-CNT
stu-id name dept-id dept-id stu-cnt
1 Bobby COMP COMP 2
4 Jerry COMP MATH 1
5 Tony MATH
26