sql trigger 创建与用法
发器也是一种带名的pl/sql块。触发器类似于过程和函数,因为它们都是拥有声明
、执行和异常处理过程的带名pl/sql块。与包类似,触发器必须存储在中并
且不能被块进行本地化声明。
对于触发器而言,当触发事件发生的时候就会显式地执行该触发器,并且触发器不
接受参数
create table employee(
2> id int,
3> name nvarchar (10),
4> salary int,
5> start_date datetime,
6> city nvarchar (10),
7> region char (1))
8> go
1>
2> insert into employee (id, name, salary, start_date, city,region)
3> values (1, 'jason', 40420, '02/01/94', 'new york','w')
4> go(1 rows affected)
1> insert into employee (id, name, salary, start_date, city,region)
2> values (2, 'robert',14420, '01/02/95','vancouver','n')
3> go(1 rows affected)
1> insert into employee (id, name, salary, start_date, city,region)
2> values (3, 'celia', 24020, '12/03/96', 'toronto','w')
3> go(1 rows affected)
1> insert into employee (id, name, salary, start_date, city,region)
2> values (4, 'linda', 40620, '11/04/97', 'new york','n')
3> go(1 rows affected)
1> insert into employee (id, name, salary, start_date, city,region)
2> values (5, 'david', 80026, '10/05/98','vancouver','w')
3> go(1 rows affected)
1> insert into employee (id, name, salary, start_date, city,region)
2> values (6, 'james', 70060, '09/06/99', 'toronto','n')
3> go(1 rows affected)
1> insert into employee (id, name, salary, start_date, city,region)
2> values (7, 'alison',90620, '08/07/00', 'new york','w')
3> go(1 rows affected)
1> insert into employee (id, name, salary, start_date, city,region)
2> values (8, 'chris', 26020, '07/08/01','vancouver','n')
3> go(1 rows affected)
1> insert into employee (id, name, salary, start_date, city,region)
2> values (9, 'mary', 60020, '06/09/02', 'toronto','w')
3> go(1 rows affected)
1>
2> * from employee
3> go
id name salary start_date cityregion
----------- ---------- ----------- ----------------------- ---------- ------
1 jason 40420 1994-02-01 00:00:00.000 new york w
2 robert 14420 1995-01-02 00:00:00.000 vancouver n
3 celia 24020 1996-12-03 00:00:00.000 toronto w
4 linda 40620 1997-11-04 00:00:00.000 new york n
5 david 80026 1998-10-05 00:00:00.000 vancouver w
6 james 70060 1999-09-06 00:00:00.000 toronto n
7 alison 90620 2000-08-07 00:00:00.000 new york w
8 chris 26020 2001-07-08 00:00:00.000 vancouver n
9 mary 60020 2002-06-09 00:00:00.000 toronto w(9 rows affected)
1>
2> -- creating and using triggers
3>
4>
5> create trigger mytrigger
6> on employee
7> for update, insert, delete
8> as
9> select * from inserted
10> select * from deleted
11> go
1>
2>
3> select 'before insert'
4> insert employee (id, name) values (31, 'rick')
5> go-------------
before insert(1 rows affected)
id name salary start_date cityregion
----------- ---------- ----------- ----------------------- ---------- ------
31 rick null null nullnull
(1 rows affected)
id name salary start_date cityregion
----------- ---------- ----------- ----------------------- ---------- ------
(0 rows affected)
1>
2> select 'before update'
3> update employee
4> set name = 'rickie'
5> where id = 3
6> go-------------
before update(1 rows affected)
id name salary start_date cityregion
----------- ---------- ----------- ----------------------- ---------- ------
3 rickie 24020 1996-12-03 00:00:00.000 toronto w(1 rows affected)
id name salary start_date cityregion
----------- ---------- ----------- ----------------------- ---------- ------
3 celia 24020 1996-12-03 00:00:00.000 toronto w(1 rows affected)
1>
2> select 'before delete'
3> delete from employee where id = 3
4> go-------------
before delete(1 rows affected)
id name salary start_date cityregion
----------- ---------- ----------- ----------------------- ---------- ------
(0 rows affected)
id name salary start_date cityregion
----------- ---------- ----------- ----------------------- ---------- ------
3 rickie 24020 1996-12-03 00:00:00.000 toronto w(1 rows affected)
1>
2>
3> drop table employee
4> go
1>
dml触发器的激活顺序
1)执行before语句级触发器—如果存在这种触发器
2)对受该语句影响的每一行记录
执行before行级触发器—如果存在这种触发器
执行该语句本身
执行after行级触发器--如果存在这种触发器
3)执行after语句级触发器--如果存在这种触发器
同一种类型的触发器的点火次序没有经过定义。如果该次序很重要的话,那么建议
将所有这些操作组合到一个触发器当中。
3、行级触发器中的关联标识符
触发器的激活语句每处理一行数据,行级触发器就会激活一次。可以在这种行级触
发器内部,访问正被处理的记录行中的数据。这是通过两个关联标识符--:old
和:new—实现的。关联标识符也是pl/sql的一种特殊的绑定变量。标识符前面的冒
号,既说明这二者都是绑定变量,同时也说明它们不是一般的pl/sql变量。pl/sql
编译器会将它们看作下面这个类型的记录:
triggering_table%rowtype
其中triggering_table是在其上定义触发器的表名。于是,下面这种引用
:new.field
就只有当其中的field是该触发表中的字段名时才会有效。
触发语句
:old
:new
insert
未定义—所有字段均为null
触发语句完成的时候,要插入的值
update
更新以前相应记录行的原始值
触发语句完成的时候,要更新的值
delete
删除以前相应记录行的原始值
未定义—所有字段均为null
注意:insert语句上没有定义:old标识符,delete语句上也没有定义:new标识符。
如果再insert语句上使用:old标识符,或者在delete语句上使用:new标识符,
pl/sql并不会产生错误,但是这两个字段值都会为null。
伪记录
虽然在语法构成上,会将:new和:old看作triggering_table%rowtype类型的记录,
但是,实际上它们并不是记录。因此,那些能够在记录上正常执行的操作,并不能
在:new和:old上执行。例如,不能将它们作为一个整体进行赋值。只能对其中的各
个字段分别赋值。

InnoDB使用redologs和undologs確保數據一致性和可靠性。 1.redologs記錄數據頁修改,確保崩潰恢復和事務持久性。 2.undologs記錄數據原始值,支持事務回滾和MVCC。

EXPLAIN命令的關鍵指標包括type、key、rows和Extra。 1)type反映查詢的訪問類型,值越高效率越高,如const優於ALL。 2)key顯示使用的索引,NULL表示無索引。 3)rows預估掃描行數,影響查詢性能。 4)Extra提供額外信息,如Usingfilesort提示需要優化。

Usingtemporary在MySQL查詢中表示需要創建臨時表,常見於使用DISTINCT、GROUPBY或非索引列的ORDERBY。可以通過優化索引和重寫查詢避免其出現,提升查詢性能。具體來說,Usingtemporary出現在EXPLAIN輸出中時,意味著MySQL需要創建臨時表來處理查詢。這通常發生在以下情況:1)使用DISTINCT或GROUPBY時進行去重或分組;2)ORDERBY包含非索引列時進行排序;3)使用複雜的子查詢或聯接操作。優化方法包括:1)為ORDERBY和GROUPB

MySQL/InnoDB支持四種事務隔離級別:ReadUncommitted、ReadCommitted、RepeatableRead和Serializable。 1.ReadUncommitted允許讀取未提交數據,可能導致臟讀。 2.ReadCommitted避免臟讀,但可能發生不可重複讀。 3.RepeatableRead是默認級別,避免臟讀和不可重複讀,但可能發生幻讀。 4.Serializable避免所有並發問題,但降低並發性。選擇合適的隔離級別需平衡數據一致性和性能需求。

MySQL適合Web應用和內容管理系統,因其開源、高性能和易用性而受歡迎。 1)與PostgreSQL相比,MySQL在簡單查詢和高並發讀操作上表現更好。 2)相較Oracle,MySQL因開源和低成本更受中小企業青睞。 3)對比MicrosoftSQLServer,MySQL更適合跨平台應用。 4)與MongoDB不同,MySQL更適用於結構化數據和事務處理。

MySQL索引基数对查询性能有显著影响:1.高基数索引能更有效地缩小数据范围,提高查询效率;2.低基数索引可能导致全表扫描,降低查询性能;3.在联合索引中,应将高基数列放在前面以优化查询。

MySQL學習路徑包括基礎知識、核心概念、使用示例和優化技巧。 1)了解表、行、列、SQL查詢等基礎概念。 2)學習MySQL的定義、工作原理和優勢。 3)掌握基本CRUD操作和高級用法,如索引和存儲過程。 4)熟悉常見錯誤調試和性能優化建議,如合理使用索引和優化查詢。通過這些步驟,你將全面掌握MySQL的使用和優化。

MySQL在現實世界的應用包括基礎數據庫設計和復雜查詢優化。 1)基本用法:用於存儲和管理用戶數據,如插入、查詢、更新和刪除用戶信息。 2)高級用法:處理複雜業務邏輯,如電子商務平台的訂單和庫存管理。 3)性能優化:通過合理使用索引、分區表和查詢緩存來提升性能。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Dreamweaver CS6
視覺化網頁開發工具

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

SublimeText3 Linux新版
SublimeText3 Linux最新版

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

WebStorm Mac版
好用的JavaScript開發工具