类型继承与 Table 继承
Type Inheritance vs Table Inheritance
我正在阅读 Silberschatz 一书中有关基于对象的数据库的内容。它通过以下示例解释了类型继承:
create type Person
(
name varchar(20),
address varchar(20)
);
现在我们可以像这样使用类型继承:
create type Student under Person
(
degree varchar(20),
department varchar(20)
);
我想我明白了这一点。现在我创建一个 table 类型的 Person 为:
create table people of Person;
但是现在如果我想创建一个 table 类型的学生。我应该这样做吗:
create table students of Student;
或
create table students of Student under people;
/*which is described in the book as table inheritance*/
以上两者有什么区别?既然我们在定义 Student 类型时已经继承了 Person,那么在创建 Student 类型的 table 时是否有必要继承 Person(人)类型的 table?
如果有人能用一个例子来解释这一点,那就更清楚了。
不幸的是,类 和继承的世界与数据库的世界并不总能很好地相互映射。
在此示例中,一种方法是创建 2 tables:
人
- Person_Id = table
的主键
- 姓名
- 地址
学生
- Student_Id = table
的主键
- 学位
- 部门
- Person_Id = Person 的外键 table
如果你让Student.Person_Id不为null,这意味着每个学生在Persontable中都有一行。如果这对您很重要,您将需要其他东西,索引或约束(在数据库中)或逻辑(在操作数据库的代码中)来强制 Student 记录不共享 Person 记录。
这些 table 的示例数据:
人
- Person_Id: 12, 姓名: Mr. Not-a-student, 地址: 999 Letsbe Avenue
- Person_Id: 13, 姓名: 研究生女士, 地址: A Swanky Campus
- Person_Id: 14, 姓名: 本科生先生, 地址: Not-so-swanky Campus
- Person_Id: 15, 姓名: Ms. Not-a-student-either, 地址: 2B Ornottoobee Road
学生
- Student_Id: 859, 学历: Tricky Maths, 系: Maths, Person_Id: 13
- Student_Id: 860: 学位: 说唱歌词, 部门: 英语, Person_Id: 14
12号和15号是人,不是学生。人 13 和 14 分别链接到学生 859 和 860.
请注意,如果你真的在构建它,你可能会把事情分开一点(用数据库的话说,规范化事情):
地址
- Address_Id = 主键
- Address_line_1
- 等等
人
- Person_Id = 主键
- 姓名
- Address_Id = 地址 table
的外键
度数
- Degree_Id = 主键
- 主题
- 等等
部门
- Department_Id = 主键
- 姓名
- Address_Id = 地址(所在部门)的外键
- Boss = Person 的外键
学生
- Student_Id = 主键
- Degree_Id = Degree
的外键
- Department_Id = 部门的外键
- Person_Id = Person
的外键
我正在阅读 Silberschatz 一书中有关基于对象的数据库的内容。它通过以下示例解释了类型继承:
create type Person
(
name varchar(20),
address varchar(20)
);
现在我们可以像这样使用类型继承:
create type Student under Person
(
degree varchar(20),
department varchar(20)
);
我想我明白了这一点。现在我创建一个 table 类型的 Person 为:
create table people of Person;
但是现在如果我想创建一个 table 类型的学生。我应该这样做吗:
create table students of Student;
或
create table students of Student under people;
/*which is described in the book as table inheritance*/
以上两者有什么区别?既然我们在定义 Student 类型时已经继承了 Person,那么在创建 Student 类型的 table 时是否有必要继承 Person(人)类型的 table?
如果有人能用一个例子来解释这一点,那就更清楚了。
不幸的是,类 和继承的世界与数据库的世界并不总能很好地相互映射。
在此示例中,一种方法是创建 2 tables:
人
- Person_Id = table 的主键
- 姓名
- 地址
学生
- Student_Id = table 的主键
- 学位
- 部门
- Person_Id = Person 的外键 table
如果你让Student.Person_Id不为null,这意味着每个学生在Persontable中都有一行。如果这对您很重要,您将需要其他东西,索引或约束(在数据库中)或逻辑(在操作数据库的代码中)来强制 Student 记录不共享 Person 记录。
这些 table 的示例数据:
人
- Person_Id: 12, 姓名: Mr. Not-a-student, 地址: 999 Letsbe Avenue
- Person_Id: 13, 姓名: 研究生女士, 地址: A Swanky Campus
- Person_Id: 14, 姓名: 本科生先生, 地址: Not-so-swanky Campus
- Person_Id: 15, 姓名: Ms. Not-a-student-either, 地址: 2B Ornottoobee Road
学生
- Student_Id: 859, 学历: Tricky Maths, 系: Maths, Person_Id: 13
- Student_Id: 860: 学位: 说唱歌词, 部门: 英语, Person_Id: 14
12号和15号是人,不是学生。人 13 和 14 分别链接到学生 859 和 860.
请注意,如果你真的在构建它,你可能会把事情分开一点(用数据库的话说,规范化事情):
地址
- Address_Id = 主键
- Address_line_1
- 等等
人
- Person_Id = 主键
- 姓名
- Address_Id = 地址 table 的外键
度数
- Degree_Id = 主键
- 主题
- 等等
部门
- Department_Id = 主键
- 姓名
- Address_Id = 地址(所在部门)的外键
- Boss = Person 的外键
学生
- Student_Id = 主键
- Degree_Id = Degree 的外键
- Department_Id = 部门的外键
- Person_Id = Person 的外键