问题
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.
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 : 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 feel the above model are not suitable. There can be a right way for this. Please suggest me a better way.
回答1:
After some discussion, I think your tables are well designed.
I would design your tables this way:
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 |
----------------------------------------------
And the classes would look like:
class Client
{
int ID;
string NameCode;
//........
}
class Person : Client
{
string FirstName;
stirng LastName;
//........
}
class Company : Client
{
string Name;
string Location;
//.........
}
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.
来源:https://stackoverflow.com/questions/13340236/how-to-design-the-db-table-and-class