search
HomeDatabaseMysql TutorialSQLServerDML触发器之AFTER触发器

根据DML触发器发生的时间、编写触发器所使用的语言,可以分为AFTER触发器、INSTEAD OF触发器和CLR触发器。AFTER触发器在执行INSERT、UPDATE或DELETE语句操作之后、INSTEAD OF触发器和约束之后激发。INSTEAD OF在处理约束前激发,因此可以在INSTEAD OF中使用

   根据DML触发器发生的时间、编写触发器所使用的语言,可以分为AFTER触发器、INSTEAD OF触发器和CLR触发器。AFTER触发器在执行INSERT、UPDATE或DELETE语句操作之后、INSTEAD OF触发器和约束之后激发。INSTEAD OF在处理约束前激发,因此可以在INSTEAD OF中使用其他语句来替代激发触发器的INSERT、UPDATE等语句。并且,还可为基于一个或多个基表的视图定义INSTEAD OF触发器,从而扩展视图可支持的更新类型。CLR触发器可以是AFTER触发器或INSTEAD OF触发器,并且也可以是DDL触发器。

  需要注意的是,在创建DML触发器时,不能使用下列语句:

  ALTER DATABASE CREATE DATABASE DROP DATABASE

  LOAD DATABASE LOAD LOG RECONFIGURE

  RESTORE DATABASE RESTORE LOG

  14.1.1 AFTER触发器

  一个表中可以具有多个AFTER触发器,只要它们的名称不相同即可。每个触发器只能应用于一个表,但是一个触发器可以同时应用于一个表的三个用户操作(UPDATE、INSERT和DELETE)。

  下面的语句创建了一个PriTrigger表和一个DetailTable,其中PriTrigger表用于存放销售订单的编号和金额,DetailTable表用于存放每笔订单中的产品信息。为PriTrigger表的DELETE操作创建了一个名为PriTrigger的触发器,当删除PriTrigger表中的订单信息时,该触发器将删除DetailTable表中该笔订单的产品信息。

  USE AdventureWorks;

  GO

  -- 创建主表,存放销售订单编号和金额

  CREATE TABLE PriTable

  (OrderID int IDENTITY(1,1), OrderTotal money);

  GO

  -- 创建明细表,,存放每笔订单中的产品信息

  CREATE TABLE DetailTable

  (OrderID int, ProductID int, ProductCount int NOT NULL, Price money);

  GO

  -- 向主表中插入订单信息

  INSERT INTO PriTable VALUES (2100.00);

  INSERT INTO PriTable VALUES (1000.00);

  -- 向明细表中插入订单的产品信息

  INSERT INTO DetailTable VALUES (1,1,10,110.00);

  INSERT INTO DetailTable VALUES (1,2,10,100.00);

  INSERT INTO DetailTable VALUES (2,2,10,100.00);

  GO

  -- 为PriTrigger表创建触发器

  CREATE TRIGGER PriTrigger

  ON PriTable

  AFTER DELETE

  AS

  DELETE FROM DetailTable

  WHERE OrderID IN (SELECT OrderID

  FROM Deleted);

  PRINT N'已经删除了DetailTable表中的相关数据' -- 此句仅为演示需要,在触发器中不应当使用这样的信息语句

  在定义触发器时,触发器名称在CREATE TRIGGER关键字之后,ON子句指定要创建触发器的基表。AFTER子句(也可以使用FOR来代替AFTER关键字,二者功能相同)指定激活触发器的操作语句,可以同时指定多个操作语句。例如,“AFTER DELETE, INSERT”表示在对表执行DELETE、INSERT语句时激活触发器。AS关键字后指定触发器执行什么样的操作。

  注意WHERE条件中IN子句中的Deleted关键字。当从PriTrigger表中删除行时,被删除的行会被复制到一个名为Deleted的临时内存表中。如果为表指定了一个执行INSERT语句时的触发器,则在向表中插入行时,新行将同时被添加到一个名为Inserted的临时内存表中。如果为表指定了一个执行UPDATE语句时的触发器,由于更新事务类似于在删除操作之后执行插入操作。因此,旧行被复制到Deleted表中,然后,新行被复制到触发器表和Inserted表中。

  Deleted表和Inserted表都是由SQL Server自动创建和管理的,这些表的结构与定义触发器的基表的结构相同。

  执行下面的语句从PriTable表中删除OrderID为1的行,这时触发器会自动删除DetailTable表中的相关行,得到的结果和消息如图14-1所示。

  DELETE FROM PriTable WHERE OrderID = 1;

  SELECT * FROM PriTable;

  SELECT * FROM DetailTable;

SQLServerDML触发器之AFTER触发器   三联

  图14-1 删除PriTable表中OrderID为1的行时得到的结果和消息

  如果执行下面的语句,准备从PriTable表中删除OrderID为3的行。由于PriTable表中并不存在这样的行,所以并不会删除成功。虽然没有删除成功,但是在消息窗口中仍然可以看到由触发器的PRINT语句发回的信息。这说明即使语句没有影响到表中的行,也会激活触发器。在没有删除成功的情况下,Deleted表是一个空表。

  DELETE FROM PriTable WHERE OrderID = 3;

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Adding Users to MySQL: The Complete TutorialAdding Users to MySQL: The Complete TutorialMay 12, 2025 am 12:14 AM

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

Mastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMay 12, 2025 am 12:12 AM

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

MySQL: String Data Types and Indexing: Best PracticesMySQL: String Data Types and Indexing: Best PracticesMay 12, 2025 am 12:11 AM

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.

MySQL: How to Add a User RemotelyMySQL: How to Add a User RemotelyMay 12, 2025 am 12:10 AM

ToaddauserremotelytoMySQL,followthesesteps:1)ConnecttoMySQLasroot,2)Createanewuserwithremoteaccess,3)Grantnecessaryprivileges,and4)Flushprivileges.BecautiousofsecurityrisksbylimitingprivilegesandaccesstospecificIPs,ensuringstrongpasswords,andmonitori

The Ultimate Guide to MySQL String Data Types: Efficient Data StorageThe Ultimate Guide to MySQL String Data Types: Efficient Data StorageMay 12, 2025 am 12:05 AM

TostorestringsefficientlyinMySQL,choosetherightdatatypebasedonyourneeds:1)UseCHARforfixed-lengthstringslikecountrycodes.2)UseVARCHARforvariable-lengthstringslikenames.3)UseTEXTforlong-formtextcontent.4)UseBLOBforbinarydatalikeimages.Considerstorageov

MySQL BLOB vs. TEXT: Choosing the Right Data Type for Large ObjectsMySQL BLOB vs. TEXT: Choosing the Right Data Type for Large ObjectsMay 11, 2025 am 12:13 AM

When selecting MySQL's BLOB and TEXT data types, BLOB is suitable for storing binary data, and TEXT is suitable for storing text data. 1) BLOB is suitable for binary data such as pictures and audio, 2) TEXT is suitable for text data such as articles and comments. When choosing, data properties and performance optimization must be considered.

MySQL: Should I use root user for my product?MySQL: Should I use root user for my product?May 11, 2025 am 12:11 AM

No,youshouldnotusetherootuserinMySQLforyourproduct.Instead,createspecificuserswithlimitedprivilegestoenhancesecurityandperformance:1)Createanewuserwithastrongpassword,2)Grantonlynecessarypermissionstothisuser,3)Regularlyreviewandupdateuserpermissions

MySQL String Data Types Explained: Choosing the Right Type for Your DataMySQL String Data Types Explained: Choosing the Right Type for Your DataMay 11, 2025 am 12:10 AM

MySQLstringdatatypesshouldbechosenbasedondatacharacteristicsandusecases:1)UseCHARforfixed-lengthstringslikecountrycodes.2)UseVARCHARforvariable-lengthstringslikenames.3)UseBINARYorVARBINARYforbinarydatalikecryptographickeys.4)UseBLOBorTEXTforlargeuns

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version