以下是所有这些表的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
)
错误显示在下面的图片中
任何帮助解决这个问题将不胜感激。在此先感谢。
答案 0 :(得分:1)
您的第一个问题是您的学校PK是name
和levell
的组合。您尝试使用单独的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)
。这些都需要是相同的数据类型。