Mukesh Kumar
Mukesh Kumar

Reputation: 985

How can use the cascade in Postgresql query while deleting record from parent table

How can we use the cascade in PostgreSQL while deleting the one record from the parent table that is being referred in other child tables. Currently it is giving the syntax error.

ERROR:  syntax error at or near "cascade"
LINE 1: DELETE FROM fs_item where itemid = 700001803 cascade;

Upvotes: 7

Views: 12226

Answers (2)

Akash KC
Akash KC

Reputation: 16310

You have to add ON DELETE CASCADE constraint in following way:

ALTER TABLE table1 ADD CONSTRAINT "tbl1_tbl2_fkey" FOREIGN KEY(reference_key) REFERENCES table2 ON DELETE CASCADE;

Then, you can simply execute the DELETE query

 DELETE FROM fs_item where itemid = 700001803

Upvotes: 7

Richard Huxton
Richard Huxton

Reputation: 22893

There is no CASCADE for delete statements. You set the foreign key to CASCADE deletes and then it happens for you automatically.

Upvotes: 2

Related Questions