Create a MySql stored procedure and trigger (Lite version) _ MySQL

Source: Internet
Author: User
Create MySql stored procedures and triggers (Lite version) bitsCN.com
1. CREATE a stored procedure delimiter // drop procedure if exists 'proc _ test' // create procedure 'proc _ test' (TABLE_NAME VARCHAR (20), num int) begin select * FROM TABLE_NAME limit num; END // DELIMITER;
CALL the stored procedure: CALL PROC_TEST ('user', 20); delete the stored procedure: drop procudure productpricing 2. create a trigger ~~ Syntax ~~ CREATE TRIGGER <触发器名称> -- The trigger must have a name up to 64 characters, which may be followed by a separator. it is similar to the naming of other objects in MySQL. {BEFORE | AFTER} -- Trigger has the execution time setting: it can be set to BEFORE or AFTER an event occurs. {INSERT | UPDATE | DELETE} -- trigger events can also be set: they can be triggered during insert, update, or delete execution. ON <表名称> -- A trigger belongs to a table. When an insert, update, or delete operation is performed on the table, the trigger is activated. we cannot schedule two triggers for the same event of the same table. For each row -- trigger execution interval: the for each row clause notifies the trigger to execute an action every ROW, instead of executing an action FOR the entire table. <触发器sql语句> -- 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 functions. -- You must have considerable permissions to CREATE a TRIGGER. if you are a Root user, this is enough. This is different from the SQL standard. Create a TRIGGER: t_afterinsert_on_tab1. function: After the tab1 table record is added, the record is automatically added to the drop trigger if exists 't_ afterinsert_on_tab1 'table in tab2 '; create trigger t_afterinsert_on_tab1 after insert on 'tab1' for each row begin insert into tab2 (tab2_id) values (new. tab1_id); END; create trigger: t_afterdelete_on_tab1: delete the records in table tab1 and delete the corresponding records in table tab2 automatically. 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; delete TRIGGER: drop trigger [schema_name.] trigger_name; author: qhwbitsCN.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.