Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
DATABASE design question
Old 09-26-2007, 09:35 AM DATABASE design question
Novice Talker

Posts: 3
Name: Eugene
Trades: 0
Hello all.

Suppose you have a set of tables, and one of them contains a field 'SEX',
lets say , the table Employee may have this field to identify the sex of an employee.
It is clear that this field will contain not much than 3 values - 'M" or 'male',
'F' or 'female', and NULL or 'unspecified'.

The main question is :

SHOULD WE ALWAYS CREATE A SEPARATE TABLE CONTAINING THESE VALUES AND
JOIN OUR TABLES ( that holds some info about sex ) WITH IT ?
[ create table sex (id int, sex char(1) null ); ]

It seems to me that according to database design and normalization rules, IT IS OBLIGING TO CREATE SUCH A TABLE. IS IT?
Eugene_ is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-26-2007, 01:37 PM Re: DATABASE design question
NullPointer's Avatar
Will Code for Food

Posts: 2,784
Name: Matt
Location: Irvine, CA
Trades: 0
For something like gender I wouldn't bother creating a seperate table for it. Usually when I create seperate tables for something that belongs to another table (Gender belongs to Employee, ie employees have a gender) is when that item has attributes of its own that need to be organized. Employee's for example have resumes. It would kill your organization to put all of the fields corresponding to a resume into the employee table. So you create a seperate table for resumes and then under the resume field in employee you place information linking the employee to a particular resume. In this case you wouldn't even have to do that considering you can lookup to corresponding resume by the name of the employee. But in the case names were not unique you would have to develop a way to uniquly identify the row you need to find in the other table.

That's just my style by the way. Hope I helped.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 09-26-2007 at 01:44 PM..
NullPointer is offline
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 09-26-2007, 01:39 PM Re: DATABASE design question
Learning Newbie's Avatar
Defies a Status

Latest Blog Post:
Astounding Republican Paranoia
Posts: 5,662
Name: John Alexander
Trades: 0
Depends how you use it.

You might want to use a boolean value instead of a char(1) if you're worried about performance.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Old 09-27-2007, 04:41 AM Re: DATABASE design question
Novice Talker

Posts: 3
Name: Eugene
Trades: 0
Thank you for answering.
But the truth is that if we dont create a separate table in this case, our database wouldn't follow the normalization rules.
In this particular case, if we use a boolean field, then we wont be able to break it ('cause we have only 3 possible values). But not all db systems have a boolean type. So, if we would use a tinyint or char or other 1-byte length type, there may be a situation when,
using just a field 'sex' (not a foreign key), we put into this field another value, different from ( '0', '1', null ) for the integer type or
('m', 'f', 'u' / null ) for char types.
You may say that "your application have to control this and not allow to do such things", but suppose you dont have any application (just sql tools) or the application have bugs and allow to insert any values into this field.
Eugene_ is offline
Reply With Quote
View Public Profile
 
Old 09-27-2007, 04:55 AM Re: DATABASE design question
Banned

Posts: 2
Trades: 0
I would simply use an ENUM myself.
desu is offline
Reply With Quote
View Public Profile
 
Old 09-27-2007, 05:05 AM Re: DATABASE design question
Novice Talker

Posts: 3
Name: Eugene
Trades: 0
if your db system supports it
LOL
Eugene_ is offline
Reply With Quote
View Public Profile
 
Old 09-28-2007, 07:48 AM Re: DATABASE design question
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Just use a 1 char wide character column (m,f,n) and use CASE statements in your query

Code:
select fieldlist, case sex
	when 'n' then 'Not Specified'
	when 'm' then 'Male'
	when 'f' then 'Female'
	end as sex
from table
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 09-28-2007, 07:45 PM Re: DATABASE design question
Learning Newbie's Avatar
Defies a Status

Latest Blog Post:
Astounding Republican Paranoia
Posts: 5,662
Name: John Alexander
Trades: 0
Quote:
Originally Posted by Eugene_ View Post
But the truth is that if we dont create a separate table in this case, our database wouldn't follow the normalization rules.
Normalization suggestions. It's a best practice to normalize as much as possible, then denormalize for performance in many cases.

Whether you want a new field or a new table in the long run depends what you want to do. How much will joining another table cost, and how much will reading the extra column on every row will cost.

Quote:
Originally Posted by Eugene_ View Post
You may say that "your application have to control this and not allow to do such things", but suppose you dont have any application (just sql tools) or the application have bugs and allow to insert any values into this field.
With just the SQL tools I would constrain the column to force a set of valid values.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
Learning Newbie is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to DATABASE design question
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.27763 seconds with 12 queries