Home | | Database Management Systems | Specifying Constraints as Assertions and Actions as Triggers

Chapter: Fundamentals of Database Systems : The Relational Data Model and SQL : More SQL: Complex Queries, Triggers, Views, and Schema Modification

Specifying Constraints as Assertions and Actions as Triggers

1. Specifying General Constraints as Assertions in SQL 2. Introduction to Triggers in SQL

Specifying Constraints as Assertions and Actions as Triggers

 

In this section, we introduce two additional features of SQL: the CREATE ASSERTION statement and the CREATE TRIGGER statement. Section 5.2.1 discusses CREATE ASSERTION, which can be used to specify additional types of constraints that are outside the scope of the built-in relational model constraints (primary and unique keys, entity integrity, and referential integrity) that we presented in Section 3.2. These built-in constraints can be specified within the CREATE TABLE statement of SQL (see Sections 4.1 and 4.2).

 

Then in Section 5.2.2 we introduce CREATE TRIGGER, which can be used to specify automatic actions that the database system will perform when certain events and conditions occur. This type of functionality is generally referred to as active data-bases. We only introduce the basics of triggers in this chapter, and present a more complete discussion of active databases in Section 26.1.

 

1. Specifying General Constraints as Assertions in SQL

 

In SQL, users can specify general constraints—those that do not fall into any of the categories described in Sections 4.1 and 4.2—via declarative assertions, using the CREATE ASSERTION statement of the DDL. Each assertion is given a constraint name and is specified via a condition similar to the WHERE clause of an SQL query. For example, to specify the constraint that the salary of an employee must not be greater than the salary of the manager of the department that the employee works for in SQL, we can write the following assertion:

 

CREATE ASSERTION SALARY_CONSTRAINT

 

CHECK ( NOT EXISTS               ( SELECT     *

FROM                                      EMPLOYEE E, EMPLOYEE M,

 

DEPARTMENT D

 

WHERE                                    E.Salary>M.Salary

 

AND E.Dno=D.Dnumber

AND D.Mgr_ssn=M.Ssn ) );

 

The constraint name SALARY_CONSTRAINT is followed by the keyword CHECK, which is followed by a condition in parentheses that must hold true on every data-base state for the assertion to be satisfied. The constraint name can be used later to refer to the constraint or to modify or drop it. The DBMS is responsible for ensuring that the condition is not violated. Any WHERE clause condition can be used, but many constraints can be specified using the EXISTS and NOT EXISTS style of SQL conditions. Whenever some tuples in the database cause the condition of an ASSERTION statement to evaluate to FALSE, the constraint is violated. The con-straint is satisfied by a database state if no combination of tuples in that database state violates the constraint.

The basic technique for writing such assertions is to specify a query that selects any tuples that violate the desired condition. By including this query inside a NOT EXISTS clause, the assertion will specify that the result of this query must be empty so that the condition will always be TRUE. Thus, the assertion is violated if the result of the query is not empty. In the preceding example, the query selects all employees whose salaries are greater than the salary of the manager of their department. If the result of the query is not empty, the assertion is violated.

 

Note that the CHECK clause and constraint condition can also be used to specify constraints on individual attributes and domains (see Section 4.2.1) and on individual tuples (see Section 4.2.4). A major difference between CREATE ASSER-TION and the individual domain constraints and tuple constraints is that the CHECK clauses on individual attributes, domains, and tuples are checked in SQL only when tuples are inserted or updated. Hence, constraint checking can be imple-mented more efficiently by the DBMS in these cases. The schema designer should use CHECK on attributes, domains, and tuples only when he or she is sure that the constraint can only be violated by insertion or updating of tuples. On the other hand, the schema designer should use CREATE ASSERTION only in cases where it is not possible to use CHECK on attributes, domains, or tuples, so that simple checks are implemented more efficiently by the DBMS.

 

2. Introduction to Triggers in SQL

 

Another important statement in SQL is CREATE TRIGGER. In many cases it is convenient to specify the type of action to be taken when certain events occur and when certain conditions are satisfied. For example, it may be useful to specify a condition that, if violated, causes some user to be informed of the violation. A manager may want to be informed if an employee’s travel expenses exceed a certain limit by receiving a message whenever this occurs. The action that the DBMS must take in this case is to send an appropriate message to that user. The condition is thus used to monitor the database. Other actions may be specified, such as executing a specific stored procedure or triggering other updates. The CREATE TRIGGER statement is used to implement such actions in SQL. We discuss triggers in detail in Section 26.1 when we describe active databases. Here we just give a simple example of how triggers may be used.

 

Suppose we want to check whenever an employee’s salary is greater than the salary of his or her direct supervisor in the COMPANY database (see Figures 3.5 and 3.6). Several events can trigger this rule: inserting a new employee record, changing an employee’s salary, or changing an employee’s supervisor. Suppose that the action to take would be to call an external stored procedure SALARY_VIOLATION, which will notify the supervisor. The trigger could then be written as in R5 below. Here we are using the syntax of the Oracle database system.

 

R5: CREATE TRIGGER SALARY_VIOLATION

 

BEFORE INSERT OR UPDATE OF SALARY, SUPERVISOR_SSN

 

ON EMPLOYEE

FOR EACH ROW

 

WHEN ( NEW.SALARY > ( SELECT SALARY FROM EMPLOYEE

WHERE SSN = NEW.SUPERVISOR_SSN ) )

INFORM_SUPERVISOR(NEW.Supervisor_ssn,

NEW.Ssn );

 

The trigger is given the name SALARY_VIOLATION, which can be used to remove or deactivate the trigger later. A typical trigger has three components:

 

              The event(s): These are usually database update operations that are explicitly applied to the database. In this example the events are: inserting a new employee record, changing an employee’s salary, or changing an employee’s supervisor. The person who writes the trigger must make sure that all possi-ble events are accounted for. In some cases, it may be necessary to write more than one trigger to cover all possible cases. These events are specified after the keyword BEFORE in our example, which means that the trigger should be executed before the triggering operation is executed. An alternative is to use the keyword AFTER, which specifies that the trigger should be executed after the operation specified in the event is completed.

 

              The condition that determines whether the rule action should be executed: Once the triggering event has occurred, an optional condition may be evaluated. If no condition is specified, the action will be executed once the event occurs. If a condition is specified, it is first evaluated, and only if it evaluates to true will the rule action be executed. The condition is specified in the WHEN clause of the trigger.

 

              The action to be taken: The action is usually a sequence of SQL statements, but it could also be a database transaction or an external program that will be automatically executed. In this example, the action is to execute the stored procedure INFORM_SUPERVISOR.

 

Triggers can be used in various applications, such as maintaining database consistency, monitoring database updates, and updating derived data automatically. A more complete discussion is given in Section 26.1.

 

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Fundamentals of Database Systems : The Relational Data Model and SQL : More SQL: Complex Queries, Triggers, Views, and Schema Modification : Specifying Constraints as Assertions and Actions as Triggers |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.