1. An addition to the original standard allows specification of primary and candidate keys and foreign keys as part of the create table command:
- primary key clause includes a list of attributes forming the primary key.
- unique key clause includes a list of attributes forming a candidate key.
- foreign key clause includes a list of attributes forming the foreign key, and the name of the relation
referenced by the foreign key.
2. An example illustrates several features mentioned so far:
create table customer
(cname char(20) not null,
street char(30),
city char(30),
primary key (cname))
create table branch
(bname char(15) not null,
bcity char(30),
assets integer,
primary key (bname)
check (assets >= 0))
create table account
(account# char(10) not null,
(bname char(15),
balance integer,
primary key (account#)
foreign key (bname) references branch,
check (balance >= 0))
create table depositor
(cname char(20) not null,
account# char(10) not null,
primary key (cname, account#)
foreign key (cname) references customer,
foreign key (account#) references account)
3. Notes on foreign keys:
- A short form to declare a single column is a foreign key.bname
char(15) references branch
- When a referential integrity constraint is violated, the normal procedure is to reject the action. But a foreign key clause in SQL-92 can specify steps to be taken to change the tuples in the referenced relation to restore the constraint.
- Example.
- create table account
: : :
foreign key (bname) references branch
on delete cascade
on insert cascade,
: : :
If a delete of a tuple in branch results in the preceding referential integrity constraints being violated, the delete is not rejected, but the delete \cascade" to the account relation, deleting the tuple that refers to the branch that was deleted. Update will be cascaded to the new value of the branch!
- SQL-92 also allows the foreign key clause to specify actions other than cascade, such as setting the refencing eld to null, or to a default value, if the constraint is violated.
- If there is a chain of foreign key dependencies across multiple relations, a deletion or update at one end of the chain can propagate across the entire chain.
If a cascading update or delete causes a constraint violation that cannot be handled by a further
cascading operation, the system aborts the transaction and all the changes caused by the transaction
and its cascading actions are undone.
4. Given and complexity and arbitrary nature of the way constraints in SQL behave with null values, it is the best to ensure that all columns of unique and foreign key speci cations are declared to be nonnull.
