我在SQL中遇到外键错误。我的mySQL表是
create table if not exists table1(
cust_name varchar(30) not null,
cust_phone char(16) not null,
cust_mail char(30) not null,
cust_address varchar(100),
bills int(10),
primary key (cust_name)
)ENGINE=INNODB;
create table if not exists table2 (
camp_id varchar(30) not null,
advr_id varchar(30) not null,
foreign key (advr_id) references table1 (cust_name),
primary key (camp_id)
)ENGINE=INNODB;
create table if not exists table3(
ad_id varchar(30) not null,
camp_id varchar(30) not null,
foreign key (camp_id) references table2(camp_id),
primary key (ad_id)
)ENGINE=INNODB;
create table if not exists counter(
ad_id varchar(30) not null,
foreign key (ad_id) references table3(ad_id),
PRIMARY KEY(ad_id)
)ENGINE=INNODB;
当我运行上述命令时,我会遇到以下错误。
第96行的错误1215(HY000):无法添加外键约束
有什么问题?
答案 0 :(得分:1)
没有任何错误。运行没有错误。
mysql> create table if not exists table1(
-> cust_name varchar(30) not null,
-> cust_phone char(16) not null,
-> cust_mail char(30) not null,
-> cust_address varchar(100),
-> bills int(10),
-> primary key (cust_name)
-> )ENGINE=INNODB;
Query OK, 0 rows affected, 1 warning (0.08 sec)
mysql> create table if not exists table2 (
-> camp_id varchar(30) not null,
-> advr_id varchar(30) not null,
-> foreign key (advr_id) references table1 (cust_name),
-> primary key (camp_id)
-> )ENGINE=INNODB;
Query OK, 0 rows affected, 1 warning (0.10 sec)
mysql> create table if not exists table3(
-> ad_id varchar(30) not null,
-> camp_id varchar(30) not null,
-> foreign key (camp_id) references table2(camp_id),
-> primary key (ad_id)
-> )ENGINE=INNODB;
Query OK, 0 rows affected (0.37 sec)
mysql> create table if not exists counter(
-> ad_id varchar(30) not null,
-> foreign key (ad_id) references table3(ad_id),
-> PRIMARY KEY(ad_id)
-> )ENGINE=INNODB;
Query OK, 0 rows affected (0.42 sec)
我在我的数据库中执行,但没有任何错误或警告
答案 1 :(得分:0)
运行此代码时没有错误。我认为他们可能是你的原始代码中的一些拼写错误。尝试使用mysql gui应用程序获取日志的详细信息。它将准确地指出错误。