Mysql trigger and stored procedure quiz _ MySQL

Source: Internet
Author: User
Mysql trigger and stored procedure test bitsCN.com


Mysql trigger and stored procedure test

1. to test the trigger and stored procedure, first create a simple table:

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 for the insert operation of the 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. Compile an insert trigger:

Drop trigger insert_trigger;

Delimiter |

Create trigger insert_trigger before insert on airuser

FOR EACH ROW BEGIN

Insert into airuser_record SET username = NEW. username, edittime = now (), edittype = 'insert ';

END;

|

Show triggers;

4. write a stored procedure for batch insert:

DROP procedure createUsers;

Delimiter |

Create procedure createUsers (IN count int)

Begin

Declare I int;

Set I = 0;

While I

Insert 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 a record is inserted:

Call createUsers (10 );

6. verify again by inserting a record table:

SELECT * FROM mars_jpa.airuser_record;



BitsCN.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.