1 --CREATE table users--Creating user tables2 --(3 --IDs int primary key identity (.),4 --name nvarchar,--names5 --class nvarchar (200)--Class number6 --)7 8 --CREATE table class--Creating a class table9 --(Ten --classcode nvarchar ($) primary key,--class number One --className nvarchar (,--class name) A --) - - ----Insert class table data the --INSERT into class values (' c001 ', ' one Shift '); - --INSERT into class values (' c002 ', ' Class Two '); - --INSERT into class values (' c003 ', ' class Three '); - --INSERT into class values (' c004 ', ' Class Four '); + --INSERT into class values (' c005 ', ' five classes '); - ----Insert User table data + --INSERT into users values (' Zhang Yi ', ' c001 '); A --INSERT into users values (' Zhang Yi ', ' c002 '); at --INSERT into users values (' Zhang San ', ' c003 '); - --INSERT into users values (' Zhang Si ', ' c004 '); - --INSERT into users values (' Five ', ' c005 '); - - --select * from users; - --SELECT * from class; in - ----Trigger to ----(a special stored procedure, no way to call it directly, but by adding or deleting the action to trigger it + ----One action on a table can only have one trigger) - Create TriggerUsers_delete the onUsers * for Delete--perform a delete operation to trigger, and then perform the following actions $ asPanax Notoginseng Select * fromUsers - the + Create TriggerUsers_insert A onUsers the for Insert--perform an insert operation to trigger and then perform the Operation + as - Select * fromUsers $ $ Create TriggerUsers_delete - onUsers -instead of Delete--When you perform a delete operation, the operation is not performed and the following actions replace the as - Select * fromUsersWuyi the Create TriggerUsers_delete - onUsers Wuinstead of Delete - as About Select * fromDeleted--querying for deleted data $ - - Create TriggerUsers_delete - onUsers Ainstead of Delete + as the Declare @a nvarchar( -) - Select @a=Ids fromdeleted $ if @a=2 the begin the Select 'It 's too ugly to be deleted! ' the End the Else - begin in Delete fromUserswhereIds=@a the End the About Delete fromUserswhereIds=2 the the the + ----Cascade Delete - Create TriggerClass_delete the onclassBayiinstead of Delete the as the Declare @a nvarchar( $); - Select @a=Classcode fromdeleted; - Delete fromUserswhereClass=@a; the Delete fromClasswhereClasscode=@a; the the Delete fromClasswhereClasscode='c001';
"2018-01-19" SQL server-Trigger