gpt4 book ai didi

mysql - 为什么 MySQL 符号 "UNIQUE field (field)"在 MySQL 5.7.21 中导致错误?

转载 作者:太空宇宙 更新时间:2023-11-03 11:32:45 25 4
gpt4 key购买 nike

今天我在处理 mysql query 时遇到了一个新问题适用于 10.1.19-MariaDB localhost,但不适用于 MySQL 5.7.21-0ubuntu0.16.04.1-log:

CREATE TABLE testing (
pageid INT UNSIGNED NOT NULL AUTO_INCREMENT,
position SMALLINT UNSIGNED NOT NULL,
PRIMARY KEY (pageid),
UNIQUE position (position)
) ENGINE=InnoDB CHARSET=utf8

在 MySQL 5.6 中它没有任何问题(fiddle),但是在 MySQL 5.7.21(fiddle)中它抛出:

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 'position (position)) ENGINE=InnoDB CHARSET=utf8' at line 1

我找到了一个解决方案,将 UNIQUE position (position) 替换为 UNIQUE (position)

但我想知道,潜在的问题是什么,为什么它与其他数据库系统一起工作,我不确定我的解决方案是否正确。

最佳答案

MySQL 不允许创建与列同名的索引

以下代码适用于 MySQL ( fiddle )

CREATE TABLE testing (
pageid INT UNSIGNED NOT NULL AUTO_INCREMENT,
position SMALLINT UNSIGNED NOT NULL,
PRIMARY KEY (pageid),
UNIQUE idx_position (position)
) ENGINE=InnoDB CHARSET=utf8

关于mysql - 为什么 MySQL 符号 "UNIQUE field (field)"在 MySQL 5.7.21 中导致错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48462847/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com