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
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
Post a Comment