MySQL trigger and stored procedure instance demonstration

Source: Internet
Author: User

The following articles mainly introduce MySQL triggers, stored procedures, and examples of functions and views. The following describes the triggers, stored procedures, and specific operation solutions of functions and views, I hope this will help you in your future studies.

Demo of MySQL triggers, stored procedures, and functions and views:

0. The test database has the userinfo user information table and the userinfolog user information log table.

1. When a new record is created for the userinfo table, the MySQL trigger adds the new log to userinfolog.

2. Create a stored procedure for adding records to the userinfo table

3. Based on the date of birth field in the userinfo table, we will create a user-defined function for simple age calculation.

4. Create a userinfo view to call the age function.

0. Prepare related tables

 
 
  1. MySQL> use test;  
  2. MySQL> create table userinfo(userid int,username varchar(10),userbirthday date);  
  3. MySQL> create table userinfolog(logtime datetime,loginfo varchar(100));  
  4. MySQL> describe userinfo; 

1. MySQL triggers

 
 
  1. MySQL> delimiter |  
  2. MySQL> create trigger beforeinsertuserinfo  
  3. -> before insert on userinfo  
  4. -> for each row begin  
  5. -> insert into userinfolog values(now(),CONCAT(new.userid,new.username));  
  6. -> end;  
  7. -> |  
  8. MySQL> delimiter ;  
  9. MySQL> show triggers; 

2. Stored Procedure

 
 
  1. MySQL> delimiter //  
  2. MySQL> create procedure spinsertuserinfo(  
  3. -> puserid int,pusername varchar(10)  
  4. -> ,puserbirthday date  
  5. -> )  
  6. -> begin  
  7. -> insert into userinfo values(puserid,pusername,puserbirthday);  
  8. -> end;  
  9. -> //  
  10. MySQL> show procedure status like 'spinsertuserinfo';  
  11. MySQL> call spinsertuserinfo(1,'zhangsan',current_date);  
  12. MySQL> select * from userinfo; 

3. User-Defined Functions

 
 
  1. MySQL> update userinfo  
  2. -> set userbirthday='2000.01.01' 
  3. -> where userid='1';  
  4. MySQL> drop function if exists fngetage;  
  5. MySQL> delimiter //  
  6. MySQL> create function fngetage(pbirthday date)  
  7. -> returns integer  
  8. -> begin  
  9. -> return year(now()) - year(pbirthday);  
  10. -> end  
  11. -> // 

4. View

 
 
  1. MySQL> create view viewuserinfo  
  2. -> as select * ,fngetage(userbirthday) as userage from userinfo;  
  3. MySQL> select * from viewuserinfo; 

Clear log records

 
 
  1. MySQL> truncate table userinfolog;  
  2. MySQL> delete from userinfolog; 

The above content is an introduction to the MySQL trigger stored procedure function view. I hope you will find some gains.

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.