If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > General > Database Concepts & Design > Storing Redundant Data

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-30-11, 06:10
jx12345 jx12345 is offline
Registered User
 
Join Date: Apr 2010
Posts: 51
Storing Redundant Data

I'm working on a system that stores orders - there are fields for recording the time an order was created, completed, processed, and shipped.

Therefore it is possible to determine the status of an order by querying these fields

However, i thought it would make queries much simpler if i had a status field enum(created, completed, processed, shipped) that is updated at the same time as the time fields

Instinctively it felt a bit wrong to store data that could be calculated from other fields but i think it would make like much easier...

any thoughts would be gratefully recieved..

ta
j
Reply With Quote
  #2 (permalink)  
Old 07-30-11, 06:20
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
yes, go ahead and add the status column -- but don't use ENUM, which is the spawn of the devil, use TINYINT or CHAR(1), and link it via a foreign key to a status table
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 07-30-11, 06:22
jx12345 jx12345 is offline
Registered User
 
Join Date: Apr 2010
Posts: 51
many thanks, rudy, is enum really so bad? using a foreign key to an external table means harder work glueing everything back together. with the enums i can see whats what from the raw table without having to remeber 0 = this, 1 = that, etc.

Last edited by jx12345; 07-30-11 at 06:46.
Reply With Quote
  #4 (permalink)  
Old 07-30-11, 07:15
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by jx12345 View Post
many thanks, rudy, is enum really so bad? using a foreign key to an external table means harder work glueing everything back together. with the enums i can see whats what from the raw table without having to remeber 0 = this, 1 = that, etc.
yes, ENUM is evil

so okay, instead of using a TINYINT or CHAR(1) as the foreign key, use VARCHAR(12) as the foreign key, and store the status name
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 07-30-11, 07:19
jx12345 jx12345 is offline
Registered User
 
Join Date: Apr 2010
Posts: 51
ok, so even if i dont store anything in the related status table other than the primary key ie. the status_name, it is still a valid struture because the status table is used to validate status entries in the orders table???
Reply With Quote
  #6 (permalink)  
Old 07-30-11, 07:27
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
yes, perfectly valid
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 07-30-11, 07:30
jx12345 jx12345 is offline
Registered User
 
Join Date: Apr 2010
Posts: 51
many thanks!
Reply With Quote
  #8 (permalink)  
Old 07-30-11, 07:40
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
Quote:
Originally Posted by r937 View Post
yes, go ahead and add the status column -- but don't use ENUM, which is the spawn of the devil, use TINYINT or CHAR(1), and link it via a foreign key to a status table
I'd prefer a CHECK constraint as long as the status codes need not be translated through SQL (e.g. through a JOIN)
Reply With Quote
  #9 (permalink)  
Old 07-30-11, 10:59
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
one of the problems with a CHECK constraint, as well as with ENUM, is that it requires the DBA to make a change to the production schema when you want to add a new status value

if you've ever worked in a large organization, where the bureaucracy requires you to fill out a project plan and get three levels of management signature just to ask the DBA to pick his nose, you'll agree that this is a non-starter

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #10 (permalink)  
Old 07-30-11, 22:54
dbGuyo dbGuyo is offline
Registered User
 
Join Date: Jul 2011
Posts: 9
A general comment!

The question looks is already answered.
I just want to add a very general rule for those who thinks very academic.
Sometimes you need to add a redundant field to your tables because you want to code easier and to maintain your code easier.
Remember that your code writing is 10-15% of the life cycle but maintenance is 85-90% (and even more). So, the easier the code structure is, the easier would be the maintenance!
Good luck!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On