简体   繁体   English

对于派生数据库结构,什么是好的设计?

[英]What is a good design for a derived database structure?

The input for filling the tables is a big file, which has unique animalIds, their names, their animal types and some other animal type specific values. 填写表格的输入是一个大文件,其中包含唯一的animalId,它们的名称,它们的动物类型以及其他一些特定于动物类型的值。

The database looks like this: 该数据库如下所示:

table animal:
- animalId(pk)
- name
- type(fk to animalType table)   

table dogs:         
- dogId(pk)         
- animalId(fk to animal)    
- hasCoat           
- etc...            

table fish:
- fishId(pk)
- animalId(fk to animal)
- animalId(fk to animal)
- dorsalFinCount
- etc...

I designed it this way, because my other source gives me an unique key (animalId, which identifies the single animal all over the world...like an uri) and i dont want to have one big single database with empty values (eg for hasCoat and hasDorsalFin) nor have to search in two type specific databases successively. 我以这种方式设计了它,因为我的另一个来源给了我一个唯一的键(animalId,它标识了全世界的单个动物……就像一个uri),而且我不想拥有一个大的带有空值的单一数据库(例如, hasCoat和hasDorsalFin),也不必连续​​搜索两个类型特定的数据库。 Furthermore, i can easily extend this structure (table birds, with attribute hasWing, wingColor, etc...) 此外,我可以轻松扩展此结构(具有属性hasWing,wingColor等的表鸟)

My (Java) program design looks like this: 我的(Java)程序设计如下:

abstract class Animal
- animalId
- name
- type

class Dog extends DatabaseObject 
class Fish extends DatabaseObject 

abstract class DatabaseObject 
    private UUID id;
    public DatabaseObject(){this.uuid = UUID.randomUUID();}
    public UUID getId();

The Problem is, that Dog and Fish classes are Animals, but need to have a UUID for each database entry, because the unique animalId is used as PK in the animal table. 问题是,Dog和Fish类是Animals,但是每个数据库条目都需要具有UUID, 因为在animal表中将唯一的animalId用作PK。 Furthermore Dog and Fish should also extend Animal, because they need those data fields...but thats not possible in Java, because they allready extend the abstract DatabaseObject class. 此外,Dog and Fish还应该扩展Animal,因为它们需要这些数据字段...但是在Java中这是不可能的,因为它们已经扩展了抽象DatabaseObject类。

A solution could be, that Dog and Fish get an Animal field as class member, but that seems to be a bad approach. 一个解决方案可能是,“狗和鱼”成为动物类的班级成员,但这似乎是一个不好的方法。

How would you solve this Problem and what are the pros for your design (database and program structure)? 您将如何解决此问题?您的设计(数据库和程序结构)的优点是什么?

edit: Since my assumption (...that a PK cant be used as a PK in another Table) is wrong, i could change dogId and fishId into animalId. 编辑:由于我的假设(...一个PK不能用作另一个表中的PK)是错误的,因此我可以将dogId和fishId更改为animalId。 Furthermore, such a database design is known as subtyping 此外,这种数据库设计被称为子类型化

Dont reinvent the wheel, use an ORM. 不要重新发明轮子,使用ORM。 If its ok for you to use Hibernate, there already is an Inheritance Mapping feature. 如果您可以使用Hibernate,则已经有了继承映射功能。

Your design resembles a design pattern known as Class Table Inheritance. 您的设计类似于称为“类表继承”的设计模式。 You can visit the tag with the same name right here, or you can see Martin Fowler's treatment of the same topic by searching on the web. 您可以在此处访问具有相同名称的标签,也可以通过在网络上搜索来查看Martin Fowler对相同主题的处理方式。

There is one small change I would suggest for you. 我会为您建议一个小改动。 It may or may not work in your case. 在您的情况下,它可能有效也可能无效。 It's called "Shared Primary Key". 它称为“共享主键”。 You get rid of DogId and FishId. 您摆脱了DogId和FishId。 Instead, you make AminalID the PK for the dog and fish tables as well as the animal table. 而是将AminalID设置为狗和鱼表以及动物表的PK。 Note that AnimalID will be BOTH a PK and an FK. 请注意,AnimalID将同时是PK和FK。 And you, of course, have to make it a true copy of the PK from Animal. 而且,您当然必须使其成为Animal中PK的真实副本。

This has several advantages, among them simplicity and speed. 这具有几个优点,其中包括简单性和速度。 But the big advantage is that it enforces the one-to-one nature of the IS-A relationship between each subclass and the superclass. 但是最大的好处是,它强制了每个子类和超类之间IS-A关系的一对一性质。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用Java表示直接非循环图的好设计模式/结构是什么? - What is a good design pattern/structure to represent Direct Acyclic Graph in Java? 我的数据库设计好吗? - Is my database design is good? 在Java中的数据库中基于布尔值执行方法的好设计是什么? - What is a good design to execute the methods based on boolean values in Database in Java? 使用Hibernate进行分层数据版本控制的最佳数据库设计是什么? - What's a good database design for hierarchical data versioning with Hibernate? 将派生类存储在数据库中的良好实践 - Good practice to store derived classes in the database 将所有派生类的setter放到util类中是一个好的设计吗? - Is placing all setters of derived class to util class a good design? 用Java渲染这个设计有什么好的布局? - What is a good layout to render this design in Java? 什么是跟踪班级问题的良好设计模式? - What is a good design pattern for tracking issues in a class? 什么是文件创建的好设计模式? - What is a good design pattern for file creation? 此实体层次结构的良好持久性设计是什么? - What is a good persistence design for this entity hierarchy?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM