2016-11-30 95 views
0

我正在研究一個類項目的音樂數據庫,並且遇到了爲某些表設置外鍵的問題。MySQL中的MusicDB對同一個表的多個外鍵

我有大約6張桌子。

Songs 
Artist 
FeaturedArtist 
Albums 
Genres 
SubGenres 
RelatedGenres 
RecordLabel 

的問題都圍繞着以下表格

create table Songs(Title varchar(75) not null, Artist varchar(40) not null, FeaturedArtist varchar(100), 
Album varchar(100), SongYear YEAR(4) not null, Genre char(10) not null, SubGenre varchar(30), Producer varchar(30) not null, 
SongWriter varchar(50), MusicVideo varchar(30000), Lyrics varchar(30000) not null, 
      PRIMARY KEY (Title), 
      FOREIGN KEY(Artist) REFERENCES Artist(Artist), 
      FOREIGN KEY(FeaturedArtist) REFERENCES FeaturedArtists(Artist), 
      FOREIGN KEY(Album) REFERENCES Albums(Album), 
      FOREIGN KEY(Genre) REFERENCES Genre(Genre), 
      FOREIGN KEY(SubGenre) REFERENCES SubGenre(SubGenre)); 

create table Artists(Artist varchar(40) not null, Genre varchar(10) not null, Band_Group varchar(40), 
      PRIMARY KEY(Artist), 
      FOREIGN KEY Genre REFERENCES Genre(Genre)); 

create table Genre(Genre varchar(10) not null, RelatedGenres varchar(10) not null); PRIMARY KEY(Genre); 
FOREIGN KEY RelatedGenres REFERENCES RalatedGenres(Genre); 

create table RelatedGenres(Genre varchar(10) not null, RelatedGenre varchar(10)); 

create table SubGenre(SubGenre varchar(30) not null, Genre varchar(10) not null, 
      PRIMARY KEY(SubGenre), 
      FOREIGN KEY(Genre) REFERENCES Genre(Genre)); 

我如何安排的方式這些表在那裏我沒有爲試圖用一個外鍵如流派多出現錯誤一張桌子。

希望你們中的一些人能幫忙!

+0

您可以包含確切的錯誤輸出嗎? –

+0

你有什麼錯誤? –

+0

我會得到#1005 - 無法創建表'bpmtest'.'genre'(錯誤:150「外鍵約束不正確」 – KagemushaShen

回答

0

看起來問題在於你的外鍵聲明。您需要指定括號即FOREIGN KEY (Genre) REFERENCES Genre(Genre),而不是FOREIGN KEY Genre REFERENCES Genre(Genre)

裏面的子表列通過調整ArtistsGenre表在我的數據庫工作。

create table RelatedGenres(Genre varchar(10) not null, RelatedGenre varchar(10)); 

create table Genre(Genre varchar(10) not null, RelatedGenres varchar(10) not null, PRIMARY KEY(Genre), FOREIGN KEY (RelatedGenres) REFERENCES RalatedGenres(Genre)); 

create table SubGenre(SubGenre varchar(30) not null, Genre varchar(10) not null, 
     PRIMARY KEY(SubGenre), 
     FOREIGN KEY(Genre) REFERENCES Genre(Genre)); 

create table Artists(Artist varchar(40) not null, Genre varchar(10) not null, Band_Group varchar(40),    PRIMARY KEY(Artist),    FOREIGN KEY (Genre) REFERENCES Genre(Genre)); 

create table Songs(Title varchar(75) not null, Artist varchar(40) not null, FeaturedArtist varchar(100), Album varchar(100), SongYear YEAR(4) not null, Genre char(10) not null, SubGenre varchar(30), Producer varchar(30) not null, SongWriter varchar(50), MusicVideo varchar(30000), Lyrics varchar(30000) not null, 
     PRIMARY KEY (Title), 
     FOREIGN KEY(Artist) REFERENCES Artists(Artist), 
     FOREIGN KEY(FeaturedArtist) REFERENCES FeaturedArtists(Artist), 
     FOREIGN KEY(Album) REFERENCES Albums(Album), 
     FOREIGN KEY(Genre) REFERENCES Genre(Genre), 
     FOREIGN KEY(SubGenre) REFERENCES SubGenre(SubGenre)); 
+0

謝謝!然而,這些工作一直在進行,直到我嘗試創建歌曲和專輯表格。我會仔細看看你在這裏寫的是什麼,因爲很難看出它們之間的區別 – KagemushaShen

+0

我從中得到錯誤的唯一表格是歌曲表現在 – KagemushaShen

+0

這是錯誤信息創建表'bpmtest'.'songs'外鍵約束失敗在'FOREIGN KEY(藝術家)附近的數據字典中未找到引用表'bpmtest'.'artist'參考藝術家藝術家), 外國關鍵(FeaturedArtist)參考特色藝術家(藝術家), FOREIGN KEY(專輯)REFERENCES專輯(專輯), FOREIGN KEY(流派)參考文獻流派(流派), FOREIGN KEY(SubGenre)REFERENCES SubGenre(SubGenre))''。 – KagemushaShen