从父表中删除记录时如何在 Postgresql 查询中使用级联 [英] How can use the cascade in Postgresql query while deleting record from parent table

查看:62
本文介绍了从父表中删除记录时如何在 Postgresql 查询中使用级联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何在 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;

推荐答案

您必须通过以下方式添加 ON DELETE CASCADE 约束:

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;

然后,您可以简单地执行 DELETE 查询

Then, you can simply execute the DELETE query

 DELETE FROM fs_item where itemid = 700001803

这篇关于从父表中删除记录时如何在 Postgresql 查询中使用级联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆