MySQL,无法创建外键

时间:2013-09-29 17:42:21

标签: mysql database database-design mysql-workbench

    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到table person,secondName创建外键时,有人可以解释为什么我总是会出错。我不想听到为什么我在我的数据库中不使用任何id,我只是在没有它们的情况下练习。提前致谢! :)

1 个答案:

答案 0 :(得分:0)

“8。关系中的一个字段是组合(复合)键的一部分,并没有它自己的单独索引。即使该字段有一个索引作为复合键的一部分,你必须创建一个单独的仅为该键字段的索引,以便在约束中使用它。“

请参阅此旧帖子here