【发布时间】:2011-11-16 05:20:54
【问题描述】:
这是面向 OOP 数据库的关系模型,哪个更好?:
注意:->此操作数用于定义外键,如(field->table(reference))
-
第一
**
属性 (id:auto, 属性名)
type (id:auto, type_name) type_attribute (id:auto, type_code->type(id), attribute_id->attribute(id), default_value) object (id:auto, name, object_type->type(id)) object_property (id:auto, object_id->object(id), attribute_id->attribute(id), my_value)
**
-
第二
属性(id:auto,attribute_name)
类型(id:auto, type_name)
type_attribute (id:auto, type_code->type(id), attribute_code->attribute(id), default_value)
object (id:auto, name, object_type->type(id))
object_property (id:auto, (object_id, object_type)->object(id,object_type), (object_type, attribute_id)->type_attribute(id, attribute_id), my_value)
在 object_property 表中确实可以清楚地看到差异。
在第一个模型中,您可以使用代码和属性代码定义属性,这里的问题是您可以定义类型未定义对象类型的属性的元素。然而,这个模型最容易使用,因为要定义一个 object_property,你只需要两个代码,比如:
INSERT INTO object_property(object_code, attribute_code, my_value)
VALUES (3,4,'myvalue')
在第二个模型中,您可以使用 object_code、object_type 和 attribute_code 使用更一致的数据来定义属性。但是,您需要使用三个代码和额外的查询,如下所示:
INSERT INTO object_property(object_code, object_type, attribute_code)
VALUES (3, (select object_type from object where code = 3), 4, 'my_value')
哪个更好?
【问题讨论】:
-
这两个答案都不是可接受的吗?说真的,关系数据库并非设计为面向对象的。这将表现得非常糟糕,并且是一个要编码的 PITA。
标签: mysql sql database-design foreign-keys