900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > MySQL数据库getdate_MySQL数据库 DateTime 默认值是不是用getdate

MySQL数据库getdate_MySQL数据库 DateTime 默认值是不是用getdate

时间:2021-04-14 12:13:12

相关推荐

MySQL数据库getdate_MySQL数据库 DateTime 默认值是不是用getdate

MySQL数据库SQL建表语句:

CREATE TABLE `Users` (

`UserID` int NOT NULL AUTO_INCREMENT ,

`UserName` varchar(50) NOT NULL ,

`PassWord` varchar(50) NOT NULL ,

`Created` datetime NOT NULL DEFAULT ‘getdate()’ ,

`Type` int NOT NULL DEFAULT 0 ,

PRIMARY KEY (`UserID`)

);

执行后提示错误:#1067 – Invalid default value for ‘Created’

原因解析:MySQL 中,默认值无法使用函数

也就是你无法 设置某一列,默认值是 NOW () 这样的处理

如果需要 某列的默认值为 当前数据库时间,那么可以使用 TIMESTAMP 数据类型。插入的时候,填写 null 即可。

mysql> create table testA ( dt TIMESTAMP );

Query OK, 0 rows affected (0.09 sec)

mysql> insert into testA VALUES( null );

Query OK, 1 row affected (0.01 sec)

mysql> insert into testA VALUES( null );

Query OK, 1 row affected (0.08 sec)

mysql> select * from testA;

+---------------------+

| dt |

+---------------------+

| -10-15 20:30:35 |

| -10-15 20:30:36 |

+---------------------+

2 rows in set (0.00 sec)

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明MySQL数据库 DateTime 默认值是不是用getdate!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。