2-19- how to express inheritance in the object-oriented database

1. EAV表(entity-Attribute-Value)

2. Single Table Inheritance

3. The entity table inheritance

4. Class Table Inheritance

5. Semi-structured design

 

 

1. EAV表(entity-Attribute-Value)

   createtable note_dictionary

   (

     dictionary_idnumber,

     dictionary_type varchar2 (50), - recording of this type, is that more than 20 records the present

     dictionary_name varchar2 (200) - this is the name of the recording

    );

   - store records this content

 

   createtable note_content

   (

    content_idnumber,

    note_id number, - a specific record id of the present

    dictionary_idnumber, - recording of this field id

    Contents of the field - contentvarchar2 (1000)

   );

 

Advantage

1. Flexible, deletions field is not required to modify the structure of database tables. Regardless of the existence of entity inheritance, you can use stored EAV.

2. Design a simple database table (id, attr_name, value)

 

Disadvantages:

1. Type of property values ​​can not be constrained. If you are Date and number, it can only be expressed in varchar2. Statistics may also be a problem

2. Do not add constraints and default values ​​of not null.

3. post-maintenance data more difficult.

4. If there is a relationship between this record, to establish relationships, need to add the table to achieve very complex.

5. The table of contents after some time will become very heavy, in the case of which the system data amount.

6. indeed affect the whole body. A modification can not determine whether the affected elsewhere. Java code using reflection, debug hard to debug, hard to locate the problem

7. complex queries will become complicated.

8. sacrificing the benefits of a relational database itself

9. The integrity check can not use characteristics (foreign key)

 

2. Single Table Inheritance

The entity inherits all the attributes of the system are stored in a table,

Entity attributes are as follows:

Database Design as follows:

user table:

Advantage

    Database Design Simple

    The list does not need to query to get complete information on subclasses

    Avoid a lot of design flaws EAV

 

Disadvantaged

    When adding a user to modify the properties in Table subclass (lock table, the influence is large amount of data)

    For subclasses, unrelated attributes appear in the table, such as teacher's line appeared student_number, students of the subject line appears

 

scenes to be used:

Fewer neutrons class property inheritance system situation. For example, within a foreseeable time subclass attributes are relatively little time can be used in this way, after all, a simple query, the query does not need contingency tables.

 

3. The entity table inheritance

The property is complete entity, the entity table inheritance, that each entity corresponds to a full table.

This design advantages and disadvantages:

Advantage

1. For a complete object does not need contingency table query

2. The table does not unrelated attributes (compare with the single table inheritance)

Disadvantaged

1. The need to modify the base class a plurality of table when adding attributes (attributes such as birthday Add User class, you need to teacher / student tables are added column)

2. The open structure of the table, no look class inheritance

 

4. Class Table Inheritance

Then the database table design also has three tables: user table, teacher tables, student tables (teacher and student table table has a foreign key), as follows:

Advantage

A clear hierarchy of database tables, database tables directly reflects the inheritance

When no modification is a subclass of the base class table (user table) when adding attribute, the base class does not need to add attributes for multiple column tables Add

Does not need to query a plurality of tables (Comparative entity table inheritance scheme) When a query message base class teacher, student's

 

Disadvantaged

Gets the object that needs full data tables associated queries (when large volumes of data tables with table query performance difference)

 

 

5. Semi-structured design

other_properties field, the object is stored for teacher {subject = english}, subject to the student deposit: {student_number = 123}

So when you query the entire record full inquiry into memory and then converted to an object.

Advantage

Scalability, when adding a column need to add child property, the addition will be added to the base class attributes column

Simple queries, each row corresponds to the complete information of an object, without linking table query

 

Disadvantaged

Unstructured part of the problem is still there EAV design

Object attribute name no constraints (mysql not user defined name called username, a small business may not feel as the userName)

You can not constrain the type of the attribute value (age is not defined int type, the traffic may not feel a small 'abc' in)

Gets a single object data cumbersome (a complete query object, you need to obtain multiple records can not be directly mapped to the User object)

Integrity checking feature can not be used (not a foreign key)

DB unusable unstructured part of the aggregate functions (such as sum, count, etc.)

Published 137 original articles · won praise 2 · views 20000 +

Guess you like

Origin blog.csdn.net/m0_37302219/article/details/104872832