2016-04-16 44 views
0

我想爲註冊系統創建一個數據庫。然而我遇到了問題,我沒有一個特定的表的唯一鍵。我不能製作人造密鑰,因爲這會導致數據庫的完整性(在弱實體中使用人造密鑰不是最佳做法)。我怎樣才能解決這個錯誤,在「部分」表中爲了使「ClassEvents」中的行是唯一的?如何讓表格唯一,'沒有與引用表格相匹配的唯一約束'?

 
-- HOLDS A SPECIFIC COURSE WITHOUT THE INSTANCES OF THE CLASS -- 
CREATE TABLE Courses (
    courseID  SERIAL  UNIQUE NOT NULL, 
    department TEXT    NOT NULL, 
    courseNumber VARCHAR(10)  NOT NULL, 
    courseName TEXT    NOT NULL, 
    credits  INT    NOT NULL, 
    PRIMARY KEY(department, courseID) 
); 

-- PEOPLE SUPERTYPE -- 
CREATE TABLE People (
    pid SERIAL   UNIQUE NOT NULL, 
    fname TEXT      NOT NULL, 
    lname TEXT      NOT NULL, 
    PRIMARY KEY(pid) 
); 

-- HOLDS THE DIFFERENT PROFESSORS TEACHING AT THE SCHOOL -- 
-- SUBTYPE OF PEOPLE -- 
CREATE TABLE Professors (
    professorID INT UNIQUE NOT NULL, 
    status  TEXT  NOT NULL, 
    CHECK(status = 'Full-Time' OR status = 'Part-time'), 
    PRIMARY KEY(professorID), 
    FOREIGN KEY(professorID) REFERENCES People(pid) 
); 

-- HOLDS THE SPECIFIC INSTANCES OF THE CLASS DEPENDING ON THE YEAR AND TERM -- 
CREATE TABLE Sections (
    department TEXT   NOT NULL, 
    courseID  INT UNIQUE NOT NULL, 
    year   INT   NOT NULL, 
    term   TEXT   NOT NULL, 
    sectionNumber INT   NOT NULL, 
    startDate  DATE   NOT NULL, 
    endDate  DATE   NOT NULL, 
    PRIMARY KEY(department, courseID, sectionNumber, year, term), 
    FOREIGN KEY(department, courseID) REFERENCES Courses(department, courseID) 
); 

-- HOLDS THE EVENT OF THE CLASS -- 
-- A CLASS MAY HAVE DIFFERENT DAYS ON WHICH -- 
-- THEY MEET ON, SO THIS ALLOWS A CERTAIN -- 
-- SECTION TO HAVE SEVERAL DAYS WITHOUT CONFLICT -- 
CREATE TABLE ClassEvent (
    professorID INT UNIQUE NOT NULL, 
    courseID  INT UNIQUE NOT NULL, 
    sectionNumber INT   NOT NULL, 
    year   INT   NOT NULL, 
    term   TEXT   NOT NULL, 
    day   TEXT, 
    startTime  TIME, 
    endTime  TIME, 
    location  TEXT, 
    campus  TEXT, 
    CHECK(day = 'Monday' OR day = 'Tuesday' OR day = 'Wednesday' OR day = 'Thursday' OR day = 'Friday' OR day = 'Saturday' OR day = 'Sunday' OR day IS NULL), 
    PRIMARY KEY(professorID, courseID, sectionNumber, year, term, day, startTime, endTime), 
    FOREIGN KEY(professorID) REFERENCES Professors(professorID), 
    FOREIGN KEY(courseID, sectionNumber, year, term) REFERENCES Sections(courseID, sectionNumber, year, term) 
); 

-- HOLDS THE STUDENTS THAT WILL BE TAKING THE CLASSES -- 
-- SUBTYPE OF PEOPLE -- 
CREATE TABLE Students (
    studentID INT REFERENCES People(pid) UNIQUE NOT NULL, 
    studentName TEXT        NOT NULL, 
    gradYear DATE      UNIQUE NOT NULL, 
    PRIMARY KEY(studentID) 
); 


-- HOLDS A CLASS RECORD FOR STUDENTS (AND POSSIBLY PROFESSORS) -- 
CREATE TABLE Enrollment (
    studentID INT REFERENCES Students(studentID) UNIQUE NOT NULL, 
    crn  INT REFERENCES Sections(crn)    NOT NULL, 
    grade  TEXT          NOT NULL, 
    PRIMARY KEY(studentID, crn) 
); 

-- HOLDS THE DIFFERENT DEGREES THAT CAN BE ATTAINED AT THE COLLEGE/UNIVERSITY -- 
CREATE TABLE Degrees (
    degreeID  SERIAL  UNIQUE NOT NULL, 
    degreeName TEXT    NOT NULL, 
    degreeType TEXT    NOT NULL, 
    degDepartment VARCHAR(4)   NOT NULL, 
    CHECK(degreeType = 'Major' OR degreeType = 'Minor' OR degreeType = 'Masters'), 
    PRIMARY KEY(degreeID) 
); 

-- HOLDS THE CLASSES THAT WILL MAKE UP A DEGREE -- 
CREATE TABLE DegreeReq (
    degreeID INT REFERENCES Degrees(degreeID) UNIQUE NOT NULL, 
    courseID INT REFERENCES Courses(courseID) UNIQUE NOT NULL, 
    PRIMARY KEY(degreeID, courseID) 
); 

-- HOLDS THE INSTANCE OF A DEGREE FOR A CERTAIN STUDENT -- 
-- FOR EXAMPLE: A STUDENT CAN HAVE A MAJOR AND A MINOR -- 
-- SO HE/SHE CAN STORE THEM SEPARATELY -- 
CREATE TABLE DegreeInstance (
    degreeID  INT REFERENCES Degrees(degreeID) UNIQUE NOT NULL, 
    studentID  INT REFERENCES Students(studentID) UNIQUE NOT NULL, 
    startDate  DATE          NOT NULL, 
    endDate   DATE          NOT NULL, 
    creditsRequired INT          NOT NULL, 
    PRIMARY KEY(degreeID, studentID) 
); 

-- HOLDS ALL THE RATE MY PROFESSOR STATS -- 
CREATE TABLE Stats (
    professorID INT  REFERENCES Professors(professorID) UNIQUE NOT NULL, 
    dateSubmitted TIMESTAMP         UNIQUE NOT NULL, 
    rating  FLOAT            NOT NULL, 
    helpfulness FLOAT            NOT NULL, 
    clarity  FLOAT            NOT NULL, 
    easiness  FLOAT            NOT NULL, 
    PRIMARY KEY(professorID, dateSubmitted) 
); 
 
ERROR: there is no unique constraint matching given keys for referenced table "sections" 
********** Error ********** 

ERROR: there is no unique constraint matching given keys for referenced table "sections" 
SQL state: 42830 
+1

爲什麼在複合主鍵中有串行列?這沒有意義。 –

+0

爲什麼它沒有意義?我是數據庫新手,所以我盡我所能,但我非常肯定強大的實體允許使用人造鍵。 @GordonLinoff –

+0

@MBarbieri。 。 。串行列在每一行都是唯一的。沒有理由在主鍵中包含其他列。 –

回答

1

Sections表中定義(department, courseID, sectionNumber, year, term)作爲主鍵,而在表ClassEvents你引用集合(courseID, sectionNumber, year, term)無場department。你必須引用整個鍵而不是它的一部分。因此,添加此字段來解決問題

+0

謝謝!像魔術一樣工作。 –

相關問題