Mysql trigger and stored procedure Test

Source: Internet
Author: User


Mysql trigger and stored procedure Test 1. to test the trigger and stored procedure, first CREATE a simple TABLE: www.2cto.com create table 'airuser' ('userid' int (11) not null AUTO_INCREMENT, 'username' varchar (128) not null, primary key ('userid') ENGINE = InnoDB default charset = utf8 2. CREATE a record TABLE: create table 'airuser _ record '('id' int (11) not null AUTO_INCREMENT, 'username' varchar (45) default null, 'edittime' timestamp null default null, 'edittype' varchar (45) default null, primary key ('id') ENGINE = InnoDB default charset = utf8 3. write a TRIGGER for the INSERT operation: drop trigger insert_trigger; delimiter | create trigger insert_trigger before insert on airuserFOR each row begininsert into airuser_record SET username = NEW. username, edittime = now (), edittype = 'insert'; END; | show triggers; 4. stored procedure for batch insert: DROP procedure createUsers; delimiter | create procedure createUsers (IN count int) begindeclare I int; set I = 0; while I <count doinsert into airuser set username = concat ('user _ ', I); set I = I + 1; end while; end; | show procedure status; 5. call the stored procedure to verify that the stored procedure is working and that the trigger can be correctly triggered before the record is inserted: call createUsers (10); 6. finally, the record table is inserted for verification again: SELECT * FROM mars_jpa.airuser_record;

 

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.