sql - 如果外键的列设置为 false,Postgres 删除一行
问题描述
我有这样的表1:
模板表:
ID int
Name string
TemplateGroupID int
模板组表:
ID int
Name string
IsDefault bool
我想删除模板中的一行,如果该行的 FK 到 template_group 列IsDefault
设置为 true。我怎样才能做到这一点?我在想它是这样的:
delete from template
where id =33 and exists (
select * from template_group where is_default = true
);
但我认为这是不正确的,因为我不必在子选择TemplateGroupId
中选择?
解决方案
你可以使用Postgres 的delete ... from ... using ... where ...
语法:
delete
from template t
using template_group g
where g.ID = t.TemplateGroupID and g.IsDefault = true
推荐阅读
- here-api - “这些凭据不授权访问”(403)
- pyspark - Pyspark,如何使用udf计算泊松分布?
- php - Google 访问令牌不会在离线访问时自动刷新
- python - docker:来自守护进程的错误响应:OCI 运行时创建失败:没有这样的文件或目录”:未知
- build - Fortran 新手:如何在 Geany 中编译/构建多个 Fortran 文件
- python - 无法在 django 中编写动态元标记
- java - Apache POI - 饼图 - java.lang.IllegalStateException:类别和值必须具有相同的点数
- python - 无法将“版本”解析为有效参考。运行 git 标签时
- python - 如何将带注释的查询限制为 Django ORM 中的单个列?
- swift - 抛出自定义错误时如何回滚事务