简体   繁体   English

关于数据库表的设计

[英]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. 表A,B和C具有“类型”字段,而表D没有,并且由于“类型”字段,某些字段的值不同。

For example, in Table A, if "type = 0", the "length" field can be 0.00, otherwise the "length" field can not be 0.00. 例如,在表A中,如果“类型= 0”,则“长度”字段可以为0.00,否则“长度”字段不能为0.00。 However, in table D, the length field must exist and have a "sheet" field more than tables A, B, and C. 但是,在表D中,长度字段必须存在,并且比表A,B和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. 在这种情况下,我将表A,B,C,D设计成不同的表,即A,B,C,D由表名区分。 Is it too bloated? 太too肿了吗?

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. 并且我需要将另一个表(表E)与表A,B,C和D的数据相关联。根据我以前的做法,我需要在关联的表中添加一个字段,用于区分不同的表,以便它可以找到正确的表内容。 (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. 如果我将表A,B,C,D设计成同一张表,然后通过一个字段来区分A,B,C,D这四个不同的内容,这将导致某些字段变得多余。

For example, table D must have a "sheet" field, and tables A, B, and C are not required. 例如,表D必须具有“工作表”字段,而表A,B和C则不是必需的。 Design to the same table, each table A, B, C field will be more a "sheet" field。 设计到同一张表,每个表的A,B,C字段将更多为一个“工作表”字段。

The benefits are also obvious, Table E can be directly associated with the same table "id" field. 好处也很明显,表E可以直接与同一表的“ id”字段关联。

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. 我的四个表(ABCD),只有表D有一个“工作表”字段,少了一个“类型”字段,其他字段与表A / B / C的字段相同,设计成一个相同的表是没有问题的。

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). 但是,我有一个操作可以对表中的数据进行分页,可以在其中查询ABC类型的数据,但是D类型的数据需要一个“ group by”查询(例如,D类型的数据“ The sum”为“工作表”字段计算“”,其数据与“宽度”字段和“长度”字段相同。

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}

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

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