English 中文(简体)
SQLite 中删除触发器后有条件执行
原标题:Conditional execution after a delete trigger in SQLite
  • 时间:2012-05-24 16:17:18
  •  标签:
  • sqlite

我需要准备一个 SQLite 触发器 对于以下条件 -

  1. There are three tables - retail_store, wholesale_store and product
  2. Tables retail_store and wholesale_store have column product_id from table product

现在我想写一个删除触发器, 这样如果一个产品被从零售商店删除, 如果它不在批发商店桌上, 那么产品记录应该从产品表格中删除。

** 我的理解是,作为惯例,删除这样一份产品记录可能不是一个好主意,请将此问题视为技术复杂问题。

谢谢你考虑这个,干杯!

最佳回答

也许以下的sql州议员对你有用 但我不能保证语法是正确的

 CREATE TRIGGER after_retail_store_delete after delete ON retail_store
    WHEN ((select count() from  wholesale_store where productid = OLD.id) = 0)
    BEGIN
      DELETE FROM product WHERE productid = OLD.id ;
    END ;
问题回答

听起来你其实不需要触发器。 我会考虑使用串联删除。 您是否使用外国密钥? 请看 :

The ON DELETE and ON UPDATE action associated with each foreign key in an SQLite database is one of "NO ACTION", "RESTRICT", "SET NULL", "SET DEFAULT" or "CASCADE". If an action is not explicitly specified, it defaults to "NO ACTION".

NO ACTION: Configuring "NO ACTION" means just that: when a parent key is modified or deleted from the database, no special action is taken.

RESTRICT: The "RESTRICT" action means that the application is prohibited from deleting (for ON DELETE RESTRICT) or modifying (for ON UPDATE RESTRICT) a parent key when there exists one or more child keys mapped to it. The difference between the effect of a RESTRICT action and normal foreign key constraint enforcement is that the RESTRICT action processing happens as soon as the field is updated - not at the end of the current statement as it would with an immediate constraint, or at the end of the current transaction as it would with a deferred constraint. Even if the foreign key constraint it is attached to is deferred, configuring a RESTRICT action causes SQLite to return an error immediately if a parent key with dependent child keys is deleted or modified.

SET NULL: If the configured action is "SET NULL", then when a parent key is deleted (for ON DELETE SET NULL) or modified (for ON UPDATE SET NULL), the child key columns of all rows in the child table that mapped to the parent key are set to contain SQL NULL values.

SET DEFAULT: The "SET DEFAULT" actions are similar to "SET NULL", except that each of the child key columns is set to contain the columns default value instead of NULL. Refer to the CREATE TABLE documentation for details on how default values are assigned to table columns.

CASCADE: A "CASCADE" action propagates the delete or update operation on the parent key to each dependent child key. For an "ON DELETE CASCADE" action, this means that each row in the child table that was associated with the deleted parent row is also deleted. For an "ON UPDATE CASCADE" action, it means that the values stored in each dependent child key are modified to match the new parent key values.

Read more here: http://www.sqlite.org/foreignkeys.html#fk_actions

https://www.sqlite.org/foreignkeys.html#fk_enable' rel=“不跟随 noreferrer” >Sqllite Foreing Key 使用 SQL 命令:

PRAGMA foreign_keys = ON;




相关问题
sqlite3 is chopping/cutting/truncating my text columns

I have values being cut off and would like to display the full values. Sqlite3 -column -header locations.dbs " select n.namelist, f.state, t.state from names n left join locations l on l.id = n.id ...

Entity Framework with File-Based Database

I am in the process of developing a desktop application that needs a database. The application is currently targeted to SQL Express 2005 and works wonderfully. However, I m not crazy about having ...

Improve INSERT-per-second performance of SQLite

Optimizing SQLite is tricky. Bulk-insert performance of a C application can vary from 85 inserts per second to over 96,000 inserts per second! Background: We are using SQLite as part of a desktop ...

Metadata for columns in SQLite v2.8 (PHP5)

How can I get metadata / constraints (primary key and "null allowed" in particular) for each column in a SQLite v2.8 table using PHP5 (like mysql_fetch_field for MySql)? sqlite_fetch_column_types (OO:...

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签