尝试将数据插入歌曲表时出现错误。我不确定为什么。
任何帮助将不胜感激。谢谢。这是我添加的详细信息,这样我就可以发布哈哈了。
create table artist
(
id int primary key auto_increment,
name varchar(128) not null,
nationality varchar(128)
) ENGINE=InnoDB;
insert into artist (name, nationality) values ('Metallica', 'American');
insert into artist (name, nationality) values ('Rush', 'Canadian');
create table album
(
id int primary key auto_increment,
name varchar (128) not null,
artist int not null,
foreign key (artist) references artist(id),
genre int,
foreign key (genre) references genre(id)
) ENGINE=InnoDB;
insert into album (name, artist, genre) values ('Ride the Lightning', 1, 1);
insert into album (name, artist, genre) values ('Moving Pictures', 2, 2);
create table song
(
id int primary key auto_increment,
name varchar (128) not null,
duration varchar (128),
album int not null,
foreign key (album) references album(id)
) ENGINE=InnoDB;
insert into song (name, duration, album) values ('Fade to Black', '1 min', 1);
insert into song (name, duration, album) values ('Tom Sawyer', '2 min', 2);
create table genre
(
id int primary key auto_increment,
name varchar (128) not null,
description varchar (256)
) ENGINE=InnoDB;
insert into genre (name, description) values ('Rock', 'Lots of drums and guitars');
insert into genre (name, description) values ('Metal', 'Drums and guitars on steroids');
答案 0 :(得分:1)
您的代码顺序导致了问题。您需要移动
create table genre
(
id int primary key auto_increment,
name varchar (128) not null,
description varchar (256)
) ENGINE=InnoDB;
insert into genre (name, description) values ('Rock', 'Lots of drums and guitars');
insert into genre (name, description) values ('Metal', 'Drums and guitars on steroids');
在“创建相册”上方
答案 1 :(得分:1)
这是正确的方法:HOW TO DO IT
这是它不起作用的一些原因:
当您尝试创建表格相册时,没有表格“流派”:
create table album
(
id int primary key auto_increment,
name varchar (128) not null,
artist int not null,
foreign key (artist) references artist(id),
genre int,
foreign key (genre) references genre(id)
) ENGINE=InnoDB;
这里是DEMO
此外,按时创建表“流派”时,还有另一个问题。您必须在表“ genre”中插入数据才能插入其他数据。 这是DEMO起作用的地方。
答案 2 :(得分:0)
一个是创建和插入语句的顺序,应该已经给您错误,因为表引用不存在等。
根据上述错误,假设命令正确执行,则可能无法在歌曲中插入数据,原因是 您专辑表中的ID为null或您要插入歌曲的ID,专辑中不存在该ID,请先将数据插入专辑表中,然后再插入歌曲表中,因为歌曲表的外键正在检查专辑表中的ID数据