MySQL两列时间戳默认NOW值ERROR 1067

发布于 2024-08-05 09:49:06 字数 995 浏览 8 评论 0原文

我有如下所示的表格。为了解决 MySQL 的一个默认 now 列限制,我使用了如图所示的提示 此处

CREATE  TABLE IF NOT EXISTS mytable (
  id INT NOT NULL AUTO_INCREMENT ,
  create_date TIMESTAMP NULL DEFAULT '0000-00-00 00:00:00' ,
  update_date TIMESTAMP NULL DEFAULT NOW() ON UPDATE NOW() ,
  PRIMARY KEY (`parti_id`) )
ENGINE = InnoDB;

我的 sql_mode 不包括 NO_ZERO_DATE 所指出的 这里 我的输出:

mysql> SELECT @@sql_mode;
+------------+
| @@sql_mode |
+------------+
|            | 
+------------+
1 row in set (0.00 sec)

它仍然给出错误,如下所示:

ERROR 1067 (42000) at line xx in file: '/myschema.sql': Invalid default value for 'create_date'

我在 Ubuntu 上使用 MySQL 5.1.37

如何我可以修复它吗?谢谢。

I have table as shown below. In order to workaround one default now column restriction of MySQL I used the tip as shown here

CREATE  TABLE IF NOT EXISTS mytable (
  id INT NOT NULL AUTO_INCREMENT ,
  create_date TIMESTAMP NULL DEFAULT '0000-00-00 00:00:00' ,
  update_date TIMESTAMP NULL DEFAULT NOW() ON UPDATE NOW() ,
  PRIMARY KEY (`parti_id`) )
ENGINE = InnoDB;

My sql_mode does not include NO_ZERO_DATE as pointed here my output :

mysql> SELECT @@sql_mode;
+------------+
| @@sql_mode |
+------------+
|            | 
+------------+
1 row in set (0.00 sec)

It is still giving the error as shown below:

ERROR 1067 (42000) at line xx in file: '/myschema.sql': Invalid default value for 'create_date'

I use MySQL 5.1.37 on Ubuntu

How can I fix it? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

凉墨 2024-08-12 09:49:06

每个表只能有一个时间戳列,默认为 CURRENT_TIMESTAMPNOW()。这是 MySQL 中一个众所周知的错误。

为了解决这个问题,请将创建的列的默认值设置为有效的时间戳值,然后将时间戳插入到 CRUD 应用程序代码中。
使用 NOW()CURRENT_TIMESTAMP 作为更新后的列默认值。

参考资料:
http://dev.mysql.com/doc/refman/5.1/en /timestamp.html

为了进一步说明 MySQL 在这方面的缺点,请考虑以下代码:

CREATE TABLE testing_timestamps (
  id INT NOT NULL AUTO_INCREMENT,
  pk_id INT NOT NULL,
  col1 TIMESTAMP DEFAULT 0,
  col2 TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY(id)
);

delimiter $
CREATE TRIGGER testing_timestamps_trigger
  AFTER INSERT ON testing_timestamps
  FOR EACH ROW 
  BEGIN
    UPDATE testing_timestamps SET col1 = NOW() WHERE id = MAX(id);
  END;
$
delimiter ;

INSERT INTO testing_timestamps (id) VALUES (0);

输出将显示:

mysql> INSERT INTO testing_timestamps (id) VALUES (0);
ERROR 1442 (HY000): Can't update table 'testing_timestamps' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.

This is a bummer,因为在此实例中使用触发器将是一个很好的解决方法。

You can only have one timestamp column that defaults to CURRENT_TIMESTAMP or NOW() per table. This is a well known bug in MySQL.

To overcome this, make your default for the created column a valid timestamp value, then insert the timestamp in your CRUD application code.
Use NOW() or CURRENT_TIMESTAMP for your updated column default.

Reference material:
http://dev.mysql.com/doc/refman/5.1/en/timestamp.html

To further illustrate MySQL's shortcoming in this area, consider the following code:

CREATE TABLE testing_timestamps (
  id INT NOT NULL AUTO_INCREMENT,
  pk_id INT NOT NULL,
  col1 TIMESTAMP DEFAULT 0,
  col2 TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY(id)
);

delimiter $
CREATE TRIGGER testing_timestamps_trigger
  AFTER INSERT ON testing_timestamps
  FOR EACH ROW 
  BEGIN
    UPDATE testing_timestamps SET col1 = NOW() WHERE id = MAX(id);
  END;
$
delimiter ;

INSERT INTO testing_timestamps (id) VALUES (0);

The output from this will display:

mysql> INSERT INTO testing_timestamps (id) VALUES (0);
ERROR 1442 (HY000): Can't update table 'testing_timestamps' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.

This is a bummer because using a trigger in this instance would be a good work around.

忘羡 2024-08-12 09:49:06

事实上,兰迪的代码被破坏了。它不会起作用,因为它是一个突变触发器。您无法更新启动更新的表。例如,如果您要更新表 SESSIONS,则无法使用 UPDATE、INSERT 或 DELETE 语句在触发器中修改该表。允许您修改触发器所附加的表的唯一方法是使用“NEW”或“OLD”前缀,如下所示。 (当然,你可以随意更新其他表。)这里是一个如何克服op描述的问题的例子。

create table sessions (
    id integer not null,
    created timestamp not null default 0,
    updated timestamp not null default 0
);

delimiter //
create trigger bifer_sessions_ts before insert on sessions for each row
begin
    set new.created = now();
    set new.updated = now();
end;
//
create trigger bufer_sessions_ts before update on sessions for each row
begin
    set new.updated = now();
end;
//
delimiter ;

Actually, Randy's code is broken. It won't work because it is a mutating trigger. You can't update the table that initiated the updating. For example, if you are updating the table SESSIONS, you can't the go modify the table in your trigger using an UPDATE, INSERT or DELETE statement. The only way you are allowed to modify the table to which the trigger is attached is to use the "NEW" or "OLD" prefixes as demonstrated below. (Of course, you are allowed to update other tables at will.) Here is an example of how to overcome the problem described by the op.

create table sessions (
    id integer not null,
    created timestamp not null default 0,
    updated timestamp not null default 0
);

delimiter //
create trigger bifer_sessions_ts before insert on sessions for each row
begin
    set new.created = now();
    set new.updated = now();
end;
//
create trigger bufer_sessions_ts before update on sessions for each row
begin
    set new.updated = now();
end;
//
delimiter ;
自演自醉 2024-08-12 09:49:06

要执行此操作并使其正常工作:

col1 TIMESTAMP DEFAULT 0,

col2 TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 

您不需要 col1 的触发器,只需确保查询中的值为 NULL 即可。答案就在认证指南中。

To do this and have it work:

col1 TIMESTAMP DEFAULT 0,

col2 TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 

You don't need a trigger for col1, just ensure the value is NULL in your query. The answer is in the certification guide.

看轻我的陪伴 2024-08-12 09:49:06

MySQL 5.6.1 之前的版本不支持同一个表中的默认 TIMESTAMP。

在 MySQL 5.6.+ 版本之后,它允许在同一个表中存在两个或多个 TIMESTAMP 列。

您可以尝试更新您的mysql版本

Default TIMESTAMP is not supported in MySQL before versions 5.6.1 in the same table.

After version MySQL 5.6.+ it allows two or more TIMESTAMP columns in same table.

You can try updating your mysql version

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