如何设计DB表和类? [英] how to design the DB table and Class?

查看:196
本文介绍了如何设计DB表和类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我系统中的表结构。方案是,系统具有客户端。客户可以是
多种类型,例如:一个人,一个公司,一个银行等。我来了一个数据库设计如下。

Following is a table structure in my system. The scenario is, system has Clients. Clients can be many type, for exampel: a Person, a Company, a Bank or etc. I have come with a database design as follows.

strong> TABLE:CLIENT

TABLE : CLIENT

===============================
| ID    | NAME_CODE | TYPE    |
-------------------------------
| 1000  | Martin    | PERSON  |
| 1002  | George    | PERSON  |
| 1003  | Max_Group | COMPANY |
-------------------------------

TABLE:PERSON

==================================================
|  ID | CLIENT_ID  | FIRST_NAME   | LAST_NAME    |
--------------------------------------------------
| 1   | 1001       | Martin       | XXXXXXXXXXX  |
| 2   | 1002       | George       | XXXXXXXXXXX  |
--------------------------------------------------

TABLE:COMPANY

===================================================
| ID | CLIENT_ID  | NAME           | Location     |
---------------------------------------------------
| 1  | 1003       | Max Group Inc. | XXXXXXXXXXX  |
---------------------------------------------------

EDIT:为PERSON和COMPANY表格添加的ID。

EDIT : ID added for PERSON and COMPANY tables.

我欢迎如果有人可以建议我一个更好的结构。

I welcome if any one could suggest me a better structure.

对于上述结构,我需要创建类模型。以下是我创建的。

For the above structure, I need to creat the class model. Following is what I have created.

class Client
{
   int ID;
   string NameCode;
}

class Person : Client
{
    int ID;
    int ClientId;
    string FirstName;
    stirng LastName;
    //........
}

class Company : Client
{
    int ID;
    int ClientId;
    string Name;
    string Location;
    //.........
}

I感觉上面的模式都不合适。这可以有一个正确的方法。
请建议我一个更好的方法。

I feel the above model are not suitable. There can be a right way for this. Please suggest me a better way.

推荐答案

经过一番讨论,我认为你的表格设计得很好。

After some discussion, I think your tables are well designed.

我会这样设计您的表格:

I would design your tables this way:

TABLE:CLIENT p>

TABLE : CLIENT

==============================
| ID   | NAME_CODE | TYPE    |
------------------------------
| 1    | Martin    | 1       |
| 2    | George    | 1       |
| 3    | Max_Group | 2       |
------------------------------

其中类型是一个枚举,而不是硬编码,所以查询更快。

where Type is an Enum rather than anything hard-coded so that queries are faster.

TABLE:PERSON

============================================
| CLIENT_ID  | FIRST_NAME   | LAST_NAME    |
--------------------------------------------
| 1          | Martin       | XXXXXXXXXXX  |
| 2          | George       | XXXXXXXXXXX  |
--------------------------------------------

TABLE:COMPANY

==============================================
| CLIENT_ID  | NAME           | Location     |
----------------------------------------------
| 3          | Max Group Inc. | XXXXXXXXXXX  |
----------------------------------------------

类将如下所示:

class Client
{
   int ID;
   string NameCode;
    //........
}

class Person : Client
{
    string FirstName;
    stirng LastName;
    //........
}

class Company : Client
{
    string Name;
    string Location;
    //.........
}

客户端表现在具有每个客户端实体的父ID。它可以通过为个人和银行的个人id,然后在父客户端表中引用一个外键,我相信从数据库的角度更容易操作(如果你要去 composition )。但在应用程序中,我喜欢继承和多态,所以直接适应它的db设计是我发布的。

The Client table has the parent id now for every client entity. It could have been reversed by having individual id for persons and banks and then reference a foreign key in the parent client table which I believe is easier to operate from db point of view (that scales well if you're going for composition). But in the application I love inheritance and polymorphism, so the straight adaptation of it for db design is what I posted.

这篇关于如何设计DB表和类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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