数据库架构,默认值为 NOW()

发布于 2024-10-22 10:57:16 字数 535 浏览 2 评论 0原文

我有用户的数据库架构。看起来像...

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

温柔戏命师 2024-10-29 10:57:16

使用 CURRENT_TIMESTAMP,并将列更改为时间戳。

它的作用相同,但有效(只要你可以接受时间戳) - 这是一个限制。

讨论:

  1. http://bugs.mysql.com/bug.php?id=27645
  2. http://forums.mysql.com/read.php ?61,67640,67771#msg-67771

所以你的创建变成

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;

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:

  1. http://bugs.mysql.com/bug.php?id=27645
  2. http://forums.mysql.com/read.php?61,67640,67771#msg-67771

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;
守不住的情 2024-10-29 10:57:16

您需要使用 TimeStamp 而不是 DateTime

`time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,

You need to use TimeStamp not DateTime

`time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
腹黑女流氓 2024-10-29 10:57:16

一般来说,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 a TIMESTAMP column. 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.

蓝颜夕 2024-10-29 10:57:16

当我的属性为“datetime”时,我们无法设置“DEFAULT CURRENT_TIMESTAMP”,因此我们将属性更改为“TIMESTAMP”,例如:

time * datetime* DEFAULT CURRENT_TIMESTAMP,

should be...

时间 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,

should be...

time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文