SQL SERVER 触发器介绍
触发器是一种特殊类型的存储过程,它不同于之前的我们介绍的存储过程。触发器主要是通过事件进行触发被自动调用执行的。而存储过程可以通过存储过程的名称被调用。
Ø 什么是触发器
触发器对表进行插入、更新、删除的时候会自动执行的特殊存储过程。触发器一般用在check约束更加复杂的约束上面。触发器和普通的存储过程的区别是:触发器是当对某一个表进行操作。诸如:update、insert、delete这些操作的时候,系统会自动调用执行该表上对应的触发器。SQL Server 2005中触发器可以分为两类:DML触发器和DDL触发器,其中DDL触发器它们会影响多种数据定义语言语句而激发,这些语句有create、alter、drop语句。
DML触发器分为:
1、 after触发器(之后触发)
a、 insert触发器
b、 update触发器
c、 delete触发器
2、 instead of 触发器 (之前触发)
其中after触发器要求只有执行某一操作insert、update、delete之后触发器才被触发,且只能定义在表上。而instead of触发器表示并不执行其定义的操作(insert、update、delete)而仅是执行触发器本身。既可以在表上定义instead of触发器,也可以在视图上定义。
触发器有两个特殊的表:插入表(instered表)和删除表(deleted表)。这两张是逻辑表也是虚表。有系统在内存中创建者两张表,不会存储在数据库中。而且两张表的都是只读的,只能读取数据而不能修改数据。这两张表的结果总是与被改触发器应用的表的结构相同。当触发器完成工作后,这两张表就会被删除。Inserted表的数据是插入或是修改后的数据,而deleted表的数据是更新前的或是删除的数据。
Update数据的时候就是先删除表记录,然后增加一条记录。这样在inserted和deleted表就都有update后的数据记录了。注意的是:触发器本身就是一个事务,所以在触发器里面可以对修改数据进行一些特殊的检查。如果不满足可以利用事务回滚,撤销操作。
Ø 创建触发器
语法
tgr_name<br> table_name<br> encrypion –加密触发器<br> ...<br><br> Transact-SQL
# 创建insert类型触发器
--创建insert插入类型触发器<br> (object_id(, ) )<br> tgr_classes_insert<br><br> tgr_classes_insert<br> classes<br> insert --插入触发<br><br> --定义变量<br> @id , @name (20), @temp ;<br> --在inserted表中查询已经插入记录信息<br> @id = id, @name = name inserted;<br> @name = @name + (, @id);<br> @temp = @id / 2; <br> insert student (@name, 18 + @id, @temp, @id);<br> ;<br><br>--插入数据<br>insert classes (, getDate());<br>--查询数据<br> * classes;<br> * student id;
insert触发器,会在inserted表中添加一条刚插入的记录。
# 创建delete类型触发器
--delete删除类型触发器<br> (object_id(, ) )<br> tgr_classes_delete<br><br> tgr_classes_delete<br> classes<br> --删除触发<br><br> ; <br> (object_id(, ) )<br> --存在classesBackup,直接插入数据<br> insert classesBackup name, createDate deleted;<br> <br> --不存在classesBackup创建再插入<br> * classesBackup deleted;<br> ;<br><br><br><br>-- nocount ;<br> classes name = ;<br>--查询数据<br> * classes;<br> * classesBackup;
delete触发器会在删除数据的时候,将刚才删除的数据保存在deleted表中。
# 创建update类型触发器
--update更新类型触发器<br> (object_id(, ) )<br> tgr_classes_update<br><br> tgr_classes_update<br> classes<br> <br><br> @oldName (20), @newName (20);<br> --更新前的数据<br> @oldName = name deleted;<br> ( ( * student name + @oldName + ))<br> <br> --更新后的数据<br> @newName = name inserted;<br> student name = replace(name, @oldName, @newName) name + @oldName + ;<br> ;<br> <br> <br> ;<br><br>--查询数据<br> * student id;<br> * classes;<br> classes name = name = ;
update触发器会在更新数据后,将更新前的数据保存在deleted表中,更新后的数据保存在inserted表中。
# update更新列级触发器
(object_id(, ) )<br> tgr_classes_update_column<br><br> tgr_classes_update_column<br> classes<br> <br><br> --列级触发器:是否更新了班级创建时间<br> ((createDate))<br> <br> (, 16, 11);<br> ;<br> <br><br>--测试<br> * student id;<br> * classes;<br> classes createDate = getDate() id = 3;<br> classes name = id = 7;
更新列级触发器可以用update是否判断更新列记录;
# instead of类型触发器
instead of触发器表示并不执行其定义的操作(insert、update、delete)而仅是执行触发器本身的内容。
创建语法
tgr_name<br> table_name<br> encryption<br> instead ...<br><br> T-SQL
# 创建instead of触发器
(object_id(, ) )<br> tgr_classes_inteadOf<br><br> tgr_classes_inteadOf<br> classes<br> instead <br><br> @id , @name (20);<br> --查询被删除的信息,病赋值<br> @id = id, @name = name deleted;<br> + (, @id) + + @name;<br> --先删除student的信息<br> student cid = @id;<br> --再删除classes的信息<br> classes id = @id;<br> + (, @id) + + @name + ;<br><br>--test<br> * student id;<br> * classes;<br> classes id = 7;
# 显示自定义消息raiserror
(object_id(, ) )<br> tgr_message<br><br> tgr_message<br> student<br> insert, <br> (, 16, 10);<br><br>--test<br>insert student (, 22, 1, 7);<br> student sex = 0 name = ;<br> * student id;
# 修改触发器
tgr_message<br> student<br> <br> (, 16, 10);<br><br>--test<br> student name = ;
# 启用、禁用触发器
--禁用触发器<br>disable tgr_message student;<br>--启用触发器<br>enable tgr_message student;
# 查询创建的触发器信息
--查询已存在的触发器<br> * sys.triggers;<br> * sys.objects type = ;<br><br>--查看触发器触发事件<br> te.* sys.trigger_events te sys.triggers t<br> t.object_id = te.object_id<br> t.parent_class = 0 t.name = ;<br><br>--查看创建触发器语句<br> sp_helptext ;
((object_id(, ) ))<br> tgr_valid_data<br><br> tgr_valid_data<br> student<br> insert<br><br> @age ,<br> @name (20);<br> @name = s.name, @age = s.age inserted s;<br> (@age <br> (, 16, 1);<br> ;<br> <br><br>--test<br>insert student (, 2, 0, 7);<br>insert student (, 22, 0, 7);<br> * student id;
# 示例,操作日志
(object_id(, ) )
log
log(
id (1, 1) ,
(20),
createDate datetime getDate()
)
( ( * sys.objects name = ))
tgr_student_log
tgr_student_log
student
insert, ,
(( ( 1 inserted)) ( ( 1 deleted)))
insert log() ();
( ( 1 inserted) ( 1 deleted))
insert log() ();
( ( 1 inserted) ( 1 deleted))
insert log() ();
--test
insert student (, 22, 1, 7);
student sex = 0 name = ;
student name = ;
* log;
* student id;

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



HQL and SQL are compared in the Hibernate framework: HQL (1. Object-oriented syntax, 2. Database-independent queries, 3. Type safety), while SQL directly operates the database (1. Database-independent standards, 2. Complex executable queries and data manipulation).

"Usage of Division Operation in OracleSQL" In OracleSQL, division operation is one of the common mathematical operations. During data query and processing, division operations can help us calculate the ratio between fields or derive the logical relationship between specific values. This article will introduce the usage of division operation in OracleSQL and provide specific code examples. 1. Two ways of division operations in OracleSQL In OracleSQL, division operations can be performed in two different ways.

Oracle and DB2 are two commonly used relational database management systems, each of which has its own unique SQL syntax and characteristics. This article will compare and differ between the SQL syntax of Oracle and DB2, and provide specific code examples. Database connection In Oracle, use the following statement to connect to the database: CONNECTusername/password@database. In DB2, the statement to connect to the database is as follows: CONNECTTOdataba

Interpretation of MyBatis dynamic SQL tags: Detailed explanation of Set tag usage MyBatis is an excellent persistence layer framework. It provides a wealth of dynamic SQL tags and can flexibly construct database operation statements. Among them, the Set tag is used to generate the SET clause in the UPDATE statement, which is very commonly used in update operations. This article will explain in detail the usage of the Set tag in MyBatis and demonstrate its functionality through specific code examples. What is Set tag Set tag is used in MyBati

What is Identity in SQL? Specific code examples are needed. In SQL, Identity is a special data type used to generate auto-incrementing numbers. It is often used to uniquely identify each row of data in a table. The Identity column is often used in conjunction with the primary key column to ensure that each record has a unique identifier. This article will detail how to use Identity and some practical code examples. The basic way to use Identity is to use Identit when creating a table.

Solution: 1. Check whether the logged-in user has sufficient permissions to access or operate the database, and ensure that the user has the correct permissions; 2. Check whether the account of the SQL Server service has permission to access the specified file or folder, and ensure that the account Have sufficient permissions to read and write the file or folder; 3. Check whether the specified database file has been opened or locked by other processes, try to close or release the file, and rerun the query; 4. Try as administrator Run Management Studio as etc.

How to use SQL statements for data aggregation and statistics in MySQL? Data aggregation and statistics are very important steps when performing data analysis and statistics. As a powerful relational database management system, MySQL provides a wealth of aggregation and statistical functions, which can easily perform data aggregation and statistical operations. This article will introduce the method of using SQL statements to perform data aggregation and statistics in MySQL, and provide specific code examples. 1. Use the COUNT function for counting. The COUNT function is the most commonly used

Database technology competition: What are the differences between Oracle and SQL? In the database field, Oracle and SQL Server are two highly respected relational database management systems. Although they both belong to the category of relational databases, there are many differences between them. In this article, we will delve into the differences between Oracle and SQL Server, as well as their features and advantages in practical applications. First of all, there are differences in syntax between Oracle and SQL Server.
