如何以规范化数据库形式存储用户当前的就业情况? (我正在使用 PostgreSQL)
How can I store current employment for a user in normalised database form? (I am using PostgreSQL)
我有一个 USERS
table 和一个 EMPLOYMENT
table 将多个就业链接到一个用户。我如何为用户存储当前的工作,并限制他们可能没有当前的工作,而且他们只能有一个当前的工作。
就业table isCurrent
中的一个字段是否可以工作,因为默认情况下没有限制只有一个当前就业?
我考虑的另一个首选替代方案是有一个 USER_CURRENT_EMPLOYMENT
table 将用户链接到工作,但是我需要什么限制才能工作?
如前所述,我正在使用 postgresql,但我更好奇这种关系应该如何工作,而不管语言如何。
使用唯一索引和 where 谓词强制用户和 his/her 最后一天的就业(可为空)的唯一性。这将确保每个用户只有一行在 last_day.
中具有 NULL 值
"创建关于就业的唯一索引 idx_current_employer (user_id, (last_day IS NULL)) WHERE last_day IS NULL;"
这里有一个完整的脚本来说明如何使用它:
drop table if exists users;
drop table if exists employment;
create table users
(user_id int not null primary key,
user_name varchar(30) not null)
;
create table employment
(id SERIAL PRIMARY KEY,
user_id int not null,
employer_id int not null,
last_day date null)
;
--insert joe and his previous 2 employers
insert into users
values(1,'Joe');
insert into employment (user_id, employer_id, last_day)
values(1,1,'20150831');
insert into employment (user_id, employer_id, last_day)
values(1,2,'20200831');
--unique index
create unique index idx_current_employer on employment (user_id, (last_day IS NULL)) WHERE last_day IS NULL;
--insert Joe's current employer (null last day)
insert into employment (user_id, employer_id, last_day)
values(1,3,null);
--this one fails - can't insert another employer with null last day
insert into employment (user_id, employer_id, last_day)
values(1,6,null);
--set last day of previous employer first
update employment
set last_day = '20201006'
where user_id = 1
and last_day is null
;
--now insert succeeds
insert into employment (user_id, employer_id, last_day)
values(1,6,null);
--list all employment
select user_id, employer_id, last_day, case when last_day is null then True else False end as is_current
from employment
order by 1, 4 desc, 3 desc
;
我有一个 USERS
table 和一个 EMPLOYMENT
table 将多个就业链接到一个用户。我如何为用户存储当前的工作,并限制他们可能没有当前的工作,而且他们只能有一个当前的工作。
就业table isCurrent
中的一个字段是否可以工作,因为默认情况下没有限制只有一个当前就业?
我考虑的另一个首选替代方案是有一个 USER_CURRENT_EMPLOYMENT
table 将用户链接到工作,但是我需要什么限制才能工作?
如前所述,我正在使用 postgresql,但我更好奇这种关系应该如何工作,而不管语言如何。
使用唯一索引和 where 谓词强制用户和 his/her 最后一天的就业(可为空)的唯一性。这将确保每个用户只有一行在 last_day.
中具有 NULL 值"创建关于就业的唯一索引 idx_current_employer (user_id, (last_day IS NULL)) WHERE last_day IS NULL;"
这里有一个完整的脚本来说明如何使用它:
drop table if exists users;
drop table if exists employment;
create table users
(user_id int not null primary key,
user_name varchar(30) not null)
;
create table employment
(id SERIAL PRIMARY KEY,
user_id int not null,
employer_id int not null,
last_day date null)
;
--insert joe and his previous 2 employers
insert into users
values(1,'Joe');
insert into employment (user_id, employer_id, last_day)
values(1,1,'20150831');
insert into employment (user_id, employer_id, last_day)
values(1,2,'20200831');
--unique index
create unique index idx_current_employer on employment (user_id, (last_day IS NULL)) WHERE last_day IS NULL;
--insert Joe's current employer (null last day)
insert into employment (user_id, employer_id, last_day)
values(1,3,null);
--this one fails - can't insert another employer with null last day
insert into employment (user_id, employer_id, last_day)
values(1,6,null);
--set last day of previous employer first
update employment
set last_day = '20201006'
where user_id = 1
and last_day is null
;
--now insert succeeds
insert into employment (user_id, employer_id, last_day)
values(1,6,null);
--list all employment
select user_id, employer_id, last_day, case when last_day is null then True else False end as is_current
from employment
order by 1, 4 desc, 3 desc
;