views:

95

answers:

3

I am looking for a good way to implement - default characteristics for an object and override the defaults in a database.

I have a table called Products - that maintains the list of products One of the products is mailbox and the value for the attribute color is blue I need the ability to specify that mailbox is red when the country is UK Rather than create one row for every country I want the ability to say if there is no entry for the country use the defaults.

I have multiple products in the products table

TIA

A: 

If you are using a DMBS that has triggers this might be a good time to use them.

I would assign a default value for the color column but have a trigger that checks the country on insert and can override that default value as needed.

Abe Miessler
+1  A: 

How about this solution:

Products(#product_id,...,color)
ProductLocalization(#country_id,color,...)

You can also create a view to simplify the data access:

CREATE VIEW LocalProducts
AS
SELECT p.product_id,...,country_id=[default],p.color
FROM Products p
UNION ALL
SELECT p.product_id,...,pl.country_id,pl.color
FROM Products p, ProductLocalization pl

The [default] placeholder should be a default value depends on the datatype of country_id. It can be a 'default' for varchar type or a 0 for int type (and the valid id of actual countries should be in range 1 to N).

When you need to localize products for a country, you will just add a record of product features into table ProductLocalization with a specific country_id.

Feil
I want to make sure that I understand what you are suggesting. In this solution, default value in the first query represents value for the rest of the world while the second query provides country specific values. Shouldn't the second query have a join condition - WHERE p.product_id = pl.product_id?
shikarishambu
It depends on your requirement. If the country specific values are applied to each product, the query above is right. If some country specific values are just applied to one specific product, a column product_id should be added into table ProductLocalization and the primary key of this table should be (product_id,country_id), in the same time, the second query should have a join condition as you wrote.
Feil
A: 

The question is very open-ended. Which DBMS are you using? Do you have any say in the design of the tables in play outside the default value? If so, I would suggest thinking up a new design since your associations do not allow the constraints you need to enable. No matter what you do, it sounds like you are looking at placing logic in default values, which is a bit unorthodox.

My suggestion is think about placing this logic in the application or, worst case, in a trigger like Abe suggested.

Benny
I want the solution to be database independent. Hence, trigger won't work for me
shikarishambu