Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL delete from parent table only

I have a table structure with partitioned tables, where a few child tables inherit from a common parent. How so I DELETE only from the parent table?

Long story short, I ended up with some data in the parent table, and this should've never happened but now I have to clean up the mess.

like image 937
siki Avatar asked Feb 25 '14 20:02

siki


People also ask

Can we delete parent table without deleting child table?

We cannot delete a parent table that is referenced by a foreign key constraint. We need to either remove the foreign key relationship or drop the child table first. In my example, we need to drop the Employee1 table first because it has a foreign key relationship with the department table.

How do you delete a record from parent table?

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 I delete a specific table in PostgreSQL?

To empty a table of rows without destroying the table, use DELETE or TRUNCATE . DROP TABLE always removes any indexes, rules, triggers, and constraints that exist for the target table. However, to drop a table that is referenced by a view or a foreign-key constraint of another table, CASCADE must be specified.


1 Answers

You can specify that only parent table matters, simply by using keyword 'ONLY':

DELETE FROM ONLY parent_table_name;

See: http://www.postgresql.org/docs/current/static/sql-delete.html

like image 130
Tomasz Siorek Avatar answered Oct 07 '22 15:10

Tomasz Siorek