IT3306 - 02 - Database Constraints and Triggers
IT3306 - 02 - Database Constraints and Triggers
• Categories of Constraints
• Domain Constraints
• Key Constraints and Constraints on NULL Values
• Entity Integrity and Referential Integrity
• Other Types of Constraints
• Handling Constraint Violations for Insert, Delete and Update
Operations
• Inherent constraints
■ A relation consists of a certain number of simple attributes.
■ An attribute value is atomic
■ No duplicate tuples are allowed
Employee
• Other than the Primary Key, there can be other attributes which
cannot contain NULL values.
• For an example, if the columns Empname, Hire_date, NIC and
salary cannot contain NULL values, when the table is created, you
can state it as follows.
• NOT NULL constraint makes sure that a column does not hold
NULL value. When we cannot give a value for a particular column
while inserting a record into a table, default value taken by it is
NULL. By specifying NOT NULL constraint, we ensure that a
particular column(s) does (do) not contain NULL values.
Employee Department
(Referencing Table) (Referenced Table)
Empid ename address did Dept_id dname
E1001 Amal Silva Kandy 002 001 HR
E1002 Shiva Colombo 001 002 Finance
Kumar
003 Research
E1003 Fathima Ampara NULL
Siyam 004 Marketing
Relation Yes/No
Student
Professor
Course
Transcript
Teaching
Department
© e-Learning Centre, UCSC 36
Activity
• Following two tables illustrate rows that a user tries to enter. Identify
what would happen when each of the tuple is inserted into the two
tables given below. Assume that the course table already has the
rows relevant to CS100, CS101 and CS103 courses.
Students(student_id CHAR(05), name VARCHAR(50), age INT)
StudentMarks(stdid CHAR(05),course_id CHAR(05),grade CHAR)
• The other two actions are SET DEFAULT and SET NULL.
• With these actions, when you update or delete a value in the
referenced table you can set a default value or null for the
referencing value.
CONSTRAINT Student_ID_FK
FOREIGN KEY (stdid) REFERENCES Students (student_id)
ON DELETE SET NULL ON UPDATE SET DEFAULT
would prevent
If an insert violates a
referential integrity
constraint
violation
SELECT *
FROM EMPLOYEE E, EMPLOYEE M, DEPARTMENT D
WHERE E.Salary>M.Salary AND E.Dno = D.Dnumber
AND D.Mgr_ssn = M.Ssn ) );
Conditions
© e-Learning Centre, UCSC 59
Activity
• Row Triggers
• Trigger is fired once for each row in a transaction.
• If an update statement modifies multiple tuples of a table, a
row trigger is fired once for each tuple which are affected by
the update query. If there is no tuple affected by the query,
then the row trigger is not executed.
• Have access to :new (new values) and :old (old values).
E.g. update the total salary of a department when the salary
value of an employee tuple is changed.
• Statement Triggers
• Trigger is fired once for each transaction.
• If a DELETE statement deletes several rows from a table, a
statement-level DELETE trigger is fired only once, regardless
of how many rows are deleted from the table.
• Does not have access to :new and :old values.
E.g. Delete a row relevant to an employee who has
completed the contract period.
• Before Trigger
• Runs before any change is made to the database.
E.g. Before withdrawing money account balance is required to
be checked.
• After Trigger
• Runs after changes are made to the database
E.g. After withdrawing money update the account balance
• Instead of
• INSTEAD OF triggers are used to modify views that cannot be
modified directly through UPDATE, INSERT, and DELETE
statements.
When inserting values for the Employee table ensure whether the
salary is >= 20000.
Employee(Empid, Ename, Salary, Dno).
• Create a trigger that accepts insertion into the student table and
checks the GPA. If the GPA of the inserted student is greater than
3.3, or less than or equal to 3.6, that student will be automatically
applying for Computer Science stream. Otherwise, the student has
to apply for Software Engineering stream (Stream is Computer
Science, Biology etc; enrollment is number of students enrolled for
the college, decision is yes or no in getting selected for a stream)