sql - 如何创建一个 ISA 层次结构,其中 SQL 中只允许存在一种子类型?
问题描述
例如,我有一张 Person 表格。该表如下所示:
create table Person (id int not null primary key,
FirstName varchar(20) not null,
LastName varchar(20) not null
)
ISA Hierarchy 有 2 个扩展类型,Staff 和 Student,在它下面如下:
create table Staff (
id int not null primary key references Person(id),
department varchar(20) not null
)
create table Student (
id int not null primary key references Person(id),
course varchar(20) not null
)
从当前的实现中,我可能会创建一个 Student 条目和 Staff 条目,它们都与同一个 Person ID 相关。我试图使其具有排他性,以便一个人只能成为员工或学生,但我对如何做到这一点有点迷茫。
任何输入表示赞赏!
解决方案
在现实世界中,某人可能既是工作人员又是学生,但您所描述的情况是某人可能只是其中之一。因此,“员工或学生”类别是个人的单值属性,因此该类别值应存储在Person
表中。为了约束子表,表应该在两个和那个类别列Person
上都有一个额外的唯一索引,然后你的和表也应该包括这个列,并在外键中使用它返回到,并有一个检查约束来确保只有适当的值(因此,人的类别)被输入到每个子表中。id
Staff
Student
Person
推荐阅读
- python - pd.to_datetime 与 time.mktime 不一致
- nginx - Shopify 和 Squarespace 等网站在设置自定义域时如何自动配置反向代理?
- xcode10 - 如何在 Xcode10 上添加代码块?
- javascript - 如何在 Chrome 扩展的 Active Tab 的两个页面上运行后台 JS
- c3.js - 基于 C3js 圆环图数据的条件颜色
- java - 哪个 JAR 用于 JSTL?
- ubuntu - 在训练模型时运行应用程序
- messaging - 基于事件的系统中的消息格式
- php - AWS SQS 错误:SignatureDoesNotMatch HTTP 错误:403
- magento2 - 无法使用 magento 2 REST API 取消订单