SQL筆記--(7)--[觸發(fā)器]

Classification

  • DML(Data Manipulation Language) Trigger :
  • 當(dāng)用戶通過DML事件(對(duì)表或視圖的insert, update, delete)修改數(shù)據(jù)時(shí)被觸發(fā)。
  • 任意合法DML事件發(fā)生都將觸發(fā)DML觸發(fā)器,無論有無表的數(shù)據(jù)行受影響
  • DDL(Data Defination Language)Trigger :
  • 當(dāng)DDL事件(對(duì)表的create, alter, drop)發(fā)生時(shí)被觸發(fā)
  • Logon Trigger :
  • 當(dāng)LOGON事件(建立用戶對(duì)話)時(shí)被觸發(fā)

Syntax

  • DML Trigger
-- SQL Server Syntax
-- Trigger on an INSERT, UPDATE, or DELETE statement to a table or view (DML Trigger)
CREATE [ OR ALTER ] TRIGGER [ schema_name . ]trigger_name
ON { table | view }
[ WITH <dml_trigger_option> [ ,...n ] ]
{ FOR | AFTER | INSTEAD OF }
{ [ INSERT ] [ , ] [ UPDATE ] [ , ] [ DELETE ] }
[ WITH APPEND ]
[ NOT FOR REPLICATION ]
AS { sql_statement [ ; ] [ ,...n ] | EXTERNAL NAME <method specifier [ ; ] > }
<dml_trigger_option> ::=
[ ENCRYPTION ]
[ EXECUTE AS Clause ]
<method_specifier> ::=
assembly_name.class_name.method_name

+ 參數(shù)含義

Arguments Meaning 備注
WITH ENCRYPTION encryption(加密). Obfuscates the text of the CREATE TRIGGER statement. Using WITH ENCRYPTION prevents the trigger from being published as part of SQL Server replication. WITH ENCRYPTION cannot be specified for CLR triggers.
EXECUTE AS Required for triggers on memory-optimized tables. Enables you to control which user account the instance of SQL Server uses to validate permissions on any database objects that are referenced by the trigger.
FOR \ AFTER AFTER指明只有在聲明的所有SQL語句完成后此觸發(fā)器才被觸發(fā)。所有參考級(jí)聯(lián)動(dòng)作和約束性檢查都必須成功,才能觸發(fā)此DML觸發(fā)器。 AFTER不能被聲明在視圖上。AFTER is the default when FOR is the only keyword specified.
INSTEAD OF 指明此DML觸發(fā)器將代替觸發(fā)語句(Triggering SQL Statements)執(zhí)行,即重寫(Overriding)。此參數(shù)不能指明在DDL或logon觸發(fā)器中。 每一條表上或視圖上的insert, update, delete語句至多只能有一個(gè)INSTEAD OF觸發(fā)器
{ [ DELETE ] [ , ] [ INSERT ] [ , ] [ UPDATE ] } 指明觸發(fā)此觸發(fā)器的SQL語句類型,可以為三者的任意組合 For INSTEAD OF triggers, the DELETE option is not allowed on tables that have a referential relationship specifying a cascade action ON DELETE. Similarly, the UPDATE option is not allowed on tables that have a referential relationship specifying a cascade action ON UPDATE.
[ WITH APPEND ]
[ NOT FOR REPLICATION ] Indicates that the trigger should not be executed when a replication agent modifies the table that is involved in the trigger.
-- SQL Server Syntax
-- Trigger on an INSERT, UPDATE, or DELETE statement to a table (DML Trigger on memory-optimized tables)
CREATE [ OR ALTER ] TRIGGER [ schema_name . ]trigger_name
ON { table }
[ WITH <dml_trigger_option> [ ,...n ] ]
{ FOR | AFTER }
{ [ INSERT ] [ , ] [ UPDATE ] [ , ] [ DELETE ] }
AS { sql_statement [ ; ] [ ,...n ] }
<dml_trigger_option> ::=
[ NATIVE_COMPILATION ]
[ SCHEMABINDING ]
[ EXECUTE AS Clause ]
  • DDL Trigger
-- Trigger on a CREATE, ALTER, DROP, GRANT, DENY, REVOKE or UPDATE statement (DDL Trigger)
CREATE [ OR ALTER ] TRIGGER trigger_name
ON { ALL SERVER | DATABASE }
[ WITH <ddl_trigger_option> [ ,...n ] ]
{ FOR | AFTER } { event_type | event_group } [ ,...n ]
AS { sql_statement [ ; ] [ ,...n ] | EXTERNAL NAME < method specifier > [ ; ] }
<ddl_trigger_option> ::=
[ ENCRYPTION ]
[ EXECUTE AS Clause ]
  • Logon Trigger
-- Trigger on a LOGON event (Logon Trigger)
CREATE [ OR ALTER ] TRIGGER trigger_name
ON ALL SERVER
[ WITH <logon_trigger_option> [ ,...n ] ]
{ FOR| AFTER } LOGON
AS { sql_statement [ ; ] [ ,...n ] | EXTERNAL NAME < method specifier > [ ; ] }
<logon_trigger_option> ::=
[ ENCRYPTION ]
[ EXECUTE AS Clause ]
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容