SQL代码 - 无法添加或更新子行

时间:2017-04-14 15:47:38

标签: mysql sql database

我创建了一个数据库并添加了表格,没有麻烦,我正在尝试将数据添加到表中,但不断收到错误:

错误1452:(23000):无法添加或更新子行:外键约束失败(musictrack,CONSTRAINT track_ibfk_1 FOREIGN KEY(genre_id )参考genregenre_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);

0 个答案:

没有答案