在数据库中编写查询时,我一直收到此错误:
外键'FK__Asses__a_Type__4DB4832C'的引用列列表中的列数与引用的表'Course_Assesments'中的主键的列数不匹配
这是我写的代码:
CREATE DATABASE GUC_WEBSITE
CREATE TABLE Members(email VARCHAR(30), f_Name VARCHAR(10), l_Name VARCHAR(10),
address VARCHAR(50), phone_1 INTEGER, phone_2 INTEGER, password VARCHAR(20),
PRIMARY KEY (email), UNIQUE (phone_1, phone_2))
CREATE TABLE Students(email VARCHAR(30), major VARCHAR(20), age INTEGER, student_ID VARCHAR(7) NOT NULL,
PRIMARY KEY (email), UNIQUE (student_ID),
FOREIGN KEY (email) REFERENCES Members)
CREATE TABLE Graduates(email VARCHAR(30), dgree_1 VARCHAR(10), degree_2 VARCHAR(10), degree_3 VARCHAR(10),
PRIMARY KEY (email), FOREIGN KEY (email) REFERENCES Members)
CREATE TABLE Undergraduates(email VARCHAR(30), gpa REAL NOT NULL, academic_year VARCHAR(6) NOT NULL,
PRIMARY KEY (email), FOREIGN KEY (email) REFERENCES Members)
CREATE TABLE Staffs(email VARCHAR(30), department VARCHAR(10), staff_ID VARCHAR(7) NOT NULL, job_title VARCHAR(40),
PRIMARY KEY (email), UNIQUE (staff_ID), FOREIGN KEY (email) REFERENCES Members, UNIQUE (staff_ID))
CREATE TABLE Academic_Staffs(email VARCHAR(30), salary INTEGER,
PRIMARY KEY (email), FOREIGN KEY (email) REFERENCES Members)
CREATE TABLE Deans(email VARCHAR(30), faculty VARCHAR(40) NOT NULL,
PRIMARY KEY (email), FOREIGN KEY (email) REFERENCES Members)
CREATE TABLE Lecturers(email VARCHAR(30), course_Taught VARCHAR(30),
PRIMARY KEY (email), FOREIGN KEY (email) REFERENCES Members)
CREATE TABLE Courses(course_ID VARCHAR(10), outcome VARCHAR(100), objective VARCHAR(100), content VARCHAR(100), course_name VARCHAR(25),
coordinator VARCHAR(20), semester INTEGER, is_Core BIT, course_Language VARCHAR(10), is_Obligatory BIT, resources VARCHAR(50), lecturer_Email VARCHAR(30),
PRIMARY KEY (course_ID), UNIQUE (course_Name, objective), FOREIGN KEY (lecturer_Email) REFERENCES Lecturers)
CREATE TABLE Admins(email VARCHAR(30), admin_ID VARCHAR(7), is_Admin BIT,
PRIMARY KEY (email), FOREIGN KEY (email) REFERENCES Members, UNIQUE (admin_ID))
CREATE TABLE Faculties(email VARCHAR(30), fac_Name VARCHAR(40), field VARCHAR (20), f_Description VARCHAR(80), major_1 VARCHAR(10),
major_2 VARCHAR(10), degree_1 VARCHAR(15), degree_2 VARCHAR(15), degree_3 VARCHAR(15)
PRIMARY KEY (email, fac_Name), UNIQUE (f_Description), FOREIGN KEY (email) REFERENCES Members)
CREATE TABLE Guest_Questions(guest_ID VARCHAR(10), date_Of_Quest DATE, question_asked VARCHAR(50), number_Of_Questions INTEGER,
PRIMARY KEY (guest_ID))
CREATE TABLE Course_Assesments(a_Type VARCHAR(15), a_Weight REAL, course_ID VARCHAR(7), student_Email VARCHAR(30),
lecturer_Email VARCHAR(30),
PRIMARY KEY (a_Type, student_Email, lecturer_Email), FOREIGN KEY (student_Email) REFERENCES Students,
FOREIGN KEY (lecturer_Email) REFERENCES Lecturers)
CREATE TABLE Questionaires(q_ID VARCHAR(5), admin_Email VARCHAR(30), student_Email VARCHAR(30),
PRIMARY KEY (q_ID), FOREIGN KEY (admin_Email) REFERENCES Admins, FOREIGN KEY (student_Email) REFERENCES Students)
CREATE TABLE Course_Surveys(course_ID VARCHAR(10), iD VARCHAR(5), student_Email VARCHAR(30),
PRIMARY KEY (course_ID), FOREIGN KEY (course_ID) REFERENCES Courses,
FOREIGN KEY (iD) REFERENCES Questionaires,
FOREIGN KEY (student_Email) REFERENCES Students)
CREATE TABLE Graduation_Survey(iD VARCHAR(5), student_Email VARCHAR(30),
PRIMARY KEY (iD), FOREIGN KEY (iD) REFERENCES Questionaires, FOREIGN KEY (student_Email) REFERENCES Students)
CREATE TABLE Satisfactory_Surveys(iD VARCHAR(5), student_Email VARCHAR(30),
PRIMARY KEY (iD), FOREIGN KEY (iD) REFERENCES Questionaires, FOREIGN KEY (student_Email) REFERENCES Students)
CREATE TABLE Registers(student_Email VARCHAR(30), course_ID VARCHAR(10), is_approved BIT,
PRIMARY KEY (student_Email, course_ID), FOREIGN KEY (student_Email) REFERENCES Students,
FOREIGN KEY (course_ID) REFERENCES Courses)
CREATE TABLE Asses(a_Type VARCHAR(15), student_Email VARCHAR(30), course_ID VARCHAR(10),
PRIMARY KEY (a_Type, student_Email, course_ID),
FOREIGN KEY (a_Type) REFERENCES Course_Assesments,
FOREIGN KEY (student_Email) REFERENCES Students,
FOREIGN KEY (course_ID) REFERENCES Courses)
CREATE TABLE CheckResults(email VARCHAR(30), iD VARCHAR(5), course_ID VARCHAR(10),
PRIMARY KEY (email, iD, course_ID),
FOREIGN KEY (email) REFERENCES Lecturers,
FOREIGN KEY (iD) REFERENCES Course_Surveys,
FOREIGN KEY (course_ID) REFERENCES Course_Surveys)
答案 0 :(得分:0)
你的问题来自这两个表:
CREATE TABLE Course_Assesments
( a_Type VARCHAR(15),
a_Weight REAL,
course_ID VARCHAR(7),
student_Email VARCHAR(30),
lecturer_Email VARCHAR(30),
PRIMARY KEY (a_Type, student_Email, lecturer_Email),
FOREIGN KEY (student_Email) REFERENCES Students,
FOREIGN KEY (lecturer_Email) REFERENCES Lecturers
)
CREATE TABLE Asses
( a_Type VARCHAR(15),
student_Email VARCHAR(30),
course_ID VARCHAR(10),
PRIMARY KEY (a_Type, student_Email, course_ID),
FOREIGN KEY (a_Type) REFERENCES Course_Assesments,
FOREIGN KEY (student_Email) REFERENCES Students,
FOREIGN KEY (course_ID) REFERENCES Courses
)
您告诉数据库Asses.a_Type
等于Course_Assesments
中的主键,但course_assesments
中的主键由3列(a_Type,student_Email,lecturer_Email)组成,由于一列不能与3列中的元组相等,因此会出现错误。
如果您真的希望Asses
引用Course_Assesments
,则需要这样的表:
CREATE TABLE Asses
( a_Type VARCHAR(15),
student_Email VARCHAR(30),
lecturer_Email VARCHAR(30),
course_ID VARCHAR(10),
PRIMARY KEY (a_Type, student_Email, course_ID),
FOREIGN KEY (a_Type, Student_Email, lecturer_Email) REFERENCES Course_Assesments (a_Type, Student_Email, lecturer_Email),
FOREIGN KEY (student_Email) REFERENCES Students,
FOREIGN KEY (course_ID) REFERENCES Courses
)
最后,与您的问题无关,您在整个过程中错误地拼写了评估,这没有任何区别,但对于使用数据库的任何其他人在收到Invalid Object Name Course_Assessment
等错误消息时可能非常令人沮丧,因为他们已经直观地拼写了正确的评估(我曾经处理过一个数据库,这个数据库在列名中大约有一半出现时收到了收到的信息,它几乎每天都让我感到惊讶)