“无法添加外键约束”

时间:2017-06-19 16:07:52

标签: mysql sql

首先我创建了这个表名DEPARTMENT,之后我创建了Dept_Locations,它在Dnumber中有这个表中的外键引用。

create table DEPARTMENT(
    Dname               varchar(20)     NOT NULL,
    Dnumber             int             NOT NULL,
    Mgr_ssn             char(9)         NOT NULL,
    Mgr_start_date      date            NOT NULL,
    Primary key(Dnumber),
    Unique(Dname)
);

Dept_Locations表代码:

create table Dept_Locations(
    Dnum char(4),
    Dlocation varchar(16),
    primary key(Dnum,Dlocation),
    foreign key(Dnum) references DEPARTMENT(Dnumber));

现在,当我执行此代码以创建Dept_Locations时,我收到错误:

  

无法添加外键约束。

3 个答案:

答案 0 :(得分:2)

外键必须与它所引用的列的数据类型相同。

DEPARTMENT中的列<div class="filter"> <p class="filter__title">Filter businesses by size or industry</p> <p class="filter__subtitle">Profile information provided by employer via WorkplaceDynamics</p> <select name="" id="size" class="select__size"> <option value="size_all">All sizes</option> <option value="size_lg">Large</option> <option value="size_md">Midsize</option> <option value="size_sm">Small</option> </select> <select name="" id="industry" class="select__industry"> <option value="industry__all">All industries</option> <option value="industry_a">Accounting</option> <option value="industry_b">Advertising</option> <option value="industry_c">Agents / Brokers</option> <option value="industry_d">Agriculture</option> <option value="industry_e">Architectural, Engineering, and Construction Svcs</option> <option value="industry_f">Auto Dealership</option> <option value="industry_g">Automotive Glass Repair and Replacement</option> </select> </div> <h2>Large</h2> <h3>500 or more employees</h3> <div class="leaderboard"> <div class="leaderboard__group leaderboard--lg"> <div class="leaderboard__company" data-size="size_lg" data-industry=""> <div class="leaderboard__rank"> <p class="leaderboard__rank--number">1</p> </div> <div class="leaderboard__info"> <p class="leaderboard__size">Large</p> <div class="leaderboard__wrapper--name"> <p class="leaderboard__name">tk-name</p> </div> <p class="leaderboard__sector">tk-sector</p> </div> <div class="leaderboard__change"> <div class="leaderboard__change--inner"> <i class="fa fa-caret-up" aria-hidden="true"></i> <p class="leaderboard__change--number">2</p> </div> </div> </div> <div class="leaderboard__company" data-size="size_lg" data-industry=""> <div class="leaderboard__rank"> <p class="leaderboard__rank--number">2</p> </div> <div class="leaderboard__info"> <p class="leaderboard__size">Large</p> <p class="leaderboard__name">tk-name</p> <p class="leaderboard__sector">tk-sector</p> </div> <div class="leaderboard__change"> <div class="leaderboard__change--inner"> <i class="fa fa-minus" aria-hidden="true"></i> <p class="leaderboard__change--number">0</p> </div> </div> </div> <div class="leaderboard__company" data-size="size_lg" data-industry=""> <div class="leaderboard__rank"> <p class="leaderboard__rank--number">3</p> </div> <div class="leaderboard__info"> <p class="leaderboard__size">Large</p> <p class="leaderboard__name">tk-name</p> <p class="leaderboard__sector">tk-sector</p> </div> <div class="leaderboard__change"> <p class="leaderboard__label">New</p> </div> </div> <div class="leaderboard__company" data-size="size_lg" data-industry=""> <div class="leaderboard__rank"> <p class="leaderboard__rank--number">4</p> </div> <div class="leaderboard__info"> <p class="leaderboard__size">Large</p> <p class="leaderboard__name">tk-name</p> <p class="leaderboard__sector">tk-sector</p> </div> <div class="leaderboard__change"> <p class="leaderboard__label">New</p> </div> </div> <div class="leaderboard__company" data-size="size_lg" data-industry=""> <div class="leaderboard__rank"> <p class="leaderboard__rank--number">5</p> </div> <div class="leaderboard__info"> <p class="leaderboard__size">Large</p> <p class="leaderboard__name">tk-name</p> <p class="leaderboard__sector">tk-sector</p> </div> <div class="leaderboard__change"> <div class="leaderboard__change--inner"> <i class="fa fa-caret-up" aria-hidden="true"></i> <p class="leaderboard__change--number">8</p> </div> </div> </div> </div> <!-- .leaderboard __lg--> </div> <!-- START OF AD CODE --> <div class="advertising advertising--rect"> <div class="text-center hidden-xs"> <div id="fixed-leaderboard-top" class="dfp-ad" data-dfp-custom-pos="fixed-leaderboard-top, atf" data-dfp-size="[728,90]"> </div> </div> <div class="text-center hidden-sm hidden-md hidden-lg"> <div id="fixed-leaderboard-top-mobile" class="dfp-ad" data-dfp-custom-pos="fixed-leaderboard-top, atf" data-dfp-size="[320,50]"> </div> </div> </div> <!-- END OF AD CODE --> <h2>Midsize</h2> <h3>150-499 employees</h3> <div class="leaderboard"> <div class="leaderboard__group leaderboard--md"> <div class="leaderboard__company" data-size="size_md" data-industry=""> <div class="leaderboard__rank"> <p class="leaderboard__rank--number">1</p> </div> <div class="leaderboard__info"> <p class="leaderboard__size">Midsize</p> <p class="leaderboard__name">tk-name</p> <p class="leaderboard__sector">tk-sector</p> </div> <div class="leaderboard__change"> <div class="leaderboard__change--inner"> <i class="fa fa-minus" aria-hidden="true"></i> <p class="leaderboard__change--number">0</p> </div> </div> </div> <!-- .leaderboard __company--> <div class="leaderboard__company" data-size="size_md" data-industry=""> <div class="leaderboard__rank"> <p class="leaderboard__rank--number">2</p> </div> <div class="leaderboard__info"> <p class="leaderboard__size">Midsize</p> <p class="leaderboard__name">tk-name</p> <p class="leaderboard__sector">tk-sector</p> </div> <div class="leaderboard__change"> <div class="leaderboard__change--inner"> <i class="fa fa-caret-up" aria-hidden="true"></i> <p class="leaderboard__change--number">6</p> </div> </div> </div> <!-- .leaderboard __company--> <div class="leaderboard__company" data-size="size_md" data-industry=""> <div class="leaderboard__rank"> <p class="leaderboard__rank--number">3</p> </div> <div class="leaderboard__info"> <p class="leaderboard__size">Midsize</p> <p class="leaderboard__name">tk-name</p> <p class="leaderboard__sector">tk-sector</p> </div> <div class="leaderboard__change"> <p class="leaderboard__label">New</p> </div> </div> <!-- .leaderboard __company--> <div class="leaderboard__company" data-size="size_md" data-industry=""> <div class="leaderboard__rank"> <p class="leaderboard__rank--number">4</p> </div> <div class="leaderboard__info"> <p class="leaderboard__size">Midsize</p> <p class="leaderboard__name">tk-name</p> <p class="leaderboard__sector">tk-sector</p> </div> <div class="leaderboard__change"> <div class="leaderboard__change--inner"> <i class="fa fa-caret-down" aria-hidden="true"></i> <p class="leaderboard__change--number">1</p> </div> </div> </div> <!-- .leaderboard __company--> <div class="leaderboard__company" data-size="size_md" data-industry=""> <div class="leaderboard__rank"> <p class="leaderboard__rank--number">5</p> </div> <div class="leaderboard__info"> <p class="leaderboard__size">Midsize</p> <p class="leaderboard__name">tk-name</p> <p class="leaderboard__sector">tk-sector</p> </div> <div class="leaderboard__change"> <div class="leaderboard__change--inner"> <i class="fa fa-caret-up" aria-hidden="true"></i> <p class="leaderboard__change--number">1</p> </div> </div> </div> <!-- .leaderboard __company--> </div> <!-- .leaderboard __md--> </div> <!-- .leaderboard--> <!-- START OF AD CODE --> <div class="advertising advertising--rect"> <div class="text-center hidden-xs"> <div id="fixed-leaderboard-top" class="dfp-ad" data-dfp-custom-pos="fixed-leaderboard-top, atf" data-dfp-size="[728,90]"> </div> </div> <div class="text-center hidden-sm hidden-md hidden-lg"> <div id="fixed-leaderboard-top-mobile" class="dfp-ad" data-dfp-custom-pos="fixed-leaderboard-top, atf" data-dfp-size="[320,50]"> </div> </div> </div> <!-- END OF AD CODE --> <h2>Small</h2> <h3>149 employees or less</h3> <div class="leaderboard"> <div class="leaderboard__group leaderboard--sm"> <div class="leaderboard__company" data-size="size_sm" data-industry=""> <div class="leaderboard__rank"> <p class="leaderboard__rank--number">1</p> </div> <div class="leaderboard__info"> <p class="leaderboard__size">Small</p> <a href=""><p class="leaderboard__name">tk-name</p></a> <p class="leaderboard__sector">tk-sector</p> </div> <div class="leaderboard__change"> <div class="leaderboard__change--inner"> <i class="fa fa-caret-up" aria-hidden="true"></i> <p class="leaderboard__change--number">1</p> </div> </div> </div> <div class="leaderboard__company" data-size="size_sm" data-industry=""> <div class="leaderboard__rank"> <p class="leaderboard__rank--number">2</p> </div> <div class="leaderboard__info"> <p class="leaderboard__size">Small</p> <p class="leaderboard__name">tk-name</p> <p class="leaderboard__sector">tk-sector</p> </div> <div class="leaderboard__change"> <p class="leaderboard__label">New</p> </div> </div> <div class="leaderboard__company" data-size="size_sm" data-industry=""> <div class="leaderboard__rank"> <p class="leaderboard__rank--number">3</p> </div> <div class="leaderboard__info"> <p class="leaderboard__size">Small</p> <p class="leaderboard__name">tk-name</p> <p class="leaderboard__sector">tk-sector</p> </div> <div class="leaderboard__change"> <div class="leaderboard__change--inner"> <i class="fa fa-caret-up" aria-hidden="true"></i> <p class="leaderboard__change--number">5</p> </div> </div> </div> <div class="leaderboard__company" data-size="size_sm" data-industry=""> <div class="leaderboard__rank"> <p class="leaderboard__rank--number">4</p> </div> <div class="leaderboard__info"> <p class="leaderboard__size">Small</p> <p class="leaderboard__name">tk-name</p> <p class="leaderboard__sector">tk-sector</p> </div> <div class="leaderboard__change"> <div class="leaderboard__change--inner"> <i class="fa fa-caret-down" aria-hidden="true"></i> <p class="leaderboard__change--number">3</p> </div> </div> </div> <div class="leaderboard__company" data-size="size_sm" data-industry=""> <div class="leaderboard__rank"> <p class="leaderboard__rank--number">5</p> </div> <div class="leaderboard__info"> <p class="leaderboard__size">Small</p> <p class="leaderboard__name">tk-name</p> <p class="leaderboard__sector">tk-sector</p> </div> <div class="leaderboard__change"> <p class="leaderboard__label">New</p> </div> </div> </div> <!-- .leaderboard __sm--> </div> <!-- .leaderboard --> Dnumber,列int定义为Dnum - 但必须将其定义为char(4)

答案 1 :(得分:0)

认为数据类型不同。

Dnum char(4)

Dnumber int

答案 2 :(得分:0)

您的主键和具有外键约束的列的大小不同 - 请尝试:

create table DEPARTMENT(
    Dname               varchar(20)     NOT NULL,
    Dnumber             char(4)          NOT NULL, # <-- Note the size change
    Mgr_ssn             char(9)         NOT NULL,
    Mgr_start_date      date            NOT NULL,
    Primary key(Dnumber),
    Unique(Dname)
);

(或更新另一个以匹配)