ai是什么| 低血压去药店买什么药| 一杆进洞叫什么球| 什么的四季| 尿道感染吃什么药最好| 其多列是什么意思| 什么的李逵| 鼻炎吃什么药好| 鸡蛋加什么吃壮阳持久| 什么是臆想症| 孩子不好好吃饭是什么原因| ia是什么意思| 感染性发热是什么意思| 排骨粥要搭配什么好吃| 临床是什么意思| 风采依旧是什么意思| 做梦梦到自己生病了是什么意思| 水落石出是什么意思| 畸胎瘤是什么意思| 云雾茶属于什么茶| 拉肚子吃什么好得快| 04年是什么生肖| 入职体检挂什么科| bmg是什么意思| 为什么女人要带阴环| 蛇和什么属相最配| 地软是什么| 一九九八年属什么生肖| 北海龙王叫什么| 骨折吃什么好| 王火火念什么| 颜值爆表是什么意思| 肋下未及是什么意思| 出汗对身体有什么好处| 甘油三酯高应该注意什么| 低血糖有什么危险| 血糖高适合喝什么牛奶| 三尖瓣少量反流是什么意思| 膝盖发软无力是什么原因| 政委什么级别| 望洋兴叹是什么意思| 汉族人是什么人种| 西洋菜俗称叫什么| 飞廉是什么意思| 四不像是指什么动物| 洗葡萄用什么洗最干净| 前额头痛吃什么药| 指甲有白点是缺什么| 月经来了吃什么好| 早上五六点是什么时辰| 气血虚吃什么| 肌肉纤维化是什么意思| 天干是什么意思| 海鲜过敏吃什么药| 吃花生米是什么意思| 牙齿上有黑点是什么原因| 铋剂是什么药| 吃什么对嗓子好| 胰腺在人体起什么作用| 头晕是什么感觉| 脂肪瘤吃什么药可以消除| 纯阴八字为什么要保密| 官方什么意思| 4月23日是什么星座| 什么瓜不能吃脑筋急转弯| 焦虑会引起什么症状| 中午吃什么不会胖| 牙疼是什么原因导致的| cup什么意思| 下面痒用什么药效果好| 尿蛋白是什么症状| 车前草有什么功效| 梦见杀狗是什么预兆| 心外科是看什么病的| 镜检白细胞是什么意思| 牛骨煲汤搭配什么最好| 一什么浮萍| 古代天花是现代什么病| 脱氧核苷酸是什么| 白肉是什么肉| 蕊字五行属什么| 小便发红是什么症状男| 足外翻挂什么科| 空调睡眠模式是什么意思| 长脸适合什么短头发| 颈椎引起的头晕是什么症状| castle什么意思| 跳槽是什么意思| 聪明绝顶是什么意思| 腿不自觉的抖是什么原因| 姐姐家的孩子叫什么| 梦见生小孩是什么征兆| 皮质醇高是什么原因| 嘴唇不红润是什么原因| 电视为什么打不开| 空鼻症是什么症状| 吃烧烤后吃什么水果可以帮助排毒| 观字五行属什么| 副高是什么职称| 草酸是什么| 10.17是什么星座| 病毒感染咳嗽吃什么药效果好| 机械油污用什么能洗掉| 快速补血吃什么| 惰性是什么意思| 梦见栽树是什么意思| 脚气泡脚用什么泡最好| 金什么| 胎盘低是什么原因造成的| 儿童腮腺炎吃什么药| 吐露是什么意思| 右肺下叶纤维灶是什么意思| 小猫踩奶是什么意思| 鼠的本命佛是什么佛| 胰腺在人体什么部位| 256排ct能检查什么病| 惊天动地是什么生肖| 睡觉醒来口苦是什么原因| 插入阴道什么感觉| 肚脐连着什么器官| 蒲公英有什么药效| 高油酸是什么意思| 梦见捡手机是什么意思| 神经是什么东西| 螃蟹为什么吐泡泡| 痰培养是检查什么的| 特朗普是什么星座| 下午右眼跳是什么预兆| 过敏了吃什么药好| 骨痂是什么意思| 4.5是什么星座| 高尿酸有什么症状| 孩子皮肤黑是什么原因| 惨不忍睹是什么意思| 繁星是什么意思| 什么生辉| 75岁属什么| 吃什么补肾壮阳最快速| 扶她是什么| mle是什么意思| 沙和尚是什么妖怪| 指甲长出来是白色的什么原因| 熵是什么| 鳖是什么动物| pn医学上是什么意思| 左胸下面是什么部位| 双子座和什么星座最配| 八纲辨证中的八纲是什么| 六味地黄丸主治什么| ha什么意思| 发烧是什么感觉| 什么人不适合种植牙| 腹胀屁多是什么原因| 什么耳什么腮| 苏州市长什么级别| 金蟾吃什么| 马云是什么大学毕业的| 为什么牙缝里的东西很臭| 心阳虚吃什么药| pin是什么意思| 十三是什么意思| 为什么三文鱼可以生吃| 知了为什么会叫| 菠萝蜜的核有什么功效| 惊涛骇浪是什么意思| 介入是什么意思| 咳血鲜红是什么病呢| 什么t恤质量好| 此地无银三百两是什么意思| 可惜是什么意思| 胸口闷闷的有点疼是什么原因| 使节是什么意思| 吃饭容易出汗是什么原因| 氯喹是什么药| 国际劳动日是什么生肖| 飞代表什么生肖| 口水多是什么原因| 摩什么擦什么| 侄子叫我什么| 4.22什么星座| 饭前吃药和饭后吃药有什么区别| 黑胡桃色是什么颜色| 思密达是什么药| 慢性宫颈炎是什么原因引起的| 对什么有好处的英文| 神采什么| 什么是高情商| 铮铮是什么意思| 21金维他什么时候吃效果最好| 88是什么意思| 士加一笔是什么字| 6月30号是什么星座| 什么食物含硒量最高| 御是什么意思| 今年贵庚是什么意思| 事后紧急避孕药什么时候吃有效| 祸起萧墙是什么意思| 义是什么意思| 红细胞分布宽度偏低是什么原因| 吃什么可以补精| 榴莲不能和什么一起吃| 楷字五行属什么| 子不问卜自惹祸殃什么意思| 羊悬筋是什么样子图片| 大麦茶是什么做的| csw是什么意思| 慢热型是什么意思| 惶恐是什么意思| pgr是什么意思| 口加个齿读什么| 碱性磷酸酶偏高是什么原因| 水是由什么组成的| 一什么二什么| 六月初八是什么日子| 吃什么东西排酸最快| 2002年属马的是什么命| 什么网站可以看三级片| 急性荨麻疹用什么药| 散光400度是什么概念| 腰突挂什么科| 4月5日是什么星座| 阴沟肠杆菌是什么病| 什么时辰出生的人命好| 肾上腺素是什么东西| 男性尿很黄是什么原因| 唵嘛呢叭咪吽是什么意思| 脚为什么会肿| 林心如什么学历| 弥是什么意思| 痛什么什么痛| 巨蟹座是什么星象| 日可以加什么偏旁| 谷草转氨酶偏高是什么意思| 爸爸是什么意思| 属鸡的女生和什么属相最配| 胃肠感冒吃什么药| 没字去掉三点水念什么| 痔疮发痒是什么原因| 胃痛是什么原因| 收尿干什么用的| 7月4号是什么星座| 大拇指有黑色竖纹是什么原因| 格物穷理是什么意思| lv中文名叫什么| 夏天穿什么衣服比较凉爽| 什么可以美白牙齿| 会来事是什么意思| 总恶心是什么原因| 六度万行 是什么意思| 梦见钓鱼是什么意思周公解梦| 什么是钙化点| 不着相是什么意思| 亚临床甲亢是什么意思| 呕吐出血是什么原因| 思维跳脱是什么意思| 左眼跳代表什么| 肝低密度灶是什么意思| 晚上难入睡是什么原因| fl什么意思| 什么教无类| 九层塔是什么菜| 去香港澳门需要什么证件| 喉咙肿痛吃什么药好| 4月9日什么星座| 百度Jump to content

国际台参加戛纳国际电视节 拓展本土化译制播出业务

From Wikipedia, the free encyclopedia
百度 遗憾的是因塔门窄小未能将佛像搬出。

A database trigger is procedural code that is automatically executed in response to certain events on a particular table or view in a database. The trigger is mostly used for maintaining the integrity of the information on the database. For example, when a new record (representing a new worker) is added to the employees table, new records should also be created in the tables of the taxes, vacations and salaries. Triggers can also be used to log historical data, for example to keep track of employees' previous salaries.

Triggers in DBMS

[edit]

Below follows a series of descriptions of how some popular DBMS support triggers.

Oracle

[edit]

In addition to triggers that fire (and execute PL/SQL code) when data is modified, Oracle 10g supports triggers that fire when schema-level objects (that is, tables) are modified and when user logon or logoff events occur.

Schema-level triggers

[edit]
  • After Creation
  • Before Alter
  • After Alter
  • Before Drop
  • After Drop
  • Before Insert

The four main types of triggers are:

  1. Row-level trigger: This gets executed before or after any column value of a row changes.
  2. Column-level trigger: This gets executed before or after the specified column changes.
  3. For each row type: This trigger gets executed once for each row of the result set affected by an insert/update/delete.
  4. For each statement type: This trigger gets executed only once for the entire result set, but also fires each time the statement is executed.

System-level triggers

[edit]

From Oracle 8i, database events - logons, logoffs, startups - can fire Oracle triggers.[1]

Microsoft SQL Server

[edit]

MS SQL Server supports trigger for DML and DDL statement plus special trigger "logon".

The scope of DDL triggers can be a database (CREATE TRIGGER name ON DATABASE ...) or the entire SQL Server instance (CREATE TRIGGER name ON ALL SERVER). When you use the entire instance, you can capture all events executed on commands that have server-level scope as well as any commands that have database-level scope in the SQL instance.

A list of all available firing events in Microsoft SQL Server for DDL triggers is available on Microsoft Docs.[2]

Performing conditional actions in DML triggers (or testing data following modification) is done through accessing the pseudo tables (temporary ones) Inserted and Deleted tables. DML trigger are always "FOR EACH STATEMENT" but can be code at row event ("FOR EACH ROW") using a cursor on the inserted/deleted pseudo tables.

PostgreSQL

[edit]

Introduced support for triggers in 1997. The following functionality in SQL:2003 was previously not implemented in PostgreSQL:

  • SQL allows triggers to fire on updates to specific columns; As of version 9.0 of PostgreSQL this feature is also implemented in PostgreSQL.
  • The standard allows the execution of a number of SQL statements other than SELECT, INSERT, UPDATE, such as CREATE TABLE as the triggered action. This can be done through creating a stored procedure or function to call CREATE TABLE.[3]

Synopsis:

CREATE TRIGGER name { BEFORE | AFTER } { event [ OR ... ] }
    ON TABLE [ FOR [ EACH ] { ROW | STATEMENT } ]
    EXECUTE PROCEDURE funcname ( arguments )

Firebird

[edit]

Firebird supports multiple row-level, BEFORE or AFTER, INSERT, UPDATE, DELETE (or any combination of thereof) triggers per table, where they are always "in addition to" the default table changes, and the order of the triggers relative to each other can be specified where it would otherwise be ambiguous (POSITION clause.) Triggers may also exist on views, where they are always "instead of" triggers, replacing the default updatable view logic. (Before version 2.1, triggers on views deemed updatable would run in addition to the default logic.)

Firebird does not raise mutating table exceptions (like Oracle), and triggers will by default both nest and recurse as required (SQL Server allows nesting but not recursion, by default.) Firebird's triggers use NEW and OLD context variables (not Inserted and Deleted tables,) and provide UPDATING, INSERTING, and DELETING flags to indicate the current usage of the trigger.

{CREATE | RECREATE | CREATE OR ALTER} TRIGGER name FOR {table name | view name}
 [ACTIVE | INACTIVE]
 {BEFORE | AFTER}
 {INSERT [OR UPDATE] [OR DELETE] | UPDATE [OR INSERT] [OR DELETE] | DELETE [OR UPDATE] [OR INSERT] }
 [POSITION n] AS
BEGIN
 ....
END

As of version 2.1, Firebird additionally supports the following database-level triggers:

  • CONNECT (exceptions raised here prevent the connection from completing)
  • DISCONNECT
  • TRANSACTION START
  • TRANSACTION COMMIT (exceptions raised here prevent the transaction from committing, or preparing if a two-phase commit is involved)
  • TRANSACTION ROLLBACK

Database-level triggers can help enforce multi-table constraints, or emulate materialized views. If an exception is raised in a TRANSACTION COMMIT trigger, the changes made by the trigger so far are rolled back and the client application is notified, but the transaction remains active as if COMMIT had never been requested; the client application can continue to make changes and re-request COMMIT.

Syntax for database triggers:

{CREATE | RECREATE | CREATE OR ALTER} TRIGGER name
 [ACTIVE | INACTIVE] ON
 {CONNECT | DISCONNECT | TRANSACTION START | TRANSACTION COMMIT | TRANSACTION ROLLBACK}
 [POSITION n] AS
BEGIN
 .....
END

MySQL/MariaDB

[edit]

Limited support for triggers in the MySQL/MariaDB DBMS was added in the 5.0 version of MySQL, launched in 2005.[4]

As of version 8.0, they allow for DDL (Data Definition Language) triggers and for DML (Data Manipulation Language) triggers. They also allow either type of DDL trigger (AFTER or BEFORE) to be used to define triggers. They are created by using the clause CREATE TRIGGER and deleted by using the clause DROP TRIGGER. The statement called upon an event happens is defined after the clause FOR EACH ROW, followed by a keyword (SET or BEGIN), which indicates whether what follows is an expression or a statement respectively.[5]

IBM DB2 LUW

[edit]

IBM DB2 for distributed systems known as DB2 for LUW (LUW means Linux, Unix, Windows) supports three trigger types: Before trigger, After trigger and Instead of trigger. Both statement level and row level triggers are supported. If there are more triggers for same operation on table then firing order is determined by trigger creation data. Since version 9.7 IBM DB2 supports autonomous transactions.[6]

Before trigger is for checking data and deciding if operation should be permitted. If exception is thrown from before trigger then operation is aborted and no data are changed. In DB2 before triggers are read only — you can't modify data in before triggers. After triggers are designed for post processing after requested change was performed. After triggers can write data into tables and unlike some[which?] other databases you can write into any table including table on which trigger operates. Instead of triggers are for making views writeable.

Triggers are usually programmed in SQL PL language.

SQLite

[edit]
CREATE [TEMP | TEMPORARY] TRIGGER [IF NOT EXISTS] [database_name .] trigger_name
[BEFORE | AFTER | INSTEAD OF] {DELETE | INSERT | UPDATE [OF column_name [, column_name]...]} 
ON {table_name | view_name}
   [FOR EACH ROW] [WHEN condition is mandatory ]
BEGIN
   ...
END

SQLite only supports row-level triggers, not statement-level triggers.

Updateable views, which are not supported in SQLite, can be emulated with INSTEAD OF triggers.

XML databases

[edit]

An example of implementation of triggers in non-relational database can be Sedna, that provides support for triggers based on XQuery. Triggers in Sedna were designed to be analogous to SQL:2003 triggers, but natively base on XML query and update languages (XPath, XQuery and XML update language).

A trigger in Sedna is set on any nodes of an XML document stored in database. When these nodes are updated, the trigger automatically executes XQuery queries and updates specified in its body. For example, the following trigger cancels person node deletion if there are any open auctions referenced by this person:

CREATE TRIGGER "trigger3"
    BEFORE DELETE
    ON doc("auction")/site//person
    FOR EACH NODE
    DO
    {
        if (exists($WHERE//open_auction/bidder/personref/@person=$OLD/@id))
        then ( )
        else $OLD;
    }

Row and statement level triggers

[edit]

To understand how trigger behavior works, you need to be aware of the two main types of triggers; these are Row and Statement level triggers. The distinction between the two is how many times the code within the trigger is executed, and at what time.

Suppose you have a trigger that is made to be called on an UPDATE to a certain table. Row level triggers would execute once for each row that is affected by the UPDATE. It is important to keep in mind if no rows are affected by the UPDATE command, the trigger will not execute any code within the trigger. Statement level triggers will be called once regardless of how many rows are affected by the UPDATE. Here it is important to note that even if the UPDATE command didn't affect any rows, the code within the trigger will still be executed once.

Using the BEFORE and AFTER options[7] determine when the trigger is called. Suppose you have a trigger that is called on an INSERT to a certain table. If your trigger is using the BEFORE option, the code within the trigger will be executed before the INSERT into the table occurs. A common use of the BEFORE trigger is to verify the input values of the INSERT, or modify the values accordingly. Now let's say we have a trigger that uses AFTER instead. The code within the trigger is executed after the INSERT happens to the table. An example use of this trigger is creating an audit history of who has made inserts into the database, keeping track of the changes made. When using these options you need to keep a few things in mind. The BEFORE option does not allow you to modify tables, that is why input validation is a practical use. Using AFTER triggers allows you to modify tables such as inserting into an audit history table.

When creating a trigger to determine if it is statement or row level simply include the FOR EACH ROW clause for a row level, or omit the clause for a statement level. Be cautious of using additional INSERT/UPDATE/DELETE commands within your trigger, because trigger recursion is possible, causing unwanted behavior. In the examples below each trigger is modifying a different table, by looking at what is being modified you can see some common applications of when different trigger types are used.

The following is an Oracle syntax example of a row level trigger that is called AFTER an update FOR EACH ROW affected. This trigger is called on an update to a phone book database. When the trigger is called it adds an entry into a separate table named phone_book_audit. Also take note of triggers being able to take advantage of schema objects like sequences,[8] in this example audit_id_sequence.nexVal is used to generate unique primary keys in the phone_book_audit table.

CREATE OR REPLACE TRIGGER phone_book_audit
  AFTER UPDATE ON phone_book FOR EACH ROW
BEGIN
  INSERT INTO phone_book_audit 
    (audit_id,audit_change, audit_l_name, audit_f_name, audit_old_phone_number, audit_new_phone_number, audit_date) 
    VALUES
    (audit_id_sequence.nextVal,'Update', :OLD.last_name, :OLD.first_name, :OLD.phone_number, :NEW.phone_number, SYSDATE);
END;

Now calling an UPDATE on the phone_book table for people with the last name 'Jones'.

UPDATE phone_book SET phone_number = '111-111-1111' WHERE last_name = 'Jones';
Audit_ID Audit_Change F_Name L_Name New_Phone_Number Old_Phone_Number Audit_Date
1 Update Jordan Jones 111-111-1111 098-765-4321 02-MAY-14
2 Update Megan Jones 111-111-1111 111-222-3456 02-MAY-14


Notice that the phone_number_audit table is now populated with two entries. This is due to the database having two entries with the last name of 'Jones'. Since the update modified two separate row values, the created trigger was called twice; once after each modification.

After - statement-level trigger

[edit]

An Oracle syntax statement trigger that is called after an UPDATE to the phone_book table. When the trigger gets called it makes an insert into phone_book_edit_history table

CREATE OR REPLACE TRIGGER phone_book_history
  AFTER UPDATE ON phone_book
BEGIN
  INSERT INTO phone_book_edit_history 
    (audit_history_id, username, modification, edit_date) 
    VALUES
    (audit_history_id_sequence.nextVal, USER,'Update', SYSDATE);
END;

Now doing exactly the same update as the above example, however this time with a statement level trigger.

UPDATE phone_book SET phone_number = '111-111-1111' WHERE last_name = 'Jones';
Audit_History_ID Username Modification Edit_Date
1 HAUSCHBC Update 02-MAY-14

The result shows that the trigger was only called once, even though the update did change two rows.

Before each - row-level trigger

[edit]

This example demonstrates a BEFORE EACH ROW trigger that modifies the INSERT using a WHEN conditional. If the last name is larger than 10 letters, using the SUBSTR function[9] we change the last_name column value to an abbreviation.

CREATE OR REPLACE TRIGGER phone_book_insert
  BEFORE INSERT ON phone_book FOR EACH ROW
  WHEN (LENGTH(new.last_name) > 10)
BEGIN
    :new.last_name := SUBSTR(:new.last_name,0,1);
END;

Now performing an INSERT of someone with a large name.

INSERT INTO phone_book VALUES
(6, 'VeryVeryLongLastName', 'Erin', 'Minneapolis', 'MN', '989 University Drive', '123-222-4456', 55408, TO_DATE('11/21/1991', 'MM/DD/YYYY'));
Person_ID Last_Name First_Name City State_Abbreviation Address Phone_Number Zip_code DOB
6 V Erin Minneapolis MN 989 University Drive 123-222-4456 55408 21-NOV-91

The trigger worked as per the result above, modifying the value of the INSERT before it was executed.

Before - statement-level trigger

[edit]

Using a BEFORE statement trigger is particularly useful when enforcing database restrictions.[10] This example demonstrate how to enforce a restriction upon someone named "SOMEUSER" on the table phone_book.

CREATE OR REPLACE TRIGGER hauschbc 
  BEFORE INSERT ON SOMEUSER.phone_book
BEGIN
    RAISE_APPLICATION_ERROR (
         num => -20050,
         msg => 'Error message goes here.');
END;

Now, when "SOMEUSER" is logged in after attempting any INSERT this error message will show:

SQL Error: ORA-20050: Error message goes here.

Custom errors such as this one has a restriction on what the num variable can be defined as. Because of the numerous other pre-defined errors this variable must be in the range of ?20000 to ?20999.

References

[edit]
  1. ^ Nanda, Arup; Burleson, Donald K. (2003). "9". In Burleson, Donald K. (ed.). Oracle Privacy Security Auditing: Includes Federal Law Compliance with HIPAA, Sarbanes Oxley and the Gramm Leach Bliley Act GLB. Oracle in-focus series. Vol. 47. Kittrell, North Carolina: Rampant TechPress. p. 511. ISBN 9780972751391. Retrieved 2025-08-06. [...] system-level triggers [...] were introduced in Oracle8i. [...] system-level triggers are fired at specific system events such as logon, logoff, database startup, DDL execution, and servererror [...].
  2. ^ "DDL Events - SQL Server". 15 March 2023.
  3. ^ "PostgreSQL: Documentation: 9.0: CREATE TRIGGER". www.postgresql.org. 8 October 2015.
  4. ^ MySQL 5.0 Reference Manual. "Triggers. MySQL 5.0 added limited support for triggers", Oracle Corporation, Retrieved on 4 March 2020.
  5. ^ "MySQL :: MySQL 8.0 Reference Manual :: 25.3.1 Trigger Syntax and Examples".
  6. ^ "Autonomous transactions". www.ibm.com. July 30, 2009.
  7. ^ "6 Using Triggers". docs.oracle.com.
  8. ^ "Oracle's Documentation on Sequences". Archived from the original on 2025-08-06.
  9. ^ "Oracle SQL Functions – The Complete List". December 26, 2014.
  10. ^ "Database PL/SQL Language Reference". docs.oracle.com.
[edit]
紫色适合什么肤色的人 蜜蜡五行属什么 牙龈上火吃什么药 记忆力减退吃什么药效果好 面基什么意思
书五行属什么 野茶属于什么茶 一个三点水一个令念什么 军长相当于地方什么官 吃什么药头脑立刻清醒
什么叫脂肪肝 换药挂什么科 经期适合吃什么 脸颊长痘痘什么原因 三伏天是什么
瓜子脸剪什么发型好看 常吃木耳有什么好处和坏处 adhd是什么 演唱会安可是什么意思 减肥餐吃什么
人为什么要刷牙hcv9jop5ns0r.cn 冬天吃什么96micro.com 枸橼酸西地那非片有什么副作用hcv9jop5ns3r.cn 甯字五行属什么hcv9jop0ns8r.cn 蜂蜡是什么hcv7jop7ns0r.cn
医院打耳洞挂什么科hcv7jop9ns9r.cn 什么因什么果hcv8jop5ns6r.cn 碍事是什么意思hcv7jop9ns0r.cn mfd是什么意思hcv9jop0ns2r.cn 腰椎穿刺是检查什么的hcv8jop5ns7r.cn
粉尘作业时必须佩戴什么口罩hcv9jop6ns9r.cn 做梦梦到男朋友出轨了是什么意思hcv9jop2ns1r.cn 全职太太是什么意思hcv9jop0ns4r.cn 腹股沟黑是什么原因hcv9jop7ns5r.cn 无名指戴戒指是什么意思hcv7jop9ns6r.cn
贼不走空什么意思hcv8jop8ns2r.cn 男羊配什么属相最好hcv9jop7ns2r.cn 梦见飞机是什么意思fenrenren.com 低盐饮食有利于预防什么疾病hcv8jop2ns1r.cn 什么叫柞蚕丝hcv9jop8ns2r.cn
百度