如何在sqlite3中创建触发器?

时间:2022-01-15 04:14:25

i am new to this topic. i want something like this.

我是这个话题的新手。我想要这样的东西。

i have two tables in my sqliteDatabase one is master and another is child. Now if i delete a record from master suppose i delete a row from master where id=5, then all the records from the child table whose id = 5 deleted automatically. I don't know how to create triggers and how to apply foreign key constraints so someone please tell me a way to do this in sqlite3 Manager of firefox. Thanks

我的sqliteDatabase中有两个表,一个是master,另一个是child。现在,如果我从master中删除一条记录,假设我从master中删除了一行,其中id = 5,则自动删除id = 5的子表中的所有记录。我不知道如何创建触发器以及如何应用外键约束,所以有人请告诉我在firefox的sqlite3 Manager中执行此操作的方法。谢谢

2 个解决方案

#1


3  

You don't need a trigger for that, your foreign key will do that if you define ON DELETE CASCADE:

您不需要触发器,如果​​您定义ON DELETE CASCADE,您的外键将执行此操作:

CREATE TABLE child(
  id         INTEGER,
  some_info  TEXT, 
  master_id  INTEGER,
  FOREIGN KEY(master_id) REFERENCES master(id) ON DELETE CASCADE
);

See documentation about foreign keys.

请参阅有关外键的文档。

EDIT:

If you really need to do it using a trigger, have a look at Foreing Key Triggers.

如果你真的需要使用触发器,请查看Foreing Key Triggers。

#2


0  

This page should help: http://justatheory.com/computers/databases/sqlite/foreign_key_triggers.html

此页面应该有所帮助:http://justatheory.com/computers/databases/sqlite/foreign_key_triggers.html

This is a general trigger reference for SQLite

这是SQLite的一般触发器参考

#1


3  

You don't need a trigger for that, your foreign key will do that if you define ON DELETE CASCADE:

您不需要触发器,如果​​您定义ON DELETE CASCADE,您的外键将执行此操作:

CREATE TABLE child(
  id         INTEGER,
  some_info  TEXT, 
  master_id  INTEGER,
  FOREIGN KEY(master_id) REFERENCES master(id) ON DELETE CASCADE
);

See documentation about foreign keys.

请参阅有关外键的文档。

EDIT:

If you really need to do it using a trigger, have a look at Foreing Key Triggers.

如果你真的需要使用触发器,请查看Foreing Key Triggers。

#2


0  

This page should help: http://justatheory.com/computers/databases/sqlite/foreign_key_triggers.html

此页面应该有所帮助:http://justatheory.com/computers/databases/sqlite/foreign_key_triggers.html

This is a general trigger reference for SQLite

这是SQLite的一般触发器参考