Database Systems - BIT University of Colombo
Database Systems - BIT University of Colombo
Systems II
BIT – 3rd Year
Semester 6
IT6405: Database Systems II
Learning Outcome
After successful completion of this
course students will be able to:
– create stored procedures and triggers
– describe data storage & access and
manipulate query processing techniques
– demonstrate transaction processing
techniques of database systems
– determine designs for distributed databases
UCSC - 2016 2
IT6405: Database Systems II
Outline of Syllabus
1. Stored Procedures and Triggers
2. Data Storage and Querying
3. Transaction Management
4. Distributed Databases
UCSC - 2016 3
IT6405: Database Systems II
References
1. Elmasri, Navathe, Somayajulu, and Gupta,
“Fundamentals of Database Systems”, 5th Edition,
Pearson Education (2008)
Note: 6th Edition released in 2011
2. Silberschatz A., Korth H.F. and Sudarshan S., “Database
System Concepts”, 5th Edition, McGraw Hill (2006).
Note: 6th Edition released in 2010
3. Ramakrishnan, Gehrke, “Database Management
Systems”, 3rd edition, McGraw Hill
UCSC - 2016 4
IT6405: Database Systems II – Topic 1
Duration: 06 hours
UCSC - 2016 5
IT6405: Database Systems II – Topic 1
Learning Objectives
• Write stored procedures and triggers in
SQL
UCSC - 2016 6
IT6405: Database Systems II – Topic 1
Detailed Syllabus
1.1 Constraints and triggers (03hrs) [REF1: Chapter 1,
5, 26, pg. 21, 132, 933-945]
UCSC - 2016 7
IT6405: Database Systems II – Topic 1
UCSC - 2016 8
IT6405: Database Systems II – Topic 1
Data Integrity
• It ensures that the values entering to the
database is accurate and valid
• Uses integrity constraints
• Integrity constraints maintains accurate
databases by eliminating invalid data
updates/ insert/ deletes
UCSC - 2016
Common Database Integrity
Constraints
• Entity integrity does not allow two rows with
the same identity in a table
• Domain integrity allows only predefined
values
• Referential integrity allows only the
consistency of values
across related tables
• User-defined integrity define constraints
UCSC - 2016 10
IT6405: Database Systems II – Topic 1
Database Constraints
• They are the restrictions on the contents of
the database and its operations
• Types of Constraints
– Primary key constraint
– Foreign key constraint (referential integrity)
– Unique constraint
– NOT NULL constraint
– Check Constraint
– Default constraint
UCSC - 2016 11
Primary Key & Unique Constraints
CREATE TABLE Student (
stdid CHAR (10),
name CHAR(20),
address CHAR(25),
age INTEGER,
CONSTRAINT pk_stdID PRIMARY KEY (stdid) );
CREATE TABLE Student (
Stdid CHAR (10) PRIMARY KEY,
Name CHAR(20),
Address CHAR(25),
Age INTEGER,
NIC CHAR(10) UNIQUE );
UCSC - 2016 12
IT6405: Database Systems II – Topic 1
UCSC - 2016 13
IT6405: Database Systems II – Topic 1
UCSC - 2016 14
IT6405: Database Systems II – Topic 1
Default Constraint
CREATE TABLE Student (
Name CHAR(20),
Address CHAR(25),
Department CHAR (20)
DEFAULT ‘Computer Science’,
Age INTEGER
);
UCSC - 2016 15
IT6405: Database Systems II – Topic 1
Check Constraint
CREATE TABLE UnderGrad_Student (
Sid Number Primary Key,
Name CHAR(20),
Address CHAR(25),
Age INTEGER,
Reg_Course CHAR(10),
CHECK (Age BETWEEN 19 AND 26)
);
UCSC - 2016 16
Referential Triggered Action
• Operations
ON DELETE, ON UPDATE
• Actions To Take
SET NULL, CASCADE, SET DEFAULT,
NO ACTION, RESTRICT
CREATE TABLE Employee (
NIC VARCHAR(10) PRIMARY KEY,
name VARCHAR(50) ,
works_in INTEGER,
CONSTRAINT fk_EmpDept FOREIGN KEY
(works_in) REFERENCES Dept ON DELETE
CASCADE ON UPDATE NO ACTION
); UCSC - 2016 17
IT6405: Database Systems II – Topic 1
Drop Tables
The actions to take when Dropping tables
UCSC - 2016 18
IT6405: Database Systems II – Topic 1
Triggers
Types of Triggers
• Row Triggers - For each row of the table
trigger is fired
E.g. delete each employee from employee
table whose department is IT
• Statement Triggers - Only once the trigger will
fire
E.g. update salary of every employee working
for the IT department
UCSC - 2016 22
IT6405: Database Systems II – Topic 1
Triggers
• Trigger Timing
Before Trigger – runs before any change is
made to the database.
E.g. if you want to withdraw money from ATM
first need to check account balance before
doing the transaction
• After Trigger - runs after changes are made to
the database
E.g. After withdrawing money update the
account balance
UCSC - 2016 23
IT6405: Database Systems II – Topic 1
UCSC - 2016 26
CREATE TRIGGER Salary-trigger
BEFORE UPDATE OF Salary ON Employee
condition
action
UCSC - 2016 27
IT6405: Database Systems II – Topic 1
• Postgres
– Row-level
– Statement-level
– old/new row/table
• MySQL
– Row-level only
– No Old/New Table
– For Each Row Implicit
– No Referencing
– Only one trigger per event type
– Limited trigger chaining
UCSC - 2016 30
IT6405: Database Systems II – Topic 1
If GPA (>= 2.5) eligible for Physics Special
when Update GPA need to identify Eligible students
Student
sID sName gender GPA
Ex
130001 Sunil M 1.63
130002 Anil M 0
130004 Nimali F 3.75
Eligible
sID discipline Major Decision
130004 Physics Physics
130004 Physics Engineering Physics
130004 Physics Computational Physics
UCSC - 2016
create trigger trigger_name
timing event1 on table_name
condition
Ex
action Insert into Student values(
130006, ‘Paul’, ‘M’, 3.5);
Insert into Student values(
Create trigger special 130007, ‘Kevin’, ‘M’, 2.3);
After insert on Student
for each row
when New.GPA >= 2.5 and 65 < (select mark from Marks
where sID = New.sID and cID=‘PH111’)
begin
Insert into Eligible values( New.sID,
‘Physics’, ‘Physics’, null);
Insert into Eligible values( New.sID,
‘Physics’, ‘Engineering Physics’, null);
Insert into Eligible values( New.sID,
‘Physics’, ‘Computational Physics’, null);
end; 32
create trigger trigger_name
timing event1 on table_name
condition
Ex
action
UPDATE Student
Insert into Student values(
SET sName= ‘Paul’
130009, ‘Paul’, ‘M’, 2.5);
WHERE sID = 130009;
33
IT6405: Database Systems II – Topic 1
REFERENCING
OLD AS OldTuple
NEW AS NewTuple
REFERENCING
OLD_TABLE AS OldStuff
NEW_TABLE AS NewStuff
UCSC - 2016 37
IT6405: Database Systems II – Topic 1
Statement Triggers
Monitoring Statement Events
SQL> INSERT INTO Dept (DeptNo, Dname, Loc)
2 VALUES (50, ‘Marketing', ‘Colombo');
UCSC - 2016
Row Triggers
Monitoring Row Events
SQL> UPDATE Emp
2 SET sal = sal * 1.1
3 WHERE deptno = 30;
39
AFTER statement trigger
Example: Registering Operations
SQL> CREATE TRIGGER increase_salary_trg
2 BEFORE UPDATE
3 ON Emp
4 BEGIN
5 INSERT INTO sal_hist (increased, t)
6 VALUES (YES, SYSDATE);
7 END;
UCSC - 2016 40
Example: Calculating Derived
Columns
SQL>CREATE OR REPLACE TRIGGER derive_commission_trg
2 BEFORE UPDATE OF sal ON Emp
3 FOR EACH ROW
4 WHEN (new.job = 'Salesman')
5 BEGIN
6 :new.comm := :old.comm * (:new.sal/:old.sal);
7 END;
Trigger name: derive_commission_trg
Timing: BEFORE executing the statement
Triggering event: UPDATE of sal column
Filtering condition: job = ‘Salesman’
Target: Emp table
Trigger parameters: old, new
Trigger action: calculate the new commission
to be updated
UCSC - 2016 41
IT6405: Database Systems II – Topic 1
UCSC - 2016 42
IT6405: Database Systems II – Topic 1
… continuation
MAX_INS MAX_UPD MAX_DEL
5 5 5
5
5 0 0
UCSC - 2016 43
Example: Counting Statement
Execution
SQL>CREATE OR REPLACE TRIGGER audit_emp
2 AFTER DELETE ON Emp
3 FOR EACH ROW
4 BEGIN
5 UPDATE audit_table SET del = del + 1
6 WHERE user_name = :old.ename
7 AND table_name = 'Emp’;
7 END;
UCSC - 2016 44
Tracing Record
Value Changes
USER_NAME TIMESTAMP ID OLD_LAST_NAME NEW_LAST_NAME
Gihan 12-NOV-97 7950 NULL Dias
… continuation
OLD_TITLE NEW_TITLE OLD_SALARY NEW_SALARY
NULL Analyst NULL 35000
UCSC - 2016 45
Example: Recording Changes
SQL>CREATE OR REPLACE TRIGGER audit_emp_values
2 AFTER DELETE OR INSERT OR UPDATE ON Emp
3 FOR EACH ROW
4 BEGIN
5 INSERT INTO audit_emp_values (user_name,
6 timestamp, id, old_last_name, new_last_name,
7 old_title, new_title, old_salary, new_salary)
8 VALUES (USER, SYSDATE, :old.empno, :old.ename,
9 :new.ename, :old.job, :new.job,
10 :old.sal, :new.sal);
11 END;
UCSC - 2016 46
Example: Protecting Referential
Integrity
SQL>CREATE OR REPLACE TRIGGER
cascade_updates
2 AFTER UPDATE OF deptno ON Dept
3 FOR EACH ROW
4 BEGIN
5 UPDATE Emp
6 SET Emp.deptno = :new.deptno
7 WHERE Emp.deptno = :old.deptno;
8 END
Whenever the department number changes, all employee records
for this department will automatically be changed as well, so that
the employees will continue to work for the same department.
UCSC - 2016 47