mysql - Insert current date on insert -


i have table following structure:

+----+-------------+----------------+------------+------------+ | id | column | column |  inserted  |   edited   | +----+-------------+----------------+------------+------------+ | 1  | ...         | ...            | 2014-08-15 | 2016-03-04 | | 2  | ...         | ...            | 2015-09-16 | 2016-10-07 | | 3  | ...         | ...            | 2016-10-17 | 2016-11-16 | +----+-------------+----------------+------------+------------+ 

when new entry inserted, current date should added column inserted. should never changed.
when entry edited, current date should added column edited , should update every time entry edited.

my approach define datatype date in both cases , change standard value curdate(). instead, inserts curdate() string.

update example query:

create table `test`.`testtab`    (       `id`             int not null auto_increment,       `some column`    varchar(100) null,       `another column` varchar(100) null,       `inserted`       varchar(100) null default 'curdate()',       `edited`         varchar(100) null default 'curdate()',       primary key (`id`)    )  engine = innodb;  

though, i'm not sure data types.

based on needs work you:

create table `test`.`testtab`    (       `id`             int not null auto_increment,       `some column`    varchar(100) null,       `another column` varchar(100) null,       `inserted`       datetime default   current_timestamp,       `edited`         datetime on update current_timestamp,       primary key (`id`)    )  engine = innodb;  

then while processing extract date part:

date_format(datetime, '%y-%m-%d') 

you can use trigger workaround set datetime field now() new inserts:

create trigger `triggername` before insert on  `tablename`  each row  set new.datetimefield = now() 

it should work updates too


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -