-5

可能重复:
MySQL 中的多列外键?

在数据库中编写查询时,我不断收到此错误:

外键“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)
4

1 回答 1

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、讲师_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
)

最后,与您的问题无关,您在整个过程中都拼错了 Assessment,这没有任何区别,但是对于使用数据库的其他人来说,当他们收到错误消息时会非常沮丧,例如Invalid Object Name Course_Assessment因为他们直观地拼写了 Assessment(我曾经处理过一个在列名中出现大约一半的情况下拼写为 Received as Recieved 的数据库,它几乎每天都让我感到困惑)

于 2012-11-21T15:40:35.710 回答