简体   繁体   中英

About the design of the database table

Described as follows:

In my project, the design table encountered confusion.

Now I have four different content types,Each field is not exactly the same.

Such as ,

Table A, B, and C have a "type" field, and table D is not, and because of the "type" field, there are some fields that are different values.

For example, in Table A, if "type = 0", the "length" field can be 0.00, otherwise the "length" field can not be 0.00. However, in table D, the length field must exist and have a "sheet" field more than tables A, B, and C.

In this case, I will designed Table A, B, C, D into different tables, that is, A, B, C, D is distinguished by the table name. Is it too bloated?

And I need to associate another table (Table E) with data of Table A, B, C, and D. According to my previous practice, I need to add a field in the associated table, used to distinguish between different tables, so that it can find the correct table content. (Very inconvenient)

If I design tables A, B, C, D into the same table, and then through a field to distinguish A, B, C, D these four different content,This will cause some fields to be redundant.

For example, table D must have a "sheet" field, and tables A, B, and C are not required. Design to the same table, each table A, B, C field will be more a "sheet" field。

The benefits are also obvious, Table E can be directly associated with the same table "id" field.

Designed into four tables, distinguished by table name

Or designed as one table, more of the distinction between the operation, completed in the code ??


update..

My four tables (ABCD), only the table D has a "sheet" field, less with a "type" field, other fields are the same with Table A/B/C 's fields , designed into one same table is no problem.

however,I have an operation to paginate the data in the table, where the ABC type of data can be queried, but the D type of data needs a "group by " query (for example, the D type of data, The "sum" is calculated for the "sheet" field with the same data as the "width" field and the "length" field).

so if I designed into one same table,how can I achieve the above requirements。

thanks for your answers!

Depend on your needs, but you can use a main table for the common fields, and child tables for the additional information.

Main table:

TableABCDE: { ABCDE_id (Pk), field1, fieldN}

Childs Table

DataA:  { A_ID (PK), ABCDE_ID (FK), field_A1, fieldAN}
DataB:  { B_ID (PK), ABCDE_ID (FK), field_B1, fieldBN}
DataC:  { C_ID (PK), ABCDE_ID (FK), field_C1, fieldCN}
DataD:  { D_ID (PK), ABCDE_ID (FK), field_D1, fieldDN}
DataE:  { E_ID (PK), ABCDE_ID (FK), field_E1, fieldEN}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM