2013-09-29 79 views
0
create table movie(

    movieTitle varchar(40) 
      not null 

, yearReleased year 
      check (not year > year(current_date)) 

, movieLength int(3) 
      null 

, constraint coPKmovie 
    primary key (movieTitle, yearReleased) 
); 

create table person(

    personName varchar(40) 
      not null 

, secondName varchar(40) 
      not null 

, dateOfBirth datetime 
      not null 

, yearCareerStarted year 
      not null 
      check (not year > year(current_date)) 

, bornCountry char(03) 
      not null 

, constraint coPKperson 
    primary key (personName, secondName) 
); 

create table participant(

    partPersonName varchar(40) 
      not null 

, partSecondName varchar(40) 
      not null 

, movieTitle varchar(40) 
      not null 

, jobTitle varchar(30) 
      not null 

, constraint coPKpart 
    primary key (partPersonName, partSecondName, movieTitle, jobTitle) 

); 


alter table participant 
    add constraint partFKname foreign key (partPersonName) 
    references person (personName) 

, add constraint partFKSecond foreign key (partSecondName) 
    references person (secondName) 

, add constraint partFKmovie foreign key (movieTitle) 
    references movie (movieTitle) 

    on delete cascade 
    on update cascade; 

有人可以解釋爲什麼我總是得到一個錯誤,而我想從表參與者,partSecondName到表person,secondName創建外鍵。我不想聽到爲什麼我不在數據庫中使用任何ID,我只是在沒有他們的情況下練習。提前致謝! :)MySQL,不能創建外鍵

+0

什麼是錯誤? – Mihai

+2

這個人的FK是錯誤的,你應該同時對名字和第二個名字進行單獨的約束,而不是單獨的約束。 –

+0

非常感謝你Matteo Tassinari,它現在工作!哦,現在我會知道:)謝謝:) –

回答

0

「8.關係中的一個字段是組合(複合)鍵的一部分,並沒有它自己的單獨索引。即使該字段的索引是組合鍵的一部分,您也必須只爲該關鍵字段創建一個單獨的索引,以便在約束中使用它。「

看到這篇舊文章here