[Database Principle] Trigger

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, UPDATEand DELETEoperations.

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 Insertedand delete tables Deleted.
Insert picture description here

  • [The working principle of INSERT trigger] When INSERToperating on the table , the INSERTtrigger is fired, and the new data row is added to the table and the Insertedtable that created the trigger .
  • [The working principle of the DELETE trigger] When DELETEoperating on the table , the DELETEtrigger is activated, and the system puts the deleted rows into the Deletedtable from the affected table.
  • [The working principle of UPDATE trigger] When the UPDATEoperation is performed, the UPDATEtrigger is activated. The trigger moves the original row into the Deletedtable and inserts the updated row into the Insertedtable.

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 TRIGGERto create a trigger;
  • The second line indicates whether the trigger is applied to the table Tableor the view View. Note that only Instead oftriggers can be created on the view , but not For|Aftertriggers;
  • Line 3 indicates whether to encrypt;
  • The Forsum of the fourth line Afterindicates that the trigger action is executed after the operation of Instead ofthe 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 Serveror the current database Database. The effect of the former is to make the scope of the trigger be the current server, that is event_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 Forand Afterare shown in the fifth line after the operation, the execution of a trigger action, the latter two parameters, event_typerefers to the name of activation of the trigger event T-SQL language, for example CREATE_TABLE|DROP_TABLE|ALTER_TABLE; the latter is predefined The name of the T-SQL language event group, event_groupthe 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 TRIGGERthe 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 ONall triggers defined in the clause scope;
  • [Object_name] The object name of the DML trigger created on it.

[Example] safetyDisable 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 TRIGGERstatement, the syntax format is as follows:

ENABLE TRIGGER {
    
    [schema_name.] trigger_name[ ,...n ] | ALL }
ON object name

Delete the trigger.

Use the DROP TRIGGERstatement to delete the trigger.

DROP TRIGGER trigger_name

Guess you like

Origin blog.csdn.net/weixin_44246009/article/details/108135489