MySQL date column auto fill with current date

2019-02-16 14:18发布

I have a table where I have a date column. Is there a way for MySQL to auto fill this field whenever I insert a new registry with the current date? Or is this made automatically by default?

P.S.: I'm using PHPMyAdmin

6条回答
等我变得足够好
2楼-- · 2019-02-16 14:49

You can do something like this from the SQL screen

ALTER TABLE `table_name` CHANGE `created_at` `created_at` TIMESTAMP NOT NULL 
查看更多
叼着烟拽天下
3楼-- · 2019-02-16 14:50

You can specify default values for dates similarly to any other column. The syntax looks like this:

my_date DATE DEFAULT '2014-01-19'

查看更多
我命由我不由天
4楼-- · 2019-02-16 15:00

Although it is an old post, maybe this image will help as it is more explicit: (For phpMyAdmin users)

enter image description here

This configuration sets that field with a value like:

2015-12-11 07:50:47

PS: Note that the timestamp will set the time OF your server!! (i.e. the example above got the time from Pacific Time (07:50:47) but it could have been from a Spanish user at 16:50:47 local time) Keep this in mind.

Also, if you already have a "Created Date" you might need another column that updates the modification date whenever there is an update: You only need to set on update CURRENT TIME STAMP in Attributes Field.

Ready to rock!

enter image description here

查看更多
爷、活的狠高调
5楼-- · 2019-02-16 15:00

I realize this may not be a direct answer to the question but I do believe this is the most useable solution.

I highly recommend using a DATETIME or TIMESTAMP data type for the column in question.
If you are utilizing a fairly current version of MySQL, MySQL will do the work for you.

Details:
To be very clear, as of 5.6.5, for both the TIMESTAMP & DATETIME datatypes, you can do the following:

  1. Set a DEFAULT value of the current date & time (using NOW() or one of its aliases such as CURRENT_TIMESTAMP)
    This means every time you insert a new row into this table a TIMESTAMP or DATETIME column with this default will get the current date and time
  2. Set an ON UPDATE constraint that will UPDATE a column to the current date & time when, (you guessed it) the row is updated

Here's how:
An Example in a CREATE TABLE statement:

CREATE TABLE t1 (
ts1 DATETIME ON UPDATE CURRENT_TIMESTAMP
,ts2 DATETIME DEFAULT NOW()
);

Please note that DATETIME can be replaced with TIMESTAMP for effectively the same functionality.
Additionally I suggest the use of the DATETIME data type over TIMESTAMP as DATETIME has a much larger range of dates it can support. It's worth mentioning that TIMESTAMP is smaller for those few cases that matters.
For more details please read my answer here: https://stackoverflow.com/a/26117532/1748266

查看更多
劳资没心,怎么记你
6楼-- · 2019-02-16 15:07

Set Default to in your mySql query

CURRENT_TIMESTAMP
查看更多
聊天终结者
7楼-- · 2019-02-16 15:11

you have to use

now()

function where you want to fill current time.

i.e.:

INSERT INTO  user_rights (`user_id`,`right`,`group_id`,`created_date`) VALUES ( '42',  '160',  '1',  now());
查看更多
登录 后发表回答