SQLite - Triggers
SQLite - Triggers
htm
Triggers
What is a trigger? In SQL, a trigger is a sql statement or series of sql statements that are executed
automatically in response to a specified event such as the update of or creation or deletion of a table or
record. Each trigger must have a name that is unique to the database. Triggers are deleted when the table
that they are associated with is dropped or they can be deleted with a DROP TRIGGER statement. Once
created, triggers cannot be modified, to make changes the trigger must be dropped and then recreated.
CREATE TRIGGER
This trigger automatically updates the inventory table by subtracting the Quantity of items requisitioned
from the OnHandQuan value when an insert statement adds a record to the ReqDetail table.
CREATE TRIGGER trigger_name BEFORE UPDATE ON child_table FOR EACH ROW BEGIN
SELECT CASE
WHEN ((SELECT parent_table . primary_key FROM parent_table WHERE parent_table . primary_key =
NEW.foreign_key ) ISNULL)
THEN RAISE(ABORT, 'Error Message')
END;
END;
1 de 3 02/02/2014 09:40
SQLite -Triggers http://sqlite.awardspace.info/syntax/sqlitepg11.htm
sqlite>CREATE TRIGGER ReqNumUp BEFORE UPDATE ON ReqDetail FOR EACH ROW BEGIN
...>SELECT CASE
...> WHEN ((SELECT ReqEquip.ReqNumber FROM ReqEquip
...>WHERE ReqEquip.ReqNumber= NEW.ReqNumber) ISNULL)
...> THEN RAISE(ABORT, 'update on table ReqDetail violates foreign key')
...> END;
...>END;
sqlite>
Cascading Delete
Delete records from a child table when a record from the parent table is deleted
CREATE VIEW
A VIEW is a saved SELECT statement that can be used in much the same way as a table. However in
SQLite a view can not be used to add, update or delete the records in the underlying tables.
sqlite> .headers on
sqlite> .mode column
sqlite> .width 10 14 10 10
sqlite> select * from ReqTotal;
However a trigger assigned to a view can be used to insert,update or delete records in underlying tables of
the view.
2 de 3 02/02/2014 09:40
SQLite -Triggers http://sqlite.awardspace.info/syntax/sqlitepg11.htm
3 de 3 02/02/2014 09:40