gpt4 book ai didi

mysql外键为主键报错

转载 作者:行者123 更新时间:2023-11-28 23:40:54 24 4
gpt4 key购买 nike

我的 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;

当我运行上述命令时,出现以下错误。

ERROR 1215 (HY000) at line 96: Cannot add foreign key constraint

问题是什么?

最佳答案

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

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外键为主键报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34411927/

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