-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

問題は次の 2 つのテーブルにあります。

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、letudent_Email) で構成されています。1 つの列が 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は、直感的に評価のスペルが正しいなどの理由でエラーメッセージが表示され続けると、非常にイライラする可能性があります (私はかつて、列名の約半分で Received を Recieved と綴るデータベースを扱ったことがありましたが、ほぼ毎日のように気が付きました)

于 2012-11-21T15:40:35.710 に答える