使用一个外键创建两个表时出现约束错误 MySQL 5.6
constraint error MySQL 5.6 while creating two tables with one foreign key
我需要一些关于下面 SQL 代码的帮助。我正在使用 MySQL 5.6:
CREATE TABLE REGION (
REGION_CD varchar(200),
REGION_NAME Text (50),
Constraint REGION_PK Primary Key (REGION_CD(200)));
CREATE TABLE CUSTOMER (
CUST_ID long (50)
CUST_NAME Text (50),
REGION_CD varchar (2),
Constraint Customer_PK Primary Key
(CUST_ID(50)),
Constraint C_REGION_CD_FK
Foreign Key (REGION_CD)
References REGION);
这是我的错误信息:
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 '(50) CUST_NAME Text (50), REGION_CD varchar (2), Constraint
Customer_PK Pr' at line 2
外键关系中涉及的字段必须具有相同的类型定义。你没有那个:
REGION_CD varchar(200),
REGION_CD varchar (2),
"foreign" 字段可以为空,而父字段不能,但类型和大小定义必须相同。
原因:由于您的父记录有更长的字段限制,您可能有类似 ab12
、ab34
、ab56
的内容。如果您的子记录中包含 ab
,那么 12
、34
、56
变体中的哪些应该匹配?
我需要一些关于下面 SQL 代码的帮助。我正在使用 MySQL 5.6:
CREATE TABLE REGION (
REGION_CD varchar(200),
REGION_NAME Text (50),
Constraint REGION_PK Primary Key (REGION_CD(200)));
CREATE TABLE CUSTOMER (
CUST_ID long (50)
CUST_NAME Text (50),
REGION_CD varchar (2),
Constraint Customer_PK Primary Key
(CUST_ID(50)),
Constraint C_REGION_CD_FK
Foreign Key (REGION_CD)
References REGION);
这是我的错误信息:
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 '(50) CUST_NAME Text (50), REGION_CD varchar (2), Constraint Customer_PK Pr' at line 2
外键关系中涉及的字段必须具有相同的类型定义。你没有那个:
REGION_CD varchar(200),
REGION_CD varchar (2),
"foreign" 字段可以为空,而父字段不能,但类型和大小定义必须相同。
原因:由于您的父记录有更长的字段限制,您可能有类似 ab12
、ab34
、ab56
的内容。如果您的子记录中包含 ab
,那么 12
、34
、56
变体中的哪些应该匹配?