I need to prepare a SQLite trigger for following condition -
Now I want to write a delete trigger such that if a product is deleted from retail_store and if it is not in table wholesale_store, then that product record should be deleted from product table.
** I understand as a practice it may not be a good idea to delete a product record like this. Please take this question only as a technical complication.
Thanks for considering this one. Cheers!
Maybe the following sql statemen is useful for you, but i can't assure the syntax is correct.
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 ;
Sounds like you don't actually need a trigger. I would consider making use of cascading delete. Are you using foreign keys? Check this out:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With