Use of MySQL triggers

Source: Internet
Author: User

Original requirements:

Number field: L + Month day + 10 digit serial number, for example: L201502090000000001 the second day of data 10-bit serial number is starting from 1

The test data tables and triggers are built as follows:

set foreign_key_checks=0;-- ------------------------------ Table structure for  gztest-- ----------------------------drop table if exists  ' gztest '; create table  ' Gztest '   (   ' id '  int (one)  not null auto_increment,    ' code '  bigint  DEFAULT NULL,   ' content '  char (255)  default  NULL,  PRIMARY KEY  (' id '))  engine=innodb auto_increment=0 default  CHARSET=utf8;DROP TRIGGER IF EXISTS  ' GetCode ';D elimiter ;; create trigger  ' GetCode '  BEFORE INSERT ON  ' gztest '  for each row  BEGINdeclare newcode BIGINT; Set newcode= (select  ' code '  FROM gztest WHERE  ' Code '  like concat (date_ FORMAT (Curdate (), '%y%m%d '), '% ')  ORDER BY  ' code '  desc limit 1); if newcode  THEN    SET NEW.code=newcode+1; Else  set new.code=concat (Date_format (Curdate (), '%y%m%d '), ' 0000000001 '); end if; END;;D elimiter ;


This article is from the "connected with the Internet" blog, please be sure to keep this source http://suiwnet.blog.51cto.com/2492370/1613398

Use of MySQL triggers

Related Article

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.