设计SQL数据库以表示OO类层次结构 [英] Designing SQL database to represent OO class hierarchy

查看:115
本文介绍了设计SQL数据库以表示OO类层次结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将类层次结构转换为存储在SQL数据库中。

I'm in the process of converting a class hierarchy to be stored in an SQL database.

原始伪代码:

abstract class Note
{
   int id;
   string message;
};

class TimeNote : public Note
{
   time_t time;
};

class TimeRangeNote : public Note
{
   time_t begin;
   time_t end;
};

class EventNote : public Note
{
   int event_id;
};

// More classes deriving from Note excluded.

目前,我有几个想法如何将其存储在数据库中。

Currently I'm having a couple of ideas how to store this in a database.

A。将所有笔记存储在单个宽表中

该表将包含从注释

The table would contain all information needed by all classes deriving from Note.

CREATE TABLE t_note(
   id INTEGER PRIMARY KEY,
   message TEXT,
   time DATETIME,
   begin DATETIME,
   end DATETIME,
   event_id INTEGER
);

注释派生的未来类需要添加新列到此表。

Future classes deriving from Note need to add new columns to this table.

B。将每个类映射到表

CREATE TABLE t_note(
   id INTEGER PRIMARY KEY,
   message TEXT
);

CREATE TABLE t_timenote(
   note_id INTEGER PRIMARY KEY REFERENCES t_note(id),
   time DATETIME
);

CREATE TABLE t_timerangenote(
   note_id INTEGER PRIMARY KEY REFERENCES t_note(id),
   begin DATETIME,
   end DATETIME
);

CREATE TABLE t_eventnote(
   note_id INTEGER PRIMARY KEY REFERENCES t_note(id),
   event_id INTEGER
);

注释派生的未来类需要创建一个新的表。

Future classes deriving from Note need to create a new table.

C。使用数据库规范化和 VARIANT / SQL_VARIANT

C. Use database normalization and VARIANT/SQL_VARIANT

CREATE TABLE t_note(
   id INTEGER PRIMARY KEY,
   message TEXT
);

CREATE TABLE t_notedata(
   note_id INTEGER REFERENCES t_note(id),
   variable_id TEXT, -- or "variable_id INTEGER REFERENCES t_variable(id)".
                     -- where t_variable has information of each variable.
   value VARIANT
);

注释派生的未来类需要添加新的 variable_id

Future classes deriving from Note need to add new variable_id.

D。将每个具体课程映射到表格 (根据当前答案新增)

CREATE TABLE t_timenote(
   id INTEGER PRIMARY KEY,
   message TEXT,
   time DATETIME
);

CREATE TABLE t_timerangenote(
   id INTEGER PRIMARY KEY,
   message TEXT,
   begin DATETIME,
   end DATETIME
);

CREATE TABLE t_eventnote(
   id INTEGER PRIMARY KEY,
   message TEXT,
   event_id INTEGER
);

注释派生的未来类需要创建一个新表。

Future classes deriving from Note need to create a new table.

SQL中最合理的表示是什么?

是否有任何更好的选择?

What would be the most logical representation in SQL be?
Are there any better options?

推荐答案

一般来说,我更喜欢obtionB(即一个表为基类和一个表为每个具体子类)。

In general I prefer obtion "B" (i.e. one table for base class and one table for each "concrete" subclass).

当然这有一些缺点:首先你必须加入至少2个表,每当你读完一个子类的完整实例。此外,任何必须以任何方式操作的人都将不断访问基础表。

Of course this has a couple of drawbacks: first of all you have to join at least 2 tables whenever you have to read a full instance of a subclass. Also, the "base" table will be constantly accessed by anyone who has to operate on any kind of note.

但是,除非您有极端的情况(数十亿美元)的行,非常快的响应时间需要等等)。

But this is usually acceptable unless you have extreme cases (billions of rows, very quick response times required and so on).

还有第三个可能的选项:将每个子类映射到不同的表。这有助于分区对象,但一般来说,在开发工作中花费更多。

There is a third possible option: map each subclass to a distinct table. This helps partitioning your objects but costs more in development effort, in general.

请参阅 这个 进行完整的讨论。

See this for a complete discussion.

(关于您的C解决方案,使用VARIANT:我不能评论优点/缺点,因为它看起来像一个专有解决方案 - 它是什么? -SQL?我不熟悉它)。

(Regarding your "C" solution, using VARIANT: I can't comment on the merits/demerits, because it looks like a proprietary solution - what is it ? Transact-SQL? and I am not familiar with it).

这篇关于设计SQL数据库以表示OO类层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆