sql - 多个表的 SQL 删除触发器
问题描述
所以我有两个表,id_pastille
我想创建一个触发器,每次id_pastille
删除一个触发器时,每个具有该表的表id_pastille
也会被删除。
CREATE TABLE tp1_trajet
(
id_trajet int NOT NULL,
longitude double precision NOT NULL,
latitude double precision NOT NULL,
"date" timestamp NOT NULL,
id_pastille int NOT NULL,
CONSTRAINT PK_zone_geographique PRIMARY KEY ( id_trajet ),
CONSTRAINT FK_ FOREIGN KEY ( id_pastille ) REFERENCES tp1_pastille ( id_pastille )
让我们说这张桌子
CREATE TABLE tp1_compte
(
id_compte int NOT NULL,
date_achat date NOT NULL,
id_pastille int NOT NULL,
id_usager int NOT NULL,
probabilite_malade double precision NULL,
CONSTRAINT PK_compte PRIMARY KEY ( id_compte ),
CONSTRAINT UQ_compte_id_pastille UNIQUE ( id_pastille ),
CONSTRAINT UQ_compte_id_usager UNIQUE ( id_usager ),
CONSTRAINT FK_compte_id_pastille FOREIGN KEY ( id_pastille ) REFERENCES tp1_pastille ( id_pastille ),
CONSTRAINT FK_compte_id_usager FOREIGN KEY ( id_usager ) REFERENCES tp1_usager ( id_usager )
);
到目前为止,我想出了这个:
CREATE FUNCTION Delete_pastille() RETURNS trigger
AS $DeleteCustomerWithOrders$
BEGIN
DELETE FROM tp1_bulle,
tp1_compte,
tp1_pastille,
tp1_signes_vitaux,
tp1_usager,
tp1_transaction,
tp1_trajet
WHERE id_pastille = OLD."Id";
RETURN OLD;
END;
$DeleteCustomerWithOrders$
LANGUAGE plpgsql;
解决方案
您只能从DELETE
语句中的单个表中删除。尝试使用几个语句。
最好的解决方案可能是在指向包含原始副本 ( pastille
?) 的表的所有列上定义外键约束,但其中一个列除外。如果外键是用 定义的ON DELETE CASCADE
,删除原来的会自动删除所有依赖的行。
推荐阅读
- c - 如何在 C 中限制值用户输入 < INT_MAX
- angular - FirebaseError:消息:我们无法注册默认的服务工作者,服务工作者已经添加
- python-3.x - 在 DRF 中使用 elasticsearch 的正确方法
- css - 获取两个 i 标签之间信息的 CSS 选择器或 XPath?
- javascript - 是否可以生成似乎由 js 文件生成的 cookie - 抓取问题
- windows - Windows 10 服务启动错误2(系统找不到指定路径)
- python - 是否可以在熊猫数据框中进行全文搜索
- php - 在 PHP 中更改引用计数时触发事件
- java - 在旧 EJB 配置位于 ejb-jar.xml 的项目中使用 ejb 注释和 CDI
- ajax - Bootstrap 数据表分页和排序