Mysql trigger simple instance

Compared with other large databases such as Oracle, DB2, SQL Server, etc., MySQL has its own shortcomings, but this has not reduced its popularity. For the average individual user and small and medium-sized enterprise, MySQL provides more than enough functionality, and because MySQL is open source software, it can greatly reduce the total cost of ownership.

Linux as the operating system, Apache or Nginx as the web server, MySQL as the database, PHP/Perl/Python as the server-side script interpreter. Since these four softwares are free or open source software (FLOSS), you can use this method to build a stable, free website system without spending a penny (except for labor costs), which is called "LAMP" by the industry. Or "LNMP" combination.

Mysql trigger simple instance

MySQL trigger instance

CREATE TRIGGER "Trigger Name" -- The trigger must have a name of up to 64 characters, possibly followed by a separator. It is basically similar to the way other objects in MySQL are named.

{ BEFORE | AFTER } -- The trigger has a time setting for execution: it can be set before or after the event occurs.

{ INSERT | UPDATE | DELETE } -- The same can be set for triggered events: they can be triggered during the execution of insert, update or delete.

ON Table Name -- A trigger belongs to a table: the trigger is activated when an insert, update, or delete operation is performed on the table. We can't schedule two triggers for the same event in the same table.

FOR EACH ROW -- Trigger execution interval: The FOR EACH ROW clause tells the trigger to perform an action every other row instead of once for the entire table.

Trigger SQL Statement -- The trigger contains the SQL statement to be triggered: the statement here can be any legal statement, including compound statements, but the statements here are subject to the same restrictions as the function.

-- You must have considerable permissions to create a trigger (CREATE TRIGGER), which is sufficient if you are already a root user. This is different from the SQL standard.

~~Instance~~

Example1:

Create table tab1

DROP TABLE IF EXISTS tab1;

CREATE TABLE tab1(

Tab1_id varchar(11)

);

Create table tab2

DROP TABLE IF EXISTS tab2;

CREATE TABLE tab2(

Tab2_id varchar(11)

);

Create a trigger: t_afterinsert_on_tab1

Role: automatically increase the record to the tab2 table after adding the tab1 table record

DROP TRIGGER IF EXISTS t_afterinsert_on_tab1;

CREATE TRIGGER t_afterinsert_on_tab1

AFTER INSERT ON tab1

FOR EACH ROW

BEGIN

Insert into tab2(tab2_id) values(new.tab1_id);

END;

have a test

1INSERT INTO tab1(tab1_id) values('0001');

Look at the results

SELECT * FROM tab1;

SELECT * FROM tab2;

Example2:

Create a trigger: t_afterdelete_on_tab1

Role: delete the tab1 table record automatically delete the corresponding record in the tab2 table

DROP TRIGGER IF EXISTS t_afterdelete_on_tab1;

CREATE TRIGGER t_afterdelete_on_tab1

AFTER DELETE ON tab1

FOR EACH ROW

BEGIN

Delete from tab2 where tab2_id=old.tab1_id;

END;

have a test

1DELETE FROM tab1 WHERE tab1_id='0001';

Look at the results

SELECT * FROM tab1;

SELECT * FROM tab2;

Metal USB Flash Drive

Bulk usb drives for sale,Bulk USB Drives - Premium USB,custom USB drives,Amazon flash drive,USB memory direct,Promotional USB drives

Shenzhen Konchang Electronic Technology Co.,Ltd , https://www.konchangs.com