Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
mysql外键作为主键错误_Mysql_Foreign Keys - Fatal编程技术网

mysql外键作为主键错误

mysql外键作为主键错误,mysql,foreign-keys,Mysql,Foreign Keys,我的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 no

我的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):无法添加外键约束


什么问题?

没有任何错误。这是运行没有错误

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)

我在数据库中执行,但没有任何错误或警告

运行此代码时没有错误。我想他们可能是你的原代码中有拼写错误。尝试使用mysql gui应用程序获取日志的详细信息。它将精确地指出错误。

在尝试从表
计数器
@Shafiq中删除
ad_id varchar(30)not null,
时,执行此操作不会出现任何问题。您应该首先检查表中的数据,这可能会产生问题。谢谢,这是我发现的问题,只是拼写(名称)错误