Mysql inserts current date only during insert

this is a table structure

id             int
name           varchar
insert_date    date
update_date    TIMESTAMP

Now here is idia, insert_date will be able to insert at the current time, and update_date will insert the current time in each edit or insert, it will update it.

since I could make the default value (insert_date) the current time and date, but when updating it should not change the value (insert_date), if I keep it TIMESTAMP when updating, it will change.

insert_date - insert the current time, only insert only once

considers

+4
source share
5 answers

you can use the NOW()function in the appropriate fields

LINK LINKS

0

DEFAULT CURRENT_TIMESTAMP

ON UPDATE CURRENT_TIMESTAMP

CREATE TABLE IF NOT EXISTS `datetest` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(100) NOT NULL,
      `timestamps_updated` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP,
      `timestamps_inserted` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' DEFAULT CURRENT_TIMESTAMP,

      PRIMARY KEY (`id`)
    ) 
+2

,

insert_date data type date to datetime

Insert into tbl(insert_date) values(now())

$date=date('Y-m-d H:i:s')

Insert into tbl(insert_date) values('$date')

Datetime MySQL?

+1

..

CURTIME()

...

()

....

CURDATE()

..

SELECT: SELECT NOW(), CURDATE(), CURTIME()

- : () CURDATE() CURTIME() 2008-11-11 12:45:34 2008-11-11 12:45:34

, .

+1

Use the type DATETIMEif you want to save the Date and Time , and you can choose the CURRENT_TIMESTAMPdefault value or use it NOW()to get the current date and time (yyyy-mm -dd Hour: minutes: seconds).

0
source

All Articles