MySQL Triggers - Year-Month(YYMM) Prefix + Auto Increment ID -


i saw post explaining use of triggers create prefixed ids
how make mysql table primary key auto increment prefix

http://sqlfiddle.com/#!2/0ed88/1

i new triggers , find out if there's way have prefix year-month (yymm) instead of preset 4 letters "lhpl" written in sqlfiddle?

much appreciated! lots learn in mysql journey!

    create table table1_seq     (       id int not null auto_increment primary key     )|      create table table1     (       id varchar(7) not null primary key default '0', name varchar(30)     )|       create trigger tg_table1_insert     before insert on table1     each row     begin       insert table1_seq values (null);       set new.id = concat('lhpl', lpad(last_insert_id(), 3, '0'));     end |       insert table1 (name) values ('jhon'), ('mark')| 

replace

'lhpl' 

with

date_format(now(),'%y%m') 

the now() , date_format() functions documented here:

reference: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html


Comments

Popular posts from this blog

jQuery Mobile app not scrolling in Firefox -

c++ - How to add Crypto++ library to Qt project -

php array slice every 2th rule -