Assertion and Triggers
Assertion and Triggers
Domain Constraints
Referential Integrity
Assertions
Triggers
Security
Authorization
Authorization in SQL
Domain Constraints
Ensures that a value that appears in one relation for a given set of
attributes also appears for a certain set of attributes in another
relation.
Example: If “Perryridge” is a branch name appearing in one of the
tuples in the account relation, then there exists a tuple in the branch
relation for branch “Perryridge”.
Formal Definition
Let r1(R1) and r2(R2) be relations with primary keys K1 and K2
respectively.
The subset α of R2 is a foreign key referencing K1 in relation r1, if for
every t2 in r2 there must be a tuple t1 in r1 such that t1[K1] = t2[α].
Referential integrity constraint also called subset dependency since its
can be written as
∏α (r2) ⊆ ∏K1 (r1)
Checking Referential Integrity on
Database Modification
The following tests must be made in order to preserve the
following referential integrity constraint:
∏α (r2) ⊆ ∏K (r1)
Insert. If a tuple t2 is inserted into r2, the system must ensure
that there is a tuple t1 in r1 such that t1[K] = t2[α]. That is
t2 [α] ∈ ∏K (r1)
Delete. If a tuple, t1 is deleted from r1, the system must
compute the set of tuples in r2 that reference t1:
σα = t1[K] (r2)
If this set is not empty
either the delete command is rejected as an error, or
the tuples that reference t1 must themselves be deleted
(cascading deletions are possible).
Database Modification (Cont.)
Update. There are two cases:
If a tuple t2 is updated in relation r2 and the update modifies values for
foreign key α, then a test similar to the insert case is made:
Let t2’ denote the new value of tuple t2. The system must ensure
that
t2’[α] ∈ ∏K(r1)
If a tuple t1 is updated in r1, and the update modifies values for the
primary key (K), then a test similar to the delete case is made:
1. The system must compute
σα = t1[K] (r2)
using the old value of t1 (the value before the update is applied).
2. If this set is not empty
Primary and candidate keys and foreign keys can be specified as part of
the SQL create table statement:
The primary key clause lists attributes that comprise the primary key.
The unique key clause lists attributes that comprise a candidate key.
The foreign key clause lists the attributes that comprise the foreign key and
the name of the relation referenced by the foreign key.
By default, a foreign key references the primary key attributes of the
referenced table
foreign key (account-number) references account
Short form for specifying a single column as foreign key
account-number char (10) references account
Reference columns in the referenced table can be explicitly specified
but must be declared as primary/candidate keys
foreign key (account-number) references account(account-number)
Referential Integrity in SQL – Example
Alternative to cascading:
on delete set null
on delete set default
Null values in foreign key attributes complicate SQL referential
integrity semantics, and are best prevented using not null
if any attribute of a foreign key is null, the tuple is defined to satisfy
the foreign key constraint!
Assertions
The sum of all loan amounts for each branch must be less than
the sum of all account balances at the branch.
create assertion sum-constraint check
(not exists (select * from branch
where (select sum(amount) from loan
where loan.branch-name =
branch.branch-name)
>= (select sum(amount) from account
where loan.branch-name =
branch.branch-name)))
Assertion Example
Every loan has at least one borrower who maintains an account with
a minimum balance or $1000.00
create assertion balance-constraint check
(not exists (
select * from loan
where not exists (
select *
from borrower, depositor, account
where loan.loan-number = borrower.loan-number
and borrower.customer-name = depositor.customer-name
and depositor.account-number = account.account-number
and account.balance >= 1000)))
Triggers
Server Errors
Users Log On or Off
Database Started or Stopped
Trigger DML Events
Physical level
Physical access to computers allows destruction of data by
intruders; traditional lock-and-key security is needed
Computers must also be protected from floods, fire, etc.
Human level
Users must be screened to ensure that an authorized users do
not give access to intruders
Users should be trained on password selection and secrecy
Authorization
DBA U2 U5
U3
Authorization Grant Graph
Each site must ensure that it communicate with trusted sites (not
intruders).
Links must be protected from theft or modification of messages
Mechanisms:
Identification protocol (password-based),
Cryptography.
Database-Level Security