当前位置: 首页 > 知识库问答 >
问题:

PostgreSQL错误:没有唯一约束匹配引用表的给定键

燕琨
2023-03-14

我正在为班级注册建立一个数据库,但我在“注册”表上遇到了问题。我需要将分区中的所有主键传递到注册中,但我还想传递一个附加键(“sectionNumber”)。然而,我不想让“sectionNumber”成为“Sections”主键的一部分,因为我不想将sectionNumber传递到每个我有外键的表中,这些外键来自“Sections”有人对如何解决这个问题有什么建议吗?我一直得到下面的错误。

-- 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        UNIQUE NOT NULL,
    credits       INT                NOT NULL,
    PRIMARY KEY(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 (
    courseID      INT          NOT NULL,
    year          INT          NOT NULL,
    term          TEXT         NOT NULL, 
    sectionNumber INT          NOT NULL,
    startDate     DATE         NOT NULL,
    endDate       DATE         NOT NULL,
    crn           INT          NOT NULL,
    CHECK(term = 'Fall' OR term = 'Winter' OR term = 'Spring' OR term = 'Summer'),
    PRIMARY KEY(courseID, year, term),
    FOREIGN KEY(courseID) REFERENCES Courses(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          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),
    CHECK(term = 'Fall' OR term = 'Winter' OR term = 'Spring' OR term = 'Summer'),
    PRIMARY KEY(professorID, courseID, year, term, day, startTime, endTime),
    FOREIGN KEY(professorID) REFERENCES Professors(professorID),
    FOREIGN KEY(courseID, year, term) REFERENCES Sections(courseID, year, term)
);

-- GENERATES THE PREREQUESITES --
CREATE TABLE Prerequisites (
    courseID      INT        NOT NULL,
    year          INT        NOT NULL,
    term          TEXT       NOT NULL,
    prereqID      INT        NOT NULL,
    CHECK(term = 'Fall' OR term = 'Winter' OR term = 'Spring' OR term = 'Summer'),
    PRIMARY KEY(courseID, year, term, prereqID),
    FOREIGN KEY(courseID, year, term) REFERENCES Sections(courseID, year, term),
    FOREIGN KEY(prereqID) REFERENCES Courses(courseID)
);


-- 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  UNIQUE NOT NULL,
    courseID      INT         NOT NULL,
    year          INT         NOT NULL,
    term          TEXT        NOT NULL,
    sectionNumber INT         NOT NULL,
    grade         TEXT,
    CHECK(grade = 'A' OR grade = 'A-' OR grade = 'B+' OR grade = 'B' OR grade = 'B-' OR grade = 'C+' OR grade = 'C' OR grade = 'C-' OR grade = 'D+' OR grade = 'D' OR grade = 'D-' OR grade = 'F' OR grade = 'P/F'),
    CHECK(term = 'Fall' OR term = 'Winter' OR term = 'Spring' OR term = 'Summer'),
    PRIMARY KEY(studentID, courseID, year, term, sectionNumber),
    FOREIGN KEY(studentID) REFERENCES Students(studentID),
    FOREIGN KEY(courseID, year, term) REFERENCES Sections(courseID, year, term),
    FOREIGN KEY(sectionNumber) REFERENCES Sections(sectionNumber)
);

-- 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)        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 Rating (
    professorID      INT        UNIQUE NOT NULL,
    rmpID            BIGINT     UNIQUE NOT NULL,
    avgRating        FLOAT             NOT NULL,
    avgHelpfulness   FLOAT             NOT NULL,
    avgClarity       FLOAT             NOT NULL,
    avgEasiness      FLOAT             NOT NULL,
    PRIMARY KEY(professorID, rmpID),
    FOREIGN KEY(professorID) REFERENCES Professors(professorID)
);
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个答案

梁丘佑运
2023-03-14

您已声明此关系:

FOREIGN KEY(sectionNumber) REFERENCES Sections(sectionNumber)

sectionNumber未声明为唯一。您需要修复数据结构或使用主键。

 类似资料:
  • 我试着用这些代码使用postgresql创建表,但人们一直说,并没有唯一的约束匹配引用表的给定键。当我试图创建表、节和评估时,出现了这个消息。 请帮帮我。 )我更改了create语句的顺序,并在表中添加了unique key,但仍然不起作用。

  • PostgreSQL/PGAdmin4错误:没有唯一的约束匹配引用表的给定键 这是我试图编码到PGAdmin4/Postgresql:http://i.imgur.com/xPEu8Sh.jpg的模式 我能够转换所有表格,除了“资格证书”。 我尝试处理以下查询: 我收到以下消息:错误:没有唯一的约束匹配引用表“section”的给定键 这些是我在表格部分中介绍的外国和主要内容 普凯:我,伊姆格。c

  • 试图在Postgres 9.1中创建此示例表结构: 运行上述代码会产生一个错误,这对我来说没有意义: 有人能解释为什么会出现这种错误吗?

  • 所以我得到了一个错误,没有唯一的约束匹配我的一个表的给定键。我有两个表:用户和项目,其中我们有一个多对一的关系(一个用户有多个项目)。我用炼金术来做这一切: 要创建表,我在我的应用程序中有以下语句。py': 错误是:没有唯一的约束匹配引用表用户的给定键。 我不确定哪里出错了,因为我在ItemModel中有一个外键与UserModel中的主键匹配。欢迎任何建议/提示! 谢谢

  • 问题内容: 我有一个表定义: 我正在尝试创建失败的表 错误是: 我不确定如何解决此问题。 问题答案: 我认为您正在寻找两个单独的外键:

  • 我在创建一个包含外键的表时遇到问题,该外键来自另一个包含两个主键的表。 以下是表格: 无法创建表risk_final并给我错误: 错误:没有唯一的约束匹配引用表"names_types"的给定键 我该怎么解决呢?