数据库架构,默认值为 NOW()
我有用户的数据库架构。看起来像...
CREATE TABLE `users` (
`id` int( 8 ) unsigned AUTO_INCREMENT,
`username` varchar( 255 ),
`password` varchar( 40 ),
`level` tinyint( 1 ) unsigned DEFAULT 1,
`time` datetime DEFAULT NOW(),
`email` varchar( 255 ),
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
有六个字段:id、用户名、密码、级别、时间,电子邮件,但我只想插入其中三个 - 当用户注册时:用户名,密码和 >电子邮件。其余的将有默认值。
问题是 MySQL 抛出错误:#1067 - “时间”的默认值无效。伙计们,有什么想法吗?
I have database schema for users. It looks like...
CREATE TABLE `users` (
`id` int( 8 ) unsigned AUTO_INCREMENT,
`username` varchar( 255 ),
`password` varchar( 40 ),
`level` tinyint( 1 ) unsigned DEFAULT 1,
`time` datetime DEFAULT NOW(),
`email` varchar( 255 ),
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
There are six fields: id, username, password, level, time, email, but I want to insert only three of them - when user is registration: username, password and email. Rest of them will have default values.
Problem is that MySQL throws error: #1067 - Invalid default value for 'time'. Any ideas, guys?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 CURRENT_TIMESTAMP,并将列更改为时间戳。
它的作用相同,但有效(只要你可以接受时间戳) - 这是一个限制。
讨论:
所以你的创建变成
Use CURRENT_TIMESTAMP, and change the column to timestamp.
It does the same, but works (as long as you can live with timestamp) - it is a limitation.
Discussions:
So your create becomes
您需要使用
TimeStamp
而不是DateTime
You need to use
TimeStamp
notDateTime
一般来说,MySQL不支持函数作为默认值。据我所知,只有一个例外:您可以将
CURRENT_TIMESTAMP
分配给TIMESTAMP
列。因此,您需要更改列类型(请注意TIMESTAMP
不如DATETIME
强大)或依赖触发器来填充值。In general, MySQL does not support functions as default values. There's only one exception as far as I know: you can assign
CURRENT_TIMESTAMP
to aTIMESTAMP
column. So you need to either change your column type (being aware thatTIMESTAMP
is not as powerful asDATETIME
) or rely on a trigger to fill the value.当我的属性为“datetime”时,我们无法设置“DEFAULT CURRENT_TIMESTAMP”,因此我们将属性更改为“TIMESTAMP”,例如:
time
* datetime* DEFAULT CURRENT_TIMESTAMP,时间
TIMESTAMP 默认 CURRENT_TIMESTAMP,we can't make the "DEFAULT CURRENT_TIMESTAMP" when my attribute is "datetime" so we will change our attribute to "TIMESTAMP" like:
time
* datetime* DEFAULT CURRENT_TIMESTAMP,time
TIMESTAMP DEFAULT CURRENT_TIMESTAMP,