Structured Query Language (SQL) - Cultural View of Technology
Structured Query Language (SQL) - Cultural View of Technology
Structured Query Language (SQL) - Cultural View of Technology
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Foreign key 55<br />
SET NULL<br />
The foreign key values in the referencing row are set to NULL when the referenced row is updated or deleted. This is<br />
only possible if the respective columns in the referencing table are nullable. Due to the semantics <strong>of</strong> NULL, a<br />
referencing row with NULLs in the foreign key columns does not require a referenced row.<br />
SET DEFAULT<br />
Similarly to SET NULL, the foreign key values in the referencing row are set to the column default when the<br />
referenced row is updated or deleted.<br />
Triggers<br />
Referential actions are generally implemented as implied triggers (i.e. triggers with system-generated names, <strong>of</strong>ten<br />
hidden.) As such, they are subject to the same limitations as user-defined triggers, and their order <strong>of</strong> execution<br />
relative to other triggers may need to be considered; in some cases it may become necessary to replace the referential<br />
action with its equivalent user-defined trigger to ensure proper execution order, or to work around mutating-table<br />
limitations.<br />
Another important limitation appears with transaction isolation: your changes to a row may not be able to fully<br />
cascade because the row is referenced by data your transaction cannot "see", and therefore cannot cascade onto. An<br />
example: while your transaction is attempting to renumber a customer account, a simultaneous transaction is<br />
attempting to create a new invoice for that same customer; while a CASCADE rule may fix all the invoice rows your<br />
transaction can see to keep them consistent with the renumbered customer row, it won't reach into another<br />
transaction to fix the data there; because the database cannot guarantee consistent data when the two transactions<br />
commit, one <strong>of</strong> them will be forced to rollback (<strong>of</strong>ten on a first-come-first-served basis.)<br />
Example<br />
As a first example to illustrate foreign keys, suppose an accounts database has a table with invoices and each invoice<br />
is associated with a particular supplier. Supplier details (such as address or phone number) are kept in a separate<br />
table; each supplier is given a 'supplier number' to identify it. Each invoice record has an attribute containing the<br />
supplier number for that invoice. Then, the 'supplier number' is the primary key in the Supplier table. The foreign<br />
key in the Invoices table points to that primary key. The relational schema is the following. Primary keys are marked<br />
in bold, and foreign keys are marked in italics.<br />
Supplier ( SupplierNumber, Name, Address, Type )<br />
Invoices ( InvoiceNumber, SupplierNumber, Text )<br />
The corresponding Data Definition <strong>Language</strong> statement is as follows.<br />
CREATE TABLE Supplier (<br />
SupplierNumber INTEGER NOT NULL,<br />
Name VARCHAR(20) NOT NULL,<br />
Address VARCHAR(50) NOT NULL,<br />
Type VARCHAR(10),<br />
CONSTRAINT supplier_pk PRIMARY KEY(SupplierNumber),<br />
CONSTRAINT number_value CHECK (SupplierNumber > 0) )<br />
CREATE TABLE Invoices (<br />
InvoiceNumber INTEGER NOT NULL,<br />
SupplierNumber INTEGER NOT NULL,