Home:ALL Converter>How can use the cascade in Postgresql query while deleting record from parent table

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

Ask Time:2012-09-14T16:45:31         Author:Mukesh Kumar

Json Formatter

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;

Author:Mukesh Kumar,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/12420973/how-can-use-the-cascade-in-postgresql-query-while-deleting-record-from-parent-ta
Akash KC :

You have to add ON DELETE CASCADE constraint in following way:\n\nALTER TABLE table1 ADD CONSTRAINT \"tbl1_tbl2_fkey\" FOREIGN KEY(reference_key) REFERENCES table2 ON DELETE CASCADE;\n\n\nThen, you can simply execute the DELETE query\n\n DELETE FROM fs_item where itemid = 700001803\n",
2012-09-14T09:00:23
yy