如何解决PostgreSQL-一对一-哪种方法“更好”?
我想在两个表之间创建一对一关系:teachers
和office
(老师是关系的所有者)。
在网上查找时,我发现了两种不同的方法:
第一个,在office表中定义一个foreign key
并添加约束NOT NULL
和UNIQUE
。基本上,带有UNIQUE约束的强制一对多关系:
像这样:
CREATE TABLE office(
id SERIAL PRIMARY KEY,floor SMALLINT CHECK (floor > 0 AND floor <= 10),size SMALLINT CHECK (size > 0),teacher_id INTEGER NOT NULL UNIQUE REFERENCES teachers(teacher_id)
);
第二个是我们将Office表中的primary key
和{strong> foreign key
组合成到一行。像这样:
CREATE TABLE office(
id SERIAL PRIMARY KEY REFERENCES teachers(teacher_id),size SMALLINT CHECK (size > 0)
);
在功能上,在我看来这两种方法的工作原理相同。第一个似乎占用了一点额外的内存,因为它还有一行。
我的问题是:每种方法的优点和缺点是什么? 一种方法比另一种“更好”吗?
解决方法
如果这确实是1-1关系,为什么不只在teachers
表中包括描述办公室的列?
您可能不想这样做是有原因的。一个重要原因是office
可以不存在teacher
而存在。例如,如果您的学校没有暑期课程,那么办公室将继续存在。
这表明办公室独立于教师。因此,您需要一个单独的表,并为其提供单独的主键。
在您的第二个模型中-主键 是teachers
的外键-您实际上是在说办公室是教师的一种,而不是一个单独的实体。
不过有两个注释:
首先,Postgres不推荐generated always as identity
,而不推荐serial
。第二张表不应使用serial
列作为主键;它应该是int
:
CREATE TABLE office (
id int PRIMARY KEY REFERENCES teachers(teacher_id),floor SMALLINT CHECK (floor > 0 AND floor <= 10),size SMALLINT CHECK (size > 0)
);
将自动分配的列作为外键引用没有意义。
,我将采用第二种方法,为主键共享完全相同的值。对于office
表,主键不应自动生成(serial
),而应自动生成int
。
例如,您可以这样做:
create table teachers (
id int primary key not null,name varchar(20) not null
);
create table office (
id int primary key references teachers (id),floor smallint check (floor > 0 and floor <= 10),size smallint check (size > 0)
);
alter table teachers
add constraint fk_uq1_teachers_office
foreign key (id) references office (id) deferrable initially deferred;
然后插入数据如下:
begin transaction;
insert into teachers (id,name) values (100,'Mary');
insert into office (id,floor,size) values (100,3,850);
commit;
请注意,要强制执行1-1关系,您需要具有两个外键:一个从teachers
到office
,另一个从{{1} }至office
。否则,应用程序可能会将数据插入teachers
,而忘记插入teachers
。幸运的是,您使用的PostgreSQL实现了约束可延迟性的 standard SQL功能。
请参见DB Fiddle上的运行示例。
,双向强制外键a可能成为维护的噩梦。另一种选择是将教师上岗建模为m:m,然后放置部分唯一索引来约束他们。由于老师和办公室都是独立的实体,因此具有极大的灵活性,并且如果需要,您将拥有老师和办公室的关系历史记录。就目前情况而言,除非指派了老师,否则办公室将不存在。 (您可以将office.teacher设置为可空值。并且至少应该这样做。)采用m:m结构,表定义将变为:
create table teachers (
id int primary key not null,name varchar(20) not null
);
create table offices (
id int primary key,size smallint check (size > 0)
);
create table teacher_offices
(id integer generated always as identity,teacher_id integer not null,office_id integer not null,effective_date date not null default now()::date,end_date date,constraint teacher_office_pk
primary key(id),constraint to2office_fk
foreign key (office_id)
references offices (id)
on delete cascade,constraint to2teacher_fk
foreign key (teacher_id)
references teachers (id)
on delete cascade
);
-- create the partial unique index
create unique index office_already_occupied_by_teacher
on teacher_offices (office_id)
where end_date is null;
create unique index teacher_already_has_office
on teacher_offices (teacher_id)
where end_date is null;
在这里查看full example。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。