Article Directory
Overview of triggers.
Trigger is a special type of stored procedure.
main advantage.
- The trigger is automatically activated and executed when an event occurs.
- Triggers can fulfill more complex integrity requirements than constraints.
- The trigger can take corresponding measures according to the state before and after the table data is modified.
- Triggers can prevent malicious or wrong
INSERT
,UPDATE
andDELETE
operations.
species.
- [DML Trigger] is a trigger that is activated and automatically executed when a data manipulation language (DML) event is executed.
- [DDL trigger] is a trigger that is activated and executed automatically when a data definition language (DDL) event is executed.
- [Login Trigger] is a trigger activated by a login (LOGON) event.
working principle.
SQL Server creates two special tables in the server's memory for each trigger during work: insert tables Inserted
and delete tables Deleted
.
- [The working principle of INSERT trigger] When
INSERT
operating on the table , theINSERT
trigger is fired, and the new data row is added to the table and theInserted
table that created the trigger . - [The working principle of the DELETE trigger] When
DELETE
operating on the table , theDELETE
trigger is activated, and the system puts the deleted rows into theDeleted
table from the affected table. - [The working principle of UPDATE trigger] When the
UPDATE
operation is performed, theUPDATE
trigger is activated. The trigger moves the original row into theDeleted
table and inserts the updated row into theInserted
table.
Create a trigger.
Create DML triggers.
CREATE TRIGGER trigger_name
ON {
table | view}
[With Encryption]
{
For | After | Instead Of}
{
[ INSERT ] [ , ] [ UPDATE ] [ , ] [ DELETE] }
AS sql_statement [;]
- Line 1 uses
CREATE TRIGGER
to create a trigger; - The second line indicates whether the trigger is applied to the table
Table
or the viewView
. Note that onlyInstead of
triggers can be created on the view , but notFor|After
triggers; - Line 3 indicates whether to encrypt;
- The
For
sum of the fourth lineAfter
indicates that the trigger action is executed after the operation ofInstead of
the fifth line, and the statement in the fifth line is skipped and the trigger action is directly executed; - The fifth line is the condition that can activate the trigger, and it must contain at least one operation;
- Line 6 is the specific trigger action.
[Example] Design a trigger. When a student is deleted from the student table S, all the student's course selection records in the course selection table SC are also deleted.
USE Teach
GO
CREATE TRIGGER del_S
ON S
AFTER DELETE
AS
DELETE
FROM SC
WHERE SC.SNo IN(SELECT SNo FROM Deleted)
GO
Create DDL triggers.
CREATE TRIGGER trigger_name
ON {
All Server | Database}
[With Encryption]
{
FOR | AFTER} {
event_type | event_group } [,...n ]
AS sql_statement [; ]
- Create trigger on line 1;
- The second line specifies whether the trigger is applied to the current server
All Server
or the current databaseDatabase
. The effect of the former is to make the scope of the trigger be the current server, that isevent_type|event_group
, the trigger will be activated if the specified later appears anywhere in the entire server ; the latter The effect is to make the scope of the current database; - Line 3 indicates whether to encrypt;
- Line 4
For
andAfter
are shown in the fifth line after the operation, the execution of a trigger action, the latter two parameters,event_type
refers to the name of activation of the trigger event T-SQL language, for exampleCREATE_TABLE|DROP_TABLE|ALTER_TABLE
; the latter is predefined The name of the T-SQL language event group,event_group
the trigger will be activated after the execution of the belonging T-SQL language event; - The fifth line is the specific trigger action.
[Example] Create a DDL trigger safety to prohibit modifying and deleting any table in the current database.
USE Teach
GO
CREATE TRIGGER safety
ON DATABASE
FOR DROP_TABLE, ALTER_TABLE
AS PRINT '不能删除或修改数据库表!'
ROLLBACK
GO
Disable triggers.
In some cases, the user wants to suspend the function of the trigger, but does not delete it. At this time, DISABLE TRIGGER
the trigger can be invalidated by a statement. The syntax format is as follows:
DISABLE TRIGGER {
[ schema_name.] trigger_name [ ,...n ] | ALL }
ON object_name
- [Schema_name] The name of the schema to which the trigger belongs;
- [ALL] Specify to disable
ON
all triggers defined in the clause scope; - [Object_name] The object name of the DML trigger created on it.
[Example] safety
Disable and verify the trigger created on the database above .
//A temporary table TS.
SELECT * INTO TS
FROM S
//Try to delete TS,but fail because of trigger 'safety'.
DROP TABLE TS
//Disable 'safety'.
GO
DISABLE TRIGGER safety
ON DATABASE
//Success to delete TS.
DROP TABLE TS
GO
Unban the trigger.
To make the trigger effective again, you can use the ENABLE TRIGGER
statement, the syntax format is as follows:
ENABLE TRIGGER {
[schema_name.] trigger_name[ ,...n ] | ALL }
ON object name
Delete the trigger.
Use the DROP TRIGGER
statement to delete the trigger.
DROP TRIGGER trigger_name