Mysql SQL代码-无法添加或更新子行
我已经创建了一个数据库并向其中添加了表。不麻烦,我正在尝试向表中添加数据,但不断出现错误: 错误1452:(23000):无法添加或更新子行:外键约束失败(Mysql SQL代码-无法添加或更新子行,mysql,sql,database,Mysql,Sql,Database,我已经创建了一个数据库并向其中添加了表。不麻烦,我正在尝试向表中添加数据,但不断出现错误: 错误1452:(23000):无法添加或更新子行:外键约束失败(音乐,曲目,约束曲目从类型中选择*流派id |流派名称|+-------------------+--------------+----1 |嘻哈| 2 |豪斯| 3 |摇滚| 4 |金属| |技术| 6 |独立|+--------------+----+----套装(0.00秒)流派中的6行已经有id
音乐
,曲目
,约束曲目
外键(流派id
)引用流派
(流派id
)
表:
create table genre
(
genre_id int not null auto_increment,
genre_name varchar(10),
primary key (genre_id)
);
create table artist
(
artist_id int not null auto_increment,
artist_name varchar(20),
primary key (artist_id)
);
create table track
(
track_id int not null auto_increment,
artist_id int not null,
genre_id int not null,
track_name varchar(50),
primary key (track_id),
foreign key (genre_id) references genre (genre_id),
foreign key (artist_id) references artist (artist_id)
);
create table location
(
location_id int not null auto_increment,
location_name varchar(25),
primary key (location_id)
);
create table user
(
user_id int not null auto_increment,
genre_id int not null,
location_id int not null,
user_fname varchar(10),
user_lname varchar(10),
user_age int,
user_gender bit,
primary key (user_id),
foreign key (genre_id) references genre (genre_id),
foreign key (location_id) references location (location_id)
);
下一行代码是导致错误的地方,所有其他轨迹都是这样写的,所以无论这一行出现什么问题,我都会解决
INSERT INTO `music`.`track`(`track_name`,`artist_id`,`genre_id`) VALUES
('Next Episode',2,1);
在用相应的记录填充其他表之前,不能将行添加到
轨迹中。这就是外键
关系所做的。您需要一个类型
记录,该记录的类型id
=1,然后才能插入曲目
记录,该记录的类型id
=1.mysql>从类型中选择*流派id |流派名称|+-------------------+--------------+----1 |嘻哈| 2 |豪斯| 3 |摇滚| 4 |金属| |技术| 6 |独立|+--------------+----+----套装(0.00秒)流派中的6行已经有id