sql - MySQL slug function on insert -
i have table slug field. it's possible populate automatically field function manipulation of field?
something like:
insert people('name','slug') values ('xxxx',slug(name));
are looking this?
create table people (`name` varchar(128), `slug` varchar(128)); -- it's not real function it's oversimplified example -- need implement own logic create function name_slug(name varchar(128)) returns varchar(128) return lower(replace(name, ' ', '-')); create trigger tg_people_insert before insert on people each row set new.slug = name_slug(new.name); insert people (`name`) values ('jhon doe'),('ian martin louis'), ('mark lee'); select * people;
output:
| name | slug | --------------------------------------- | jhon doe | jhon-doe | | ian martin louis | ian-martin-louis | | mark lee | mark-lee |
here sqlfiddle demo
Comments
Post a Comment