没有主键或候选键错误..我应该怎么做..我上传了两张图片以清楚地看到错误

时间:2016-11-24 14:36:55

标签: sql-server database entity-framework

以下是所有这些表的CREATE脚本

schools架构

CREATE TABLE schools
 (
     NAME          VARCHAR(20),
     levell        VARCHAR(20),
     adress        VARCHAR(20),
     main_language VARCHAR(20),
     email         VARCHAR(20),
     information   VARCHAR(80),
     mission       VARCHAR(80),
     vision        VARCHAR(80),
     type          VARCHAR(20),
     fees          REAL,
     phone_number  INT,
     PRIMARY KEY (NAME, levell)
  ) 

school_phones架构

CREATE TABLE school_phones
  (
     NAME         VARCHAR(20) PRIMARY KEY,
     phone_number INT,
     FOREIGN KEY (NAME, phone_number) REFERENCES schools
  ) 
--phone.name references school

elementary_schools架构

CREATE TABLE elementary_schools
(
  NAME   VARCHAR(20) FOREIGN KEY REFERENCES schools(NAME) ,
  levell VARCHAR(20) FOREIGN KEY REFERENCES schools(levell) ,
  PRIMARY KEY(NAME , levell)
)
--elementary.name references school

elementary_school_supplies架构

CREATE TABLE elementary_school_supplies
  (
     NAME     VARCHAR(20) PRIMARY KEY FOREIGN KEY REFERENCES elementary_schools(
     NAME),
     supplies VARCHAR(20)
  ) 
--Elementary_Schools.name references elementary

middle_schools架构

CREATE TABLE middle_schools
  (
     NAME VARCHAR(20) PRIMARY KEY FOREIGN KEY REFERENCES schools
  ) 
--middle.name references school

high_schools架构

CREATE TABLE high_schools
  (
     NAME VARCHAR(20) PRIMARY KEY FOREIGN KEY REFERENCES schools
  ) 

错误显示在下面的图片中

ERROR1: enter image description here

误差2: enter image description here

任何帮助解决这个问题将不胜感激。在此先感谢。

1 个答案:

答案 0 :(得分:1)

您的第一个问题是您的学校PK是namelevell的组合。您尝试使用单独的FK's引用这些内容时,还需要一个复合FK

你想做的就是你用School_Phones表做过的事情,就像这样;

CREATE TABLE Elementary_Schools (name varchar(20), levell varchar(20), primary key (name, levell), foreign key (name, levell) references Schools)

第二个问题非常自我解释,您只能使用相同数据类型的FK个字段。您的phone_number字段(int)无法引用levell,因为它是varchar(20)。这些都需要是相同的数据类型。