Triggers use Mysql

table of Contents

First, what triggers are?

  Said trigger, what is it stored procedure?

  What is the difference between the trigger with a stored procedure is it?

Second, the basic syntax of the trigger

Third, abnormal scene


First, what triggers are?

Is available to programmers and data analysts to ensure the data integrity of a method, it is related to table event special of the stored procedure , its execution is not called by the program, not manually start, but is triggered by an event , such as when a table operation (insert, delete, update) will activate its execution (simply put, is: you execute a sql statement, sql implementation of this statement will be automatically triggered to perform other sql statement ). Triggers are often used to enhance data integrity constraints and business rules and so on.

The main role is to implement complex thereof with reference to the primary and foreign keys can not guarantee the integrity and data consistency . It is possible for the relevant database tables modified cascade, CHECK constraints force than the more complex data integrity, and custom action message, maintenance state before and after the non-normalized data and comparison data modification. CHECK constraints different trigger may refer to other columns in the table. Using triggers to implement complex referential integrity under the following conditions; mandatory integrity between the data. When you create a multi-line trigger, when the insert, update, delete multiple rows of data, you must write a trigger handle multiple rows of data. Perform cascading update or cascade delete such action. Cascade modify database of all related tables. Revocation or rollback violate referential integrity of the operation, to prevent illegal to modify the data.

  Said trigger, what is it stored procedure?

Stored procedure (Stored Procedure) is a large database system, in order to complete a specific set of functions set of SQL statements , it is stored in the database, a permanent after compilation , the user specifies the name of the stored procedure and the given parameters (if the stored procedure with parameters) to execute it. A stored procedure is an important database objects . Using a particularly large amount of data in the case where the stored procedure can achieve the speed of efficiency improvement .

  What is the difference between the trigger with a stored procedure is it?

The main difference between the trigger and stored procedure that they run. Stored procedures must have a user, application or triggers to display the invoked and executed , and when the trigger is the occurrence of a certain time, automatically activated or executed , regardless of the connection with the user in a database, or an application. When a row is inserted, updated or deleted when the trigger was executed, but also depends on how the trigger is created, use an update trigger UPDATE occurs when using an insert trigger occurs when the INSERT, DELETE occur when using a delete trigger.

Want to know the difference between stored procedures and functions do? Welcome to stamp -> difference stored procedures and functions

Second, the basic syntax of the trigger

        1. Create Trigger

drop trigger if exists databaseName.tri_Name;  
create trigger tri_Name  -- tri_Name代表触发器名称
tirgger_time trigger_event on tableName  -- tirgger_time为触发时机,可选值有after/before,trigger_event为触发事件,可选值有insert/update/delete
for each row   -- 这句话在mysql是固定的,表示任何一条记录上的操作满足触发事件都会触发该触发器。 
begin  
    sql语句;  
end

        2. Specific examples

 INSERT type flip-flop

DROP TRIGGER `insert_tri`;

CREATE DEFINER=`root`@`localhost` TRIGGER `insert_tri` BEFORE INSERT ON `pgz_demo`
FOR EACH ROW BEGIN
	SET NEW.PASSWORD = '123456'; -- NEW用来表示将要(before)或已经(after)插入的新数据。
END;
--该触发器的意思是:对表pgz_demo进行插入操作时,在执行插入之前把这条数据的PASSWORD字段值置为'123456'

 DELETE type flip-flop

CREATE TRIGGER `delete_tri` AFTER DELETE ON `pgz_demo`
FOR EACH ROW
BEGIN
	DELETE FROM pgz_test WHERE id = old.usid;  -- old用来表示将要或已经被删除的原数据。
END
--该触发器的意思是:对表`pgz_demo`进行删除操作时,在执行删除之后同时删除表`pgz_test`中字段id的值与`pgz_demo`表中删除数据的usid相同的数据

 UPDATE type flip-flop

CREATE TRIGGER update_tri BEFORE UPDATE ON pgz_demo FOR EACH ROW
BEGIN

SET new.version = old.version + 1; -- old用来表示将要或已经被修改的原数据,new用来表示将要或已经修改为的新数据

END

View Trigger

show triggers from databaseName;

Delete Trigger

drop trigger if exists databaseName.tri_Name;

Third, abnormal scene

Precautions:

  1. New updates are not allowed in the trigger row

  2. In the event of adding INSERT trigger for a table can not add new data to the current operating table

  3. For a table, with the same operating time and multiple trigger events (ie: the same table can not have the same trigger trigger timing and triggering events)

Error demonstration:

1. After the trigger is not allowed to update the new line

CREATE TRIGGER `test_update_update` AFTER UPDATE ON `pgz_test` FOR EACH ROW
BEGIN

SET new.pid = 1; --在触发器后不允许跟新新行

END

2. In the event of adding INSERT trigger for a table can not add new data to the current operating table

CREATE TRIGGER `test_add_add` BEFORE INSERT ON `pgz_test`
FOR EACH ROW 
BEGIN
	INSERT INTO pgz_test VALUES(1, 2);  --INSERT事件的触发器进行了本表插入操作。
END

 

Published an original article · won praise 1 · views 52

Guess you like

Origin blog.csdn.net/Mr_Andyquan/article/details/104205987