MariaDB vs MySQL: 列定义中的默认表达式不起作用

huangapple go评论52阅读模式
英文:

MariaDB vs MySQL: default expression in column definition not working

问题

CREATE TABLE creationtime (
view VARCHAR(255) NULL DEFAULT NULL COLLATE 'utf8mb4_general_ci',
start DATETIME NULL DEFAULT NULL,
stop DATETIME NULL DEFAULT NULL,
diff TIME NOT NULL DEFAULT timediff(stop, start)
)
COMMENT='running time'
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB;

英文:

I am using a table in MySQL to store and calculate time differences when generating materialized views.

CREATE TABLE `creationtime` (
	`view` VARCHAR(255) NULL DEFAULT NULL COLLATE 'utf8mb4_general_ci',
	`start` DATETIME NULL DEFAULT NULL,
	`stop` DATETIME NULL DEFAULT NULL,
	`diff` TIME NOT NULL DEFAULT timediff(`stop`,`start`)
)
COMMENT='running time'
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB
;

This works perfectly: Start and stop time values are stored and when I open the table the time differences are calculated on the fly.

Now I wanted to use the same syntax on a MariaDB-Server (MariaDB version 10.1.48 on Debian Linux 9). But I am running into an error.

> /* SQL-Error (1064): You have an error in your SQL syntax; check the
> manual that corresponds to your MariaDB server version for the right
> syntax to use near 'timediff(`stop`,`start`) ) COMMENT='running time'
> COLLATE='utf8mb4_general_ci' at line 5 */

I don't get it. Where is the error?

答案1

得分: 1

https://mariadb.com/kb/en/create-table/#default-column-option 提到:

DEFAULT 子句在 MariaDB 10.2.1 中得到增强。一些增强包括:

  • DEFAULT 子句现在可以与表达式或函数一起使用。

由于您当前使用的是 MariaDB 10.1,您需要升级以获得此功能。由于 MariaDB 10.1 在2020年10月已终止支持,您本应早就升级了。版本10.2也已终止支持,10.3几乎要结束支持(下个月,即2023年5月)。

确保您阅读我上面链接的知识库页面,以获取有关DEFAULT语法的示例。与MySQL中表达式默认值的语法存在细微差异。

英文:

https://mariadb.com/kb/en/create-table/#default-column-option says:

> The DEFAULT clause was enhanced in MariaDB 10.2.1. Some enhancements include:
>
> - The DEFAULT clause can now be used with an expression or function.

Since you are currently using MariaDB 10.1, you need to upgrade to get this feature. Since MariaDB 10.1 passed its end of support in October 2020, you should have upgraded long ago. Version 10.2 is also EOL, and 10.3 is almost at its end of support (next month, May 2023).

Make sure you read the knowledgebase page I linked to above for examples of the DEFAULT syntax. There are subtle differences from the syntax for expression defaults in MySQL.

huangapple
  • 本文由 发表于 2023年4月13日 16:12:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76003133.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定