views:

42

answers:

2

I'm working with an online shop, currently products are either available or not, and enabled or not, by an int field on the products table.

They want me to add more to this, e.g. "in stock with supplier", "out of stock, ships within x days" etc. I'm thinking of just ditching the flags and creating a status field of enum type, with 'available', 'out of stock', 'coming soon', discontinued' etc.

What's the best way to do this? Add the enum type as a new column on products?

I got told I should add a separate table called Status and have a foreign key reference to it in the products table so it'll be easier to change in the future. I'm not sure how this is the case, as the enum type seems easy enough to change. You can add to it, remove from it and rename items.

A: 

a table with an FK will take a bit less space on the hard disk but I prefer using an Enum type. The difference is generally small and there is a performance benefit of not over normalizing.

Sruly
+2  A: 

If you anticipate ever changing the enum, you should probably use a separate table. It's less expensive to INSERT or UPDATE than it is to ALTER.

Also, consider how you would determine acceptable values for that field. If it's an enum, you have to hard code the options or inspect the database schema. If it's in a separate table, you can SELECT to populate a list of options.

banzaimonkey
How will it be less effective? It'll result in a join statement for each product, versus the difference between an insert and alter over the next few years..? I like the point on validation, might go with this way as an easier way to validate and populate forms. It'll mean (hopefully) that adding a new status in the future is just a new row.
Richard
@Richard The added expense is in the application, not the database. If you ALTER the enum, you'd have to search through your code to change any code that references the enum to reflect the changes to the schema. Code that uses JOINs will work as-is.
banzaimonkey