【发布时间】:2022-01-06 23:08:32
【问题描述】:
在 Postgres 中,我有两个表需要使用级联删除来清除一些数据:
- PARTICIPANTS_T: has a foreign key USER_ID on USERS_T.ID
- USERS_T
第一步是从满足条件的 PARTICIPANTS_T 中删除,例如
delete from PARTICIPANTS_T where VALID_FLAG = 'Y';
第二个是从USERS_T中删除之前删除中引用了ID的行。
delete from USERS_T where ID = [..from Step 1..]
如何在 Postgres 中进行级联删除?
我考虑过保存在一个变量中:select INTO,
if exists drop table user_ids; --This syntax is wrong
select id into user_ids from users_t where id in (
select user_id from participants_t where valid_flag = 'Y');
但这很不方便,因为user_ids 变成了需要维护的单独表。我上面的语法不正确。任何人都可以通过这 2 个步骤给出一个完整的可重复脚本吗?
【问题讨论】:
-
你可以在删除时使用级联!!!在表 1 上
标签: sql postgresql