[英]Database Schema, Default Value is NOW()
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 . 有六个字段: id , 用户名 , 密码 , 级别 , 时间 , 电子邮件 ,但我只想插入其中三个 - 当用户注册时: 用户名 , 密码和电子邮件 。 Rest of them will have default values.
其余的将具有默认值。
Problem is that MySQL throws error: #1067 - Invalid default value for 'time' . 问题是MySQL抛出错误: #1067 - 'time'的默认值无效 。 Any ideas, guys?
伙计们,有什么想法?
Use CURRENT_TIMESTAMP, and change the column to timestamp. 使用CURRENT_TIMESTAMP,并将列更改为时间戳。
It does the same, but works (as long as you can live with timestamp) - it is a limitation. 它也是这样,但是有效(只要你可以使用时间戳) - 这是一个限制。
Discussions: 讨论:
So your create becomes 所以你的创造成了
CREATE TABLE `users` (
`id` int( 8 ) unsigned AUTO_INCREMENT,
`username` varchar( 255 ),
`password` varchar( 40 ),
`level` tinyint( 1 ) unsigned DEFAULT 1,
`time` timestamp DEFAULT CURRENT_TIMESTAMP,
`email` varchar( 255 ),
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
we can't make the "DEFAULT CURRENT_TIMESTAMP" when my attribute is "datetime" so we will change our attribute to "TIMESTAMP" like: 当我的属性为“datetime”时,我们无法生成“DEFAULT CURRENT_TIMESTAMP”,因此我们将属性更改为“TIMESTAMP”,如:
time
* datetime* DEFAULT CURRENT_TIMESTAMP, time
* datetime * DEFAULT CURRENT_TIMESTAMP,
should be...
time
TIMESTAMP DEFAULT CURRENT_TIMESTAMP, time
TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
您需要使用TimeStamp
而不是DateTime
`time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
In general, MySQL does not support functions as default values. 通常,MySQL不支持函数作为默认值。 There's only one exception as far as I know: you can assign
CURRENT_TIMESTAMP
to a TIMESTAMP
column. 据我所知,只有一个例外:您可以将
CURRENT_TIMESTAMP
分配给TIMESTAMP
列。 So you need to either change your column type (being aware that TIMESTAMP
is not as powerful as DATETIME
) or rely on a trigger to fill the value. 因此,您需要更改列类型(意识到
TIMESTAMP
不如DATETIME
强大)或依赖触发器来填充值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.