2016-04-14 142 views
0

我有一個錯誤代碼,它是Error Code: 1215. Cannot add foreign key constraintMySQL。我的代碼是錯誤代碼:1215.無法在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 NOT NULL, 
    YearFormed int, 
    CountryFormed varchar(50), 
    CityFormed varchar(50), 
    BandMembers varchar(400), 
    PRIMARY KEY(FormedID)) 
    ENGINE=InnoDB; 

CREATE TABLE Track (
    TrackID int NOT NULL, 
    AlbumID int NOT NULL, 
    Songs varchar (100), 
    TrackNumber varchar (20), 
    Title varchar (20), 
    TrackDuration varchar (4), 
    PRIMARY KEY (TrackID), 
    FOREIGN KEY (AlbumID) REFERENCES Album(AlbumID)ON DELETE SET NULL ON UPDATE CASCADE) 
    ENGINE=InnoDB; 

CREATE TABLE Album(
    AlbumID int NOT NULL, 
    TrackID int NOT NULL, 
    BandID int NOT NULL, 
    Price varchar(5), 
    PublicationDate varchar(11), 
    Title varchar(30), 
    Genre varchar (36), 
    PRIMARY KEY(AlbumID), 
    FOREIGN KEY (TrackID) REFERENCES Track(TrackID)ON DELETE SET NULL ON UPDATE CASCADE, 
    FOREIGN KEY (BandID) REFERENCES Band(BandID)ON DELETE SET NULL ON UPDATE CASCADE) 
    ENGINE=InnoDB; 

CREATE TABLE Band(
    BandID int NOT NULL, 
    AlbumID int NOT NULL, 
    RecordLabel varchar(50), 
    PRIMARY KEY(BandID), 
    FOREIGN KEY (AlbumID) REFERENCES Album(AlbumID)ON DELETE SET NULL ON UPDATE CASCADE) 
    ENGINE=InnoDB; 

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

它創建了一個名爲Formed第一個表,但它給我的錯誤時,它會嘗試並創建第二個表,我不知道爲什麼。我知道一些關於MySQL的信息,我正在教我自己約Foreign Keys。我在網上看了一下,爲什麼我可以得到這個錯誤,但找不到任何有用的東西。

+1

您必須先定義「專輯」,然後才能在外鍵約束中使用它。 –

+0

@GordonLinoff我已經改變了它(看下面的答案),但當我想添加外鍵時,我仍然收到相同的錯誤 – user3283410

回答

1

從錯誤消息看來,您似乎試圖在Album表之前創建Track表,並且因此它失敗,因爲它無法爲外鍵約束找到父表(和列)。

我建議建立與主鍵的表僅然後,應用外鍵約束,一旦所有3代表的創建,如:

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

因此,從表中添加外鍵約束並在末尾添加它們? – user3283410

+0

是的,在創建表之後添加外鍵約束。 –

+0

我剛剛這樣做,但我再次收到錯誤,但在底部我已經放入 ALTER TABLE'Track' ADD FOREIGN KEY(AlbumID)REFERENCES Album(AlbumID)ON DELETE SET NULL ON UPDATE級聯; 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; – user3283410

0

按規定在子表的所有參考字段必須具有指數在父表中對其進行定義。他們還必須遵守一些其他限制。

按照文件上foreign key約束:

參考parent_tbl_name(一個index_col_name,...)

在相關子列定義一個INDEX。並確保子列的定義必須與其父列定義的匹配。

代碼修改建議

CREATE TABLE Formed(...) -- no change suggested 
CREATE TABLE Customers (...) -- no change suggested 

CREATE TABLE Band(
    ... 
    AlbumID int NOT NULL, 
    ... 
    KEY (AlbumID), -- <---- add this line 
    ... 
); 
CREATE TABLE Album(
    ... 
    TrackID int NOT NULL, 
    BandID int NOT NULL, 
    ... 
    KEY (TrakID), -- <---- add this line 
    KEY (BandID), -- <---- add this line 
    ... 
    -- FOREIGN KEY (TrackID)      -- <-- add this using ALTER 
    -- REFERENCES Track(TrackID)    --  after creating 
    --  ON DELETE SET NULL ON UPDATE CASCADE, --  Track table 
    ); 

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

CREATE TABLE Track (...) -- no changes suggested 

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

參見

[約束[符號]]外鍵
[INDEX_NAME](一個index_col_name,...)
參考tbl_name(一個index_col_name,...)
[ON DELETE reference_option]
[ON UPDATE reference_option]

reference_option:
RESTRICT | CASCADE | SET NULL | NO ACTION

相關問題