Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error code 1217 in MySQL code [duplicate]

Tags:

mysql

I'm getting the error: Error 1217 in my MySQL code. My MySQL code is

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;

And I'm getting the error DROP TABLE IF EXISTS Album Error Code: 1217. Cannot delete or update a parent row: a foreign key constraint fails, it also happens for the other DROP TABLE IF EXISTS lines. I'm new to using Foreign Keys in MySQL scripts and I don't know why I'm getting these errors. If anyone could help me fix this, it would be appreciated. I've already tried all the answers that can be found by following this link Bogus foreign key constraint fail

like image 549
smitthy Avatar asked Mar 11 '26 20:03

smitthy


1 Answers

Your Script is OK, but before Dropping Tables, try to remove the Foreign Key References

like image 54
Feelbad Soussi Wolfgun DZ Avatar answered Mar 15 '26 08:03

Feelbad Soussi Wolfgun DZ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!