Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

postgresql

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;
like image 542
Mukesh Kumar Avatar asked Sep 14 '12 08:09

Mukesh Kumar


People also ask

How does cascade delete work?

A foreign key with cascade delete means that if a record in the parent table is deleted, then the corresponding records in the child table will automatically be deleted. This is called a cascade delete in SQL Server.

How do you delete a record from a table in Postgres?

First, specify the table from which you want to delete data in the DELETE FROM clause. Second, specify which rows to delete by using the condition in the WHERE clause. The WHERE clause is optional. However, if you omit it, the DELETE statement will delete all rows in the table.

When to use cascading delete?

Cascading deletes are needed when a dependent/child entity can no longer be associated with its current principal/parent. This can happen because the principal/parent is deleted, or it can happen when the principal/parent still exists but the dependent/child is no longer associated with it.

How can we use cascade in PostgreSQL while deleting one record?

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. You have to add ON DELETE CASCADE constraint in following way: There is no CASCADE for delete statements.

How to delete all the Child Records in PostgreSQL?

As shown above, the DELETE CASCADE can be used in PostgreSQL to delete all the child records whenever the referenced parent record is deleted automatically which helps in maintaining integrity. This is a guide to Postgres Delete Cascade.

What is MySQL on delete Cascade?

However, MySQL provides a more effective way called ON DELETE CASCADE referential action for a foreign key that allows you to delete data from child tables automatically when you delete the data from the parent table. MySQL ON DELETE CASCADE example. Let’s take a look at an example of using MySQL ON DELETE CASCADE.

How do I delete a table in PostgreSQL without losing data?

Before running a delete query, it is important to ensure that the table does not leave any incomplete relationships between tables. In PostgreSQL, we can use the CASCADE DELETE feature to accomplish this. This feature allows a delete operation to remove the specified records and any foreign keys that reference them.


2 Answers

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
like image 176
Akash KC Avatar answered Sep 27 '22 21:09

Akash KC


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

like image 43
Richard Huxton Avatar answered Sep 27 '22 22:09

Richard Huxton