【问题标题】:Mysql8.0.22 set default value DATE_FORMAT(sysdate() ,'%Y%m%d') errormysql8.0.22设置默认值DATE_FORMAT(sysdate(),'%Y%m%d')报错
【发布时间】:2023-04-01 14:58:01
【问题描述】:

请原谅我糟糕的英语 这是我的sql

create table if not exists `test` (
    `client_id` varchar(18) not null default ' ',
    `begin_date` int not null default DATE_FORMAT(sysdate() ,'%Y%m%d') ,
    `end_date` int not null default DATE_FORMAT(sysdate() ,'%Y%m%d'),
unique index `uk_key` (`client_id` asc)
) engine = InnoDB  default charset = utf8 collate = utf8_bin comment = '';
commit;  

在10.3.18-MariaDB-log中,执行后没有报错 但是在Mysql 8.0.22中,报错

[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DATE_FORMAT(sysdate() ,'%Y%m%d'),
    `end_date` int not nu' at line 

被举报;

我修改了@global.sql_mode 和@sql_mode,但是没有用。

+-----------------------------------------------------------------------+
| @@sql_mode                                                            |
+-----------------------------------------------------------------------+
| STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION |
mysql> select @@global.sql_mode;
+-----------------------------------------------------------------------+
| @@global.sql_mode                                                     |
+-----------------------------------------------------------------------+
| STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION |
+-----------------------------------------------------------------------+

我该怎么办 ????

【问题讨论】:

  • 我想在 mysql 8 whitout 错误中执行这个 sql,就像在 10.3.18-MariaDB-log 中一样

标签: mysql default date-format mysql-8.0


【解决方案1】:

阅读https://dev.mysql.com/doc/refman/8.0/en/data-type-defaults.html

特别是:

在 DEFAULT 子句中指定的默认值可以是文字常量或表达式。除了一个例外,将表达式默认值括在括号内,以区别于文字常量默认值。

因此,在 MySQL 中,与 MariaDB 不同,在将表达式用作 DEFAULT 时,您需要将表达式放在括号内。

例子:

create table if not exists `test` (
    `client_id` varchar(18) not null default ' ',
    `begin_date` int not null default (DATE_FORMAT(sysdate() ,'%Y%m%d')),
    `end_date` int not null default (DATE_FORMAT(sysdate() ,'%Y%m%d')),
unique index `uk_key` (`client_id` asc)
) engine = InnoDB  default charset = utf8 collate = utf8_bin comment = '';

【讨论】:

  • 自 2010 年以来,MariaDB 逐渐与 MySQL 分道扬镳。它现在有很多不同之处,您应该将其视为完全不同的软件产品。如果您在这两种产品上都使用 SQL 代码,则需要阅读这两种产品的 SQL 参考文档。
猜你喜欢
  • 1970-01-01
  • 2021-03-09
  • 2020-11-27
  • 2015-10-12
  • 1970-01-01
  • 1970-01-01
  • 2011-07-07
  • 2021-11-26
  • 2019-03-22
相关资源
最近更新 更多