mysql的insert语句怎么插入当前时间?
比如有一张表users,有id
,name
,eamil
,password
,created_at
,updated_at
字段。
我想插入一条数据,这样写:
INSERT INTO users VALUES (Null, 'xiaoming','xiaoming@example.com','ffahifuhaif','','' );
问题:
1、id是自增长的,上面语句中id
字段写Null正确么?
2、created_at
,updated_at
两个字段要插入当前时间,应该怎么做?
怪我咯2017-04-17 16:38:33
1. Your grammar is wrong. INSERT INTO table_name (column 1, column 2,...) VALUES (value 1, value 2,....)
ID does not need to be written in, it will be assigned automatically.
2. The time field can also automatically insert the current time with default value.
http://s11.sinaimg.cn/mw690/0...
The field type is set to timestamp, and the default value is CURRENT_TIMESTAMP
When inserting data, the time will be automatically saved in this field.
怪我咯2017-04-17 16:38:33
For fields set to auto_increase, it is useless for you to set other values.
If you use php, insert the current time using NOW();
PHP中文网2017-04-17 16:38:33
You can leave unnecessary fields blank
INSERT INTO users(name,eamil,password) VALUES ( 'xiaoming','xiaoming@example.com','ffahifuhaif');
There are two other time issues, which can be set like this in the database:
created_at
default value is set to CURRENT_TIMESTAMP
created_at
默认值设置为 CURRENT_TIMESTAMP
updated_at
默认值设置为 CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
updated_at
The default value is set to CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
巴扎黑2017-04-17 16:38:33
First determine what data type your time is stored in. Is it int? date? Or timestamp? datetime? Then process it according to the specific type