SQL Server - 添加主键和外键

时间:2014-09-17 00:54:05

标签: sql sql-server

我有一个查询创建了几个表,如下所示,第二个查询应该为每个表添加主键和外键。但是,当我尝试执行查询时,我会收到以下错误消息:

  

Msg 1776,Level 16,State 0,Line 2
  参考表中没有主键或候选键' DEPARTMENT'匹配外键中的引用列列表' FK__EMPLOYEE__dno__25869641'。

     

Msg 1750,Level 16,State 0,Line 2
  无法创建约束。查看以前的错误。

我不确定这意味着什么,因为我研究了主键和外键。任何人都可以引导我朝着如何解决这个问题的正确方向前进吗?谢谢! :)

CREATE TABLE EMPLOYEE 
(
    fname       varchar(100),
    minit       char(1),
    lname       varchar(100),
    ssn         char(9),
    bdate       date,
    addr        varchar(100),
    sex         char(1),
    salary      int,
    super_ssn   char(9),
    dno         int
);

CREATE TABLE DEPARTMENT 
(
    dname           varchar(100),
    dnumber         int,
    mgr_ssn         char(9),
    mgr_start_date  date
);

CREATE TABLE DEPENDENTS 
(
    essn            char(9),
    dependent_name  varchar(100),
    sex             char(1),
    bdate           date,
    relationship    varchar(100)
);

CREATE TABLE DEPT_LOCATIONS 
(
    dnumber         int,
    dlocation       varchar(100)
);

CREATE TABLE PROJECT 
(
    pname       varchar(100),
    pnumber     int,
    plocation   varchar(100),
    dnum        int
);

CREATE TABLE WORKS_ON 
(
    essn    char(9),
    pno     int,
    hrs     float
);

ALTER TABLE EMPLOYEE ADD PRIMARY KEY(ssn);
ALTER TABLE EMPLOYEE ADD FOREIGN KEY(super_ssn) REFERENCES EMPLOYEE(ssn);
ALTER TABLE EMPLOYEE ADD FOREIGN KEY(dno) REFERENCES DEPARTMENT(dnumber);

ALTER TABLE DEPARTMENT ADD PRIMARY KEY(dnumber);
ALTER TABLE DEPARTMENT ADD FOREIGN KEY(mgr_ssn) REFERENCES EMPLOYEE(ssn);

ALTER TABLE DEPT_LOCATIONS ADD PRIMARY KEY(dnumber, dlocation);
ALTER TABLE DEPT_LOCATIONS ADD FOREIGN KEY(dnumber) REFERENCES DEPARTMENT(dnumber);

ALTER TABLE PROJECT ADD PRIMARY KEY(pnumber);
ALTER TABLE PROJECT ADD FOREIGN KEY(dnum) REFERENCES DEPARTMENT(dnumber);

ALTER TABLE WORKS_ON ADD PRIMARY KEY(essn, pno); 
ALTER TABLE WORKS_ON ADD FOREIGN KEY(essn) REFERENCES EMPLOYEE(ssn);
ALTER TABLE WORKS_ON ADD FOREIGN KEY(pno) REFERENCES PROJECT(pnumber);

ALTER TABLE DEPENDENTS ADD PRIMARY KEY(essn, dependent_name);
ALTER TABLE DEPENDENTS ADD FOREIGN KEY(essn) REFERENCES EMPLOYEE(ssn);

1 个答案:

答案 0 :(得分:0)

主键列应为非null。对于所有PK,表定义不应该为空。

CREATE TABLE EMPLOYEE (
    fname       varchar(100),
    minit       char(1),
    lname       varchar(100),
    ssn         char(9) not null,
    bdate       date,
    addr        varchar(100),
    sex         char(1),
    salary      int,
    super_ssn   char(9),
    dno         int
);

CREATE TABLE DEPARTMENT (
    dname           varchar(100),
    dnumber         int not null,
    mgr_ssn         char(9),
    mgr_start_date  date
);

CREATE TABLE DEPENDENTS (
    essn            char(9) not null,
    dependent_name  varchar(100) not null,
    sex             char(1),
    bdate           date,
    relationship    varchar(100)
);

CREATE TABLE DEPT_LOCATIONS (
    dnumber         int not null,
    dlocation       varchar(100) not null
);

CREATE TABLE PROJECT (
    pname       varchar(100),
    pnumber     int not null,
    plocation   varchar(100),
    dnum        int
);

CREATE TABLE WORKS_ON (
    essn    char(9) not null,
    pno     int not null,
    hrs     float
);

ALTER TABLE EMPLOYEE ADD PRIMARY KEY(ssn);
ALTER TABLE DEPARTMENT ADD PRIMARY KEY(dnumber);
ALTER TABLE EMPLOYEE ADD FOREIGN KEY(super_ssn) REFERENCES EMPLOYEE(ssn);
ALTER TABLE EMPLOYEE ADD FOREIGN KEY(dno) REFERENCES DEPARTMENT(dnumber);


ALTER TABLE DEPARTMENT ADD FOREIGN KEY(mgr_ssn) REFERENCES EMPLOYEE(ssn);

ALTER TABLE DEPT_LOCATIONS ADD PRIMARY KEY(dnumber, dlocation);
ALTER TABLE DEPT_LOCATIONS ADD FOREIGN KEY(dnumber) REFERENCES DEPARTMENT(dnumber);

ALTER TABLE PROJECT ADD PRIMARY KEY(pnumber);
ALTER TABLE PROJECT ADD FOREIGN KEY(dnum) REFERENCES DEPARTMENT(dnumber);

ALTER TABLE WORKS_ON ADD PRIMARY KEY(essn, pno); 
ALTER TABLE WORKS_ON ADD FOREIGN KEY(essn) REFERENCES EMPLOYEE(ssn);
ALTER TABLE WORKS_ON ADD FOREIGN KEY(pno) REFERENCES PROJECT(pnumber);

ALTER TABLE DEPENDENTS ADD PRIMARY KEY(essn, dependent_name);
ALTER TABLE DEPENDENTS ADD FOREIGN KEY(essn) REFERENCES EMPLOYEE(ssn);