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

数据库 码拜 8年前 (2015-12-18) 2508次浏览 0个评论

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                  |
+---------------------+
| 2011-10-15 20:30:35 |
| 2011-10-15 20:30:36 |
+---------------------+
2 rows in set (0.00 sec)

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明MySQL数据库 DateTime 默认值是不是用getdate
喜欢 (1)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!