我收到一个 mysql create table 语法错误,希望有人能在我失去理智之前发现它。
create table buyer_representations (
Key int not null auto_increment,
Client_ID int not null,
Start_Time varchar(5),
Start_Date date,
End_Date date,
Property_Type varchar(255),
Geographic_Location varchar(255),
Commission varchar(255),
Alternate_Commission varchar(255),
Lease_Commission varchar(255),
Holdover varchar(255),
Clauses varchar(2000),
primary key (Key)
);
ERROR 1064 (42000):您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在“int not null auto_increment”附近使用的正确语法, Client_ID int 不为空, Start_Time varchar(5), 星'在第 2 行
mysql Ver 14.14 Distrib 5.7.29, for Linux (x86_64) using EditLine wrapper
我尝试在第 2 行的末尾添加“主键”以及默认值,并使用“自动增量”切换“非空”......但没有任何效果。
编辑: 我也尝试将 Key 更改为 KeyId,并将 Key 放在引号中 .. 但错误仍然相同。
最终编辑: 将 Key 放在反引号中终于奏效了
create table buyer_representations (
`Key` int not null auto_increment,
Client_ID int not null,
Start_Time varchar(5),
Start_Date date,
End_Date date,
Property_Type varchar(255),
Geographic_Location varchar(255),
Commission varchar(255),
Alternate_Commission varchar(255),
Lease_Commission varchar(255),
Holdover varchar(255),
Clauses varchar(2000),
primary key (`Key`)
);
答案 0 :(得分:3)
Key 是一个保留关键字,重命名它,你应该是金色的。
完整的关键字和保留字列表可以在第 10.3 节关键字和保留字中找到。下面
https://dev.mysql.com/doc/refman/8.0/en/keywords.html
在您的情况下,这应该有效(我将其命名为测试,但将其重命名为适合您情况的任何名称)
create table buyer_representations (
test int not null auto_increment,
Client_ID int not null,
Start_Time varchar(5),
Start_Date date,
End_Date date,
Property_Type varchar(255),
Geographic_Location varchar(255),
Commission varchar(255),
Alternate_Commission varchar(255),
Lease_Commission varchar(255),
Holdover varchar(255),
Clauses varchar(2000),
primary key (test)
);
答案 1 :(得分:1)
问题在于您在表中使用的列名。 第一个列名是 Key。 Key在Mysql中被认为是一个保留字,它具有特殊的含义。这就是为什么会出现错误。
如果你想使用相同的列名,你可以把列名写在``里面。
例如。 创建表buyer_representations ( `Key` int not null auto_increment,
这会正常工作。