You are on page 1of 2

Using the ON DELETE CASCADE Option Use the ON DELETE CASCADE option to specify whether you want rows

deleted in a child table when corresponding rows are deleted in the parent table. If you do not specify cascading deletes, the default behavior of the database server prevents you from deleting data in a table if other tables reference it. If you specify this option, later when you delete a row in the parent table, the database server also deletes any rows associated with that row (foreign keys) in a child table. The principal advantage to the cascading-deletes feature is that it allows you to reduce the quantity of SQL statements you need to perform delete actions. For example, the all_candy table contains the candy_num column as a primary key. The hard_candy table refers to the candy_num column as a foreign key. The following CREATE TABLE statement creates the hard_candy table with the cascading-delete option on the foreign key:
CREATE TABLE all_candy (candy_num SERIAL PRIMARY KEY, candy_maker CHAR(25)); CREATE TABLE hard_candy (candy_num INT, candy_flavor CHAR(20), FOREIGN KEY (candy_num) REFERENCES all_candy ON DELETE CASCADE)

Because ON DELETE CASCADE is specified for the dependent table, when a row of the all_candy table is deleted, the corresponding rows of the hard_candy table are also deleted. For information about syntax restrictions and locking implications when you delete rows from tables that have cascading deletes, see Considerations When Tables Have Cascading Deletes.

Foreign Key Contraint Notes


Setting up a constraint to prevent deletion of a foreign key
The following is an example demonstrating how to set up a foreign key constraint in PostgreSQL 7.0:
CREATE SEQUENCE school_id_seq; CREATE TABLE school ( school_id INT NOT NULL PRIMARY KEY DEFAULT nextval('school_id_seq'), school_name VARCHAR(80) NOT NULL ); CREATE SEQUENCE student_id_seq; CREATE TABLE student ( student_id INT NOT NULL PRIMARY KEY DEFAULT nextval('student_id_seq'), student_name VARCHAR(80) NOT NULL, school_id INT NOT NULL, CONSTRAINT school_exists FOREIGN KEY(school_id) REFERENCES school ON DELETE RESTRICT ); INSERT INTO school (school_name) VALUES ('Alice''s School of Truck Driving'); INSERT INTO school (school_name) VALUES ('Bob''s School of Underwater Knitting'); INSERT INTO student (student_name, school_id) VALUES ('Charlie', 1); INSERT INTO student (student_name, school_id) VALUES ('Doug', 1);

INSERT INTO student (student_name, school_id) VALUES ('Ernie', 2);

Note the 'ON DELETE RESTRICT' which will prevent you from deleting a school if there is a student going to that school. First look at what's in the tables:
SELECT * FROM school; SELECT * FROM student;

Now attempt to delete the school:


DELETE FROM school WHERE school_id = 1;

If you try the above, you should see 'ERROR: school_exists referential integrity violation - key in school still referenced from student' and notice that nothing was deleted. If you want to try the above more than once, the following may be handy:
DROP DROP DROP DROP SEQUENCE school_id_seq; TABLE school; SEQUENCE student_id_seq; TABLE student;

Cascade & Update


Instead of 'ON DELETE RESTRICT' you could specify 'ON DELETE CASCADE'. This would allow the delete (instead of preventing it like in the example above), but it would 'cascade' the delete to the student table so that any students going to the school you deleted would also be deleted. As well as the 'ON DELETE ...' clause you can also specify what is to happen on an update of the foreign key with either:
ON UPDATE RESTRICT ON UPDATE CASCADE

or

ON UPDATE RESTRICT would prevent UPDATE school SET school_id = 20 WHERE school_id = 1 from proceeding if there were any students with a school_id of 1.

You might be able to guess that ON UPDATE CASCADE would allow the UPDATE school SET school_id = 20 WHERE school_id = 1 to proceed, but it would also update the school_id field in the student table appropriately.
delete restrict
by DEFAULT the "on delete restrict" option is used in Oracle. In other words if you try to delete a parent record, and the primary key of that record is accessed by the foreign key(s) of any children, the application will not allow you to delete that parent record if you do not have any constraints explicitly written . You must write something like ON DELETE CASCADE (or other referential integity constraints) if you wish to delete a parent record refernced by children records.

You might also like