23.10.2012 Views

Structured Query Language (SQL) - Cultural View of Technology

Structured Query Language (SQL) - Cultural View of Technology

Structured Query Language (SQL) - Cultural View of Technology

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Foreign key 54<br />

CONSTRAINT col3_fk FOREIGN KEY(col3)<br />

... )<br />

REFERENCES other_table(key_col) ON DELETE CASCADE,<br />

If the foreign key is a single column only, the column can be marked as such using the following syntax:<br />

CREATE TABLE table_name (<br />

id INTEGER PRIMARY KEY,<br />

col2 CHARACTER VARYING(20),<br />

col3 INTEGER REFERENCES other_table(column_name),<br />

... )<br />

Referential Actions<br />

Because the Database Management System enforces referential constraints, it must ensure data integrity if rows in a<br />

referenced table are to be deleted (or updated). If dependent rows in referencing tables still exist, those references<br />

have to be considered. <strong>SQL</strong>:2003 specifies 5 different referential actions that shall take place in such occurrences:<br />

• CASCADE<br />

• RESTRICT<br />

• NO ACTION<br />

• SET NULL<br />

• SET DEFAULT<br />

CASCADE<br />

Whenever rows in the master (referenced) table are deleted, the respective rows <strong>of</strong> the child (referencing) table with<br />

a matching foreign key column will get deleted as well. This is called a cascade delete.<br />

Example Tables: Customer(customer_id, cname, caddress) and Order(customer_id, products, payment)<br />

Customer is the master table and Order is the child table, where 'customer_id' is the foreign key in Order and<br />

represents the customer who placed the order. When a row <strong>of</strong> Customer is deleted, any Order row matching the<br />

deleted Customer's customer_id will also be deleted.<br />

RESTRICT<br />

A value cannot be updated or deleted when a row exists in a foreign key table that references the value in the<br />

referenced table.<br />

Similarly, a row cannot be deleted as long as there is a reference to it from a foreign key table.<br />

NO ACTION<br />

NO ACTION and RESTRICT are very much alike. The main difference between NO ACTION and RESTRICT is<br />

that with NO ACTION the referential integrity check is done after trying to alter the table. RESTRICT does the<br />

check before trying to execute the UPDATE or DELETE statement. Both referential actions act the same if the<br />

referential integrity check fails: the UPDATE or DELETE statement will result in an error.<br />

In other words, when an UPDATE or DELETE statement is executed on the referenced table using the referential<br />

action NO ACTION, the DBMS verifies at the end <strong>of</strong> the statement execution that none <strong>of</strong> the referential<br />

relationships are violated. This is different from RESTRICT, which assumes at the outset that the operation will<br />

violate the constraint. Using NO ACTION, the triggers or the semantics <strong>of</strong> the statement itself may yield an end state<br />

in which no foreign key relationships are violated by the time the constraint is finally checked, thus allowing the<br />

statement to complete successfully.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!