-5

Possible Duplicate:
Multiple-column foreign key in MySQL?

i keep getting this error while writing queries in databses:

The number of columns in the referencing column list for foreign key 'FK__Asses__a_Type__4DB4832C' does not match those of the primary key in the referenced table 'Course_Assesments'

this is the code i wrote :

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)
Community
  • 1
  • 1
Tharwat7
  • 1
  • 1
  • 5

1 Answers1

0

Your problem is from these two tables:

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
)

You are telling the database that Asses.a_Type is equal to the primary key in Course_Assesments however the primary key in course_assesments is made up of 3 columns (a_Type, student_Email, lecturer_Email), since one column cannot equal the tuple from 3 columns you get an error.

If you really want Asses to reference Course_Assesments you need a table like this:

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
)

Finally, not related to your question, you have spelt Assessment wrongly throughout, this does not make any difference, but can be very frustrating for anyone else using the database when they keey getting error messages such as Invalid Object Name Course_Assessment because they have intuitively spelt Assessment correctly (I once dealt with a database that spelt Received as Recieved in about half the appearances in column names, it caught me out almost daily)

GarethD
  • 68,045
  • 10
  • 83
  • 123