Professional Documents
Culture Documents
Unit 5: Integrity and Security: Dhanashree Huddedar
Unit 5: Integrity and Security: Dhanashree Huddedar
Dhanashree Huddedar
Index
• Domain Constraint
• Referential Integrity
• Assertions
• Triggers and Assertions in SQL
• Security and Authorization in SQL
Overview
Ensuring a Robust System
• To ensure the implemented system will be robust and cater for future
enhancements many other questions need to be addressed, some of
which are:
• How will the final system be audited?
• What security features are needed?
• Can the database handle the user requirements now and in the future?
• How can you ensure the integrity of the data within the database?
Auditing
Rule 2. You can't change any of the model_ids in the CarType table
since all the car types are in use in the Car table.
Rule 3. The values that you can enter in the model_id field in the Car
table must be in the model_id field in the CarType table.
Rule 4. The model_id field in the Car table can have a null value which
means that the car type of that car in not known
Foreign Key Integrity Constraint
24
Triggers: Introduction
Other application constraints are more complex
Need for assertions and triggers
Examples:
Sum of loans taken by a customer does not exceed 100,000
Student cannot take the same course after getting a pass grade in it
Age field is derived automatically from the Date-of-Birth field
25
Triggers
A procedure that runs automatically when a certain
event occurs in the DBMS
26
Trigger Components
Three components
Event: When this event happens, the trigger is activated
Condition (optional): If the condition is true, the trigger
executes, otherwise skipped
Action: The actions performed by the trigger
Semantics
When the Event occurs and Condition is true, execute the
Action
27
Trigger: Events
Three event types
Insert
Update
Delete
Two granularities
Execute for each row
Execute for each statement
28
1) Trigger: Event
Trigger name
Create Trigger <name>
Before|After Insert|Update|Delete ON <tablename> That is the event
….
Example
Create Trigger ABC Create Trigger XYZ
Before Insert On Students After Update On Students
…. ….
29
Granularity of Event
A single SQL statement may update, delete, or insert many records
at the same time
E.g., Update student set gpa = gpa x 0.8;
30
Example: Granularity of Event
31
2) Trigger: Condition
This component is optional
Create Trigger <name>
Before| After Insert| Update| Delete On <tableName>
For Each Row | For Each Statement
If the employee salary > 150,000 then some actions will be taken
33
Trigger: Referencing Values
In the action, you may want to reference:
The new values of inserted or updated records (:new)
The old values of deleted or updated records (:old)
34
Trigger: Referencing Values (Cont’d)
Insert Event
Has only :new defined
Delete Event
Has only :old defined
Update Event
Has both :new and :old defined
After triggering
Should not change :new because the event is already done
35
Example 1
If the employee salary increased by more than 10%, make sure the ‘rank’
field is not empty and its value has changed, otherwise reject the update
36
Example 2
If the employee salary increased by more than 10%, then increment the rank field
by 1.
37