首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MySQL,无法创建外键

问MySQL,无法创建外键
EN

Stack Overflow用户
提问于 2013-09-29 17:42:21
回答 1查看 167关注 0票数 0
代码语言:javascript
复制
    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中出现错误。我不想知道为什么我在数据库里不使用任何身份证明,我只是在没有它们的情况下练习。谢谢!)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-09-29 19:18:57

"8.关系中的一个字段是组合(复合)键的一部分,没有它自己的单独索引。尽管字段有一个索引作为复合键的一部分,但您必须为该键字段创建单独的索引,以便在约束中使用它。

请看这篇老文章here。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19081189

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档