all groups > sql server programming > february 2006 >
You're in the

sql server programming

group:

1, 2, or 3 Tables (Data Modeling Question)


1, 2, or 3 Tables (Data Modeling Question) Jeff S
2/16/2006 9:50:45 PM
sql server programming: I'm modeling some entity that could reasonably be considered to be [one
entity] or [two entities] --- people which are customers and employees.

AFAIK, these are my three basic options:

OPTION 1:
Have one table, People, with some "flag column" that tells us which kind of
person we're dealing with. This one table would obviously have columns that
are relevant to only one person type (e.g., hire date is applicable to only
the employees). Such columns would always be NULL for rows of "the other"
type (e.g., customers).

OPTION 2:
Have two tables - one for Customers and one for Employees.

OPTION 3.
Have three tables - one for People that has common attributes, and then one
each for Customers and Employees

My Question: What are some general guidelines I should consider for deciding
which of these options to implement? Alternatively, which would YOU tend to
go with and why? Beyond general guidance I'm particularly interested in
option 3; what are some of the pros and cons of having those 3 tables?

Just looking for general guidance on this important decision I have to make.

Thank you for your time and consideration.

-Jeff

Re: 1, 2, or 3 Tables (Data Modeling Question) SriSamp
2/17/2006 12:00:00 AM
I would go for option 3, which is to have 1 Persons table and then a table
each for Customers and Employees. Since all common attributes are grouped
together, there is no duplication of information. Also, since customers and
employees are separated, any domain logic that is specific for each type can
be separately applied. From a application tier design perspective, you can
create a Persons class and have Customers and Employees inherited from it.
So it falls naturally into an object design too. When time comes for
querying, by using appropriate views with joins, you can just query the
information that you need and merge them when needed also.
--
HTH,
SriSamp
Email: srisamp@gmail.com
Blog: http://blogs.sqlxml.org/srinivassampath
URL: http://www32.brinkster.com/srisamp

[quoted text, click to view]

Re: 1, 2, or 3 Tables (Data Modeling Question) Brian Selzer
2/17/2006 12:00:00 AM
The question you need to ask is: can an Employee also be a Customer? If the
answer is anything but a resounding "NO!," then the common columns in
Customers and Employees should be extracted into a People table to avoid
redundancy.

[quoted text, click to view]

Re: 1, 2, or 3 Tables (Data Modeling Question) Jeff S
2/17/2006 7:58:32 AM
Okay, that makes sense... now, how do I go about "knowing" if a given person
is a Customer or Employee or both? It seems I'd have to query both tables
and get some count, perhaps using WHERE EXISTS. Is there a more elegant way?

Thanks




[quoted text, click to view]

Re: 1, 2, or 3 Tables (Data Modeling Question) Brian Selzer
2/17/2006 3:05:46 PM
Join. Join People to Customers to find people who are customers. Join
People to Employees to find people who are employees. Join Customers to
Employees to find customers who are also employees and then join the result
to People to obtain the common info.

[quoted text, click to view]

Re: 1, 2, or 3 Tables (Data Modeling Question) Robert Bravery
2/17/2006 4:09:40 PM
HI,
I would go with SriSamp on this. Especially as an added complication where
an employee can also be a customer, You wouldnt want to enter common things
like name etc twice. By having two tables, employee and customer, with
forein keys, you can look at him as an employees, but then seperately as a
customer.

Robert

[quoted text, click to view]

AddThis Social Bookmark Button