How to use cascade in Postgresql query when deleting record from parent table

How can we use cascade in PostgreSQL when deleting one record from the parent table, which is passed in other child tables. It is currently giving a syntax error.

ERROR: syntax error at or near "cascade" LINE 1: DELETE FROM fs_item where itemid = 700001803 cascade; 
+4
source share
2 answers

You must add the ON DELETE CASCADE constraint as follows:

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

Then you can just execute the DELETE query

  DELETE FROM fs_item where itemid = 700001803 
+5
source

There is no CASCADE for delete statements. You install a foreign key to remove CASCADE, and then it happens for you automatically.

+2
source

Source: https://habr.com/ru/post/1434150/


All Articles