2013-09-29 78 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