2012-11-21 58 views
-5

可能重複:
Multiple-column foreign key in MySQL?SQL中的以下錯誤是什麼意思?

而在databses編寫查詢我不斷收到此錯誤:

列在引用列的列表爲外鍵的數量「 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

你的問題是這兩個表:

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,因爲他們已經直觀地拼寫正確評估(我曾經處理過一個數據庫,拼寫爲在列名中出現的大約一半時收到,幾乎每天)