-----Trigger------------------- ---Trigger: is a special kind of stored procedure that is an action that occurs automatically when an operation is performedSelect * fromQ_user--------------the Insert trigger-------------- if(object_id('Tr_insert','TR')) is not NULL Drop TriggerTr_insertCreate TriggerTr_insert onQ_user--a trigger based on which table is created (that is, which table is automatically triggered when an insert operation occurs) for Insert --What happens when the action occurs automatically --Action upate Insert Delete --When a for is in operation occurs after an after operation, instead of the current operation, execution of the triggered action as --While inserting the Q_user, insert the UserInformation --declare @uid varchar (@uName varchar) --Select @uid =uid, @uName =uname from inserted ----Get the corresponding row generated after the Operation ----(insert represents triggered table-based) --INSERT INTO userinformation (uid,name) VALUES (@uid, @uName) ----You can also insert a SELECT statement Insert intoUserInformation (Uid,name)SelectUid,uname frominsertedGo --Test Insert intoQ_user (UNAME,UPWD)Values('Trigger','11111') Select * fromuserinformation----Test success ------The UPDATE trigger--------------Part of the field with new Q_user's colleague and new UserInformationCreate Triggertr_update onQ_user AfterUpdate as Declare @uid varchar( -),@name varchar( -) Select @uid=Uid@name=UName fromInserted--get the corresponding row generated after the Operation UpdateUserInformationSetName=@name whereUid=@uid Go UpdateQ_userSetUName='Update triggers' whereUName='Trigger' Select * fromuserinformation----delete Trigger---------because the UID inside the userinformation is the foreign key inside the Q_user, the error is not used when the cascade is deleted.DeleteQ_userwhereUid='10002' --The DELETE statement conflicts with the REFERENCE constraint "Df_uid". This conflict occurred in database "Qzone", table "Dbo.userinformation", column ' UID ' --Cascading is the ability to use cascading updates and deletes when a table and another table have a foreign key relationship . --but only if this table is set to Cascade --The Cascade method is set to create a Cascade update, and after the foreign key statement is added on the DELETE CASCADE on update NO CASCADE, --foreign KEY (userid) references UserInfo (userid) on DELETE Cascade UpdateQ_userSetUName='cascading Updates' whereUName='Update triggers' Select * fromQ_user; Select * fromuserinformation cascade deletion with triggers---Cursor--------- ---a way to process data that can be viewed or processed in a result set --use cursors to query data for a specified interval (for example, query 10-40 for direct data containing 10-40) Print(4.5)
SQL Server master----triggers from getting Started