英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论