简体   繁体   English

引用表“exam_subjects”的给定键没有唯一的约束匹配

[英]There is no unique constraint matching given keys for referenced table “ exam_subjects”

CREATE TABLE student
(
    student_id INT PRIMARY KEY,
    first_name VARCHAR(40) NOT NULL,
    last_name VARCHAR(40) NOT NULL,
    birth_day DATE NOT NULL,
    sex VARCHAR(1) NOT NULL,
    student_email_address VARCHAR(40) NOT NULL UNIQUE,
    student_password VARCHAR(10) NOT NULL UNIQUE,
    student_nick_name VARCHAR(10) NOT NULL,
    student_qualification VARCHAR(10) NOT NULL,
    student_documents VARCHAR(255) NOT NULL,
    student_image VARCHAR(100) NOT NULL
);

CREATE TABLE student_feedback
(
    sr_no BIGSERIAL PRIMARY KEY,
    student_id INT NOT NULL,
    feedback_type VARCHAR(10) NOT NULL,
    feedback_text VARCHAR(200) NOT NULL,
    FOREIGN KEY (student_id) REFERENCES student(student_id)
);

CREATE TABLE online_exam
(
    exam_id INT PRIMARY KEY,
    exam_title VARCHAR(20) UNIQUE NOT NULL,
    exam_duration_min INT NOT NULL,
    total_questions INT NOT NULL,
    marks_per_right_answer INT NOT NULL,
    marks_per_wrong_answer INT NOT NULL,
    passing_marks INT NOT NULL,
    exam_status VARCHAR(2)
);

CREATE TABLE exam_subjects
(
    sub_id INT,
    exam_id INT,
    sub_name VARCHAR(20) NOT NULL,
    sub_desc VARCHAR(20) NOT NULL,
    UNIQUE(sub_id,exam_id),
    PRIMARY KEY(sub_id,exam_id),
    FOREIGN KEY (exam_id) REFERENCES online_exam(exam_id) ON DELETE CASCADE
);

CREATE TABLE sub_questions
(
    sub_id1 INT,
    ques_id INT,
    ques_text VARCHAR(150) NOT NULL,
    ques_attachments VARCHAR(255),
    option_1 VARCHAR(20) NOT NULL,
    option_2 VARCHAR(20) NOT NULL,
    option_3 VARCHAR(20) NOT NULL,
    option_4 VARCHAR(20) NOT NULL,
    UNIQUE(sub_id1,ques_id),
    PRIMARY KEY (sub_id1,ques_id),
    FOREIGN KEY (sub_id1) REFERENCES exam_subjects(sub_id) ON DELETE CASCADE
);

CREATE TABLE ques_answers
(
    ans_id INT,
    ques_id INT,
    ans VARCHAR(20) NOT NULL,
    PRIMARY KEY (ans_id,ques_id),
    FOREIGN KEY (ques_id) REFERENCES sub_questions(ques_id) ON DELETE CASCADE
);

CREATE TABLE exam_registration
(
    student_id INT,
    exam_id INT,
    exam_date_time TIMESTAMP NOT NULL,
    PRIMARY KEY (student_id,exam_id),
    FOREIGN KEY (student_id) REFERENCES student(student_id) ON DELETE CASCADE,
    FOREIGN KEY (exam_id) REFERENCES online_exam(exam_id) ON DELETE CASCADE
);

CREATE TABLE exam_result
(
    student_id INT,
    exam_id INT,
    sub_id INT,
    marks_scored INT NOT NULL,
    correct_ques INT NOT NULL,
    wrong_ques INT NOT NULL,
    grade VARCHAR(10) NOT NULL,
    PRIMARY KEY(student_id,exam_id,sub_id),
    FOREIGN KEY (student_id) REFERENCES student(student_id) ON DELETE CASCADE,
    FOREIGN KEY (exam_id) REFERENCES online_exam(exam_id) ON DELETE CASCADE,
    FOREIGN KEY (sub_id) REFERENCES  exam_subjects(sub_id) ON DELETE CASCADE
);

Output :输出 :

CREATE TABLE,创建表,
CREATE TABLE,创建表,
CREATE TABLE,创建表,
CREATE TABLE,创建表,
There is no unique constraint matching given keys for referenced table "exam_subjects", relation "sub_questions" do not exist,引用表“exam_subjects”的给定键没有唯一约束匹配,关系“sub_questions”不存在,
CREATE TABLE,创建表,
There is no unique constraint matching given keys for referenced table "exam_subjects".引用表“exam_subjects”没有与给定键匹配的唯一约束。

Why am I getting "there is no unique..." error in postgresql?为什么我在 postgresql 中收到“没有唯一的...”错误? How to solve it?如何解决? Please help.请帮忙。

In your table exam_subjects the primary key is a compossed key ( PRIMARY KEY(sub_id, exam_id) )在您的表exam_subjects ,主键是一个组合键( PRIMARY KEY(sub_id, exam_id)

Therefore, your table sub_questions must have both columns to create the constraint, otherwise you get the error about unique constraints (or you can create a unique field acting as primary key, and create a unique index on both columns sub_id and exam_id )因此,你的表sub_questions必须有两列来创建约束,否则你会得到关于唯一约束的错误(或者你可以创建一个唯一的字段作为主键,并在sub_idexam_id两列上创建一个唯一索引)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 没有唯一约束匹配给定引用表的键 - No unique constraint matching given keys for referenced table 没有唯一约束与给定键匹配的具有多个主键的引用表 - No unique constraint matching given keys for referenced table with multiple primary keys sqlalchemy模式:没有唯一约束匹配给定表的键 - sqlalchemy schema: there is no unique constraint matching given keys for referenced table PSQL错误,没有唯一约束匹配给定表引用的键 - PSQL Error there is no unique constraint matching given keys for referenced table 错误:没有唯一约束匹配给定键的引用表 - ERROR: there is no unique constraint matching given keys for referenced table 没有与引用表“用户”的给定键匹配的唯一约束(PostgreSQL) - No unique constraint matching given keys for referenced table “users” (PostgreSQL) 错误:没有唯一约束匹配给定键的引用表“bar” - ERROR: there is no unique constraint matching given keys for referenced table "bar" 远离错误:没有唯一约束匹配引用表的给定键 - Away around Error: There is no unique constraint matching given keys for referenced table 错误:没有唯一约束匹配给定表的键 - ERROR: No unique constraint matching given keys for referenced table SQLALCHEMY:没有唯一约束匹配给定键的引用表 - SQLALCHEMY: There is no unique constraint matching given keys for referenced table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM