我的外键 PostgreSQL 代码给出外键约束不存在
My PostgreSQL code for foreign key gives foreign key constraint does not exist
我的代码:
create table courses(
id SERIAL PRIMARY KEY
)
----------------------
create table teachers(
id SERIAL PRIMARY KEY
)
----------------------
CREATE TABLE assignments(
id SERIAL PRIMARY KEY ,
given_date TIMESTAMP,
deadline TIMESTAMP,
FOREIGN KEY(course_id) REFERENCES courses(id),
FOREIGN KEY(teacher_id) REFERENCES teachers(id),
pass_mark INT,
full_mark INT,
description TEXT,
assignment_file VARCHAR(100)
);
错误:
column "course_id" referenced in foreign key constraint does not exist
SQL state: 42703
我在课程和教师 table 的 ID 中插入数据后尝试了 运行 第三个创建 table 代码,但没有改变。
您不能引用那样的专栏。首先,您添加了要创建外键的列,然后引用它。
create table courses(
id SERIAL PRIMARY KEY
);
----------------------
create table teachers(
id SERIAL PRIMARY KEY
);
----------------------
CREATE TABLE assignments(
id SERIAL PRIMARY KEY ,
given_date TIMESTAMP,
deadline TIMESTAMP,
course_id INT,
teacher_id INT,
pass_mark INT,
full_mark INT,
description TEXT,
assignment_file VARCHAR(100),
FOREIGN KEY(course_id) REFERENCES courses(id),
FOREIGN KEY(teacher_id) REFERENCES teachers(id)
);
我的代码:
create table courses(
id SERIAL PRIMARY KEY
)
----------------------
create table teachers(
id SERIAL PRIMARY KEY
)
----------------------
CREATE TABLE assignments(
id SERIAL PRIMARY KEY ,
given_date TIMESTAMP,
deadline TIMESTAMP,
FOREIGN KEY(course_id) REFERENCES courses(id),
FOREIGN KEY(teacher_id) REFERENCES teachers(id),
pass_mark INT,
full_mark INT,
description TEXT,
assignment_file VARCHAR(100)
);
错误:
column "course_id" referenced in foreign key constraint does not exist
SQL state: 42703
我在课程和教师 table 的 ID 中插入数据后尝试了 运行 第三个创建 table 代码,但没有改变。
您不能引用那样的专栏。首先,您添加了要创建外键的列,然后引用它。
create table courses(
id SERIAL PRIMARY KEY
);
----------------------
create table teachers(
id SERIAL PRIMARY KEY
);
----------------------
CREATE TABLE assignments(
id SERIAL PRIMARY KEY ,
given_date TIMESTAMP,
deadline TIMESTAMP,
course_id INT,
teacher_id INT,
pass_mark INT,
full_mark INT,
description TEXT,
assignment_file VARCHAR(100),
FOREIGN KEY(course_id) REFERENCES courses(id),
FOREIGN KEY(teacher_id) REFERENCES teachers(id)
);