MySQL代码中的错误代码1217

时间:2016-04-14 16:57:19

标签: mysql

我在Error 1217代码中收到错误:MySQL。我的MySQL代码是

DROP TABLE IF EXISTS Formed;
DROP TABLE IF EXISTS Album;
DROP TABLE IF EXISTS Band;
DROP TABLE IF EXISTS Customers;
DROP TABLE IF EXISTS Track;

CREATE TABLE Formed(
    FormedID int AUTO_INCREMENT, 
    YearFormed int, 
    CountryFormed varchar(50), 
    CityFormed varchar(50), 
    BandMembers varchar(400), 
    PRIMARY KEY(FormedID))
    ENGINE=InnoDB;

CREATE TABLE Track (
    TrackID int AUTO_INCREMENT, 
    AlbumID int AUTO_INCREMENT,
    Songs varchar (100), 
    TrackNumber varchar (20), 
    Title varchar (30), 
    TrackDuration varchar (4), 
    PRIMARY KEY (TrackID)) 
    ENGINE=InnoDB;

CREATE TABLE Album(
    AlbumID int AUTO_INCREMENT,
    TrackID int AUTO_INCREMENT,
    BandID int AUTO_INCREMENT,
    KEY(TrackID),
    KEY(BandID),
    Price varchar(5), 
    PublicationDate varchar(11), 
    Title varchar(30), 
    Genre varchar (36),
    PRIMARY KEY(AlbumID))
    ENGINE=InnoDB;

CREATE TABLE Band(
    BandID int AUTO_INCREMENT, 
    AlbumID int AUTO_INCREMENT, 
    KEY(AlbumID),
    RecordLabel varchar(50), 
    PRIMARY KEY(BandID))
    ENGINE=InnoDB;

CREATE TABLE Customers (
    CustomerID int AUTO_INCREMENT, 
    CName varchar (20), 
    CPhone int (11), 
    CEmail varchar (50), 
    CPPaid varchar (50), 
    CPDate date, 
    PRIMARY KEY (CustomerID))
    ENGINE=InnoDB;

ALTER TABLE Track 
ADD FOREIGN KEY (AlbumID) REFERENCES Album(AlbumID)ON DELETE SET NULL ON UPDATE CASCADE;

ALTER TABLE Album
ADD FOREIGN KEY (TrackID) REFERENCES Track(TrackID)ON DELETE SET NULL ON UPDATE CASCADE;

ALTER TABLE Album
ADD FOREIGN KEY (BandID) REFERENCES Band(BandID)ON DELETE SET NULL ON UPDATE CASCADE;

ALTER TABLE Band
ADD FOREIGN KEY (AlbumID) REFERENCES Album(AlbumID)ON DELETE SET NULL ON UPDATE CASCADE;

我收到错误DROP TABLE IF EXISTS Album Error Code: 1217. Cannot delete or update a parent row: a foreign key constraint fails,其他DROP TABLE IF EXISTS行也会发生错误。我在Foreign Keys脚本中使用MySQL的新手,我不知道为什么我会收到这些错误。如果有人可以帮我解决这个问题,我们将不胜感激。我已经尝试了以下链接Bogus foreign key constraint fail

可以找到的所有答案

1 个答案:

答案 0 :(得分:2)

您的脚本没问题,但在Dropping表之前,请尝试删除Foreign Key References