views:

67

answers:

2

I have a table DEFINITIONS where I store my xml definitions form my components (form,layout,grid...) Then I have detail table PROFILES (1:M relationship) where I store my user profiles if there are any. This user profiles are only allowed if a component is of type grid.

So far I've created just table DEFINITIONS and a table PROFILES in 1:M relationship to the DEFINITIONS table.

I'm just wondering if there is a more suitable design for this situation. I'm worried about the data integrity. There could be other component's (form,layout) PK in PROFILES FK field.

Is this a well-founded concern ?

+1  A: 

Rather than storing all the definitions in one table and only allowing PROFILES to link to the grid types, why not separate out the different definitions in to their own tables?

You would then have the tables Form_Definitons, Layout_Definitions, Grid_Definitions etc. and you can create a relationship to your Profiles table only from the Grid_Definitions table.

When you want to retrieve all the definitions you can create a view to union all the xxx_Definition tables together.

Tony
Wouldn't there be to much redundant data ? The check contraint solutions seems to me much better.
Why do you think there would there be redundant data? I'm suggesting you separate out the different types of Definition to allow you to constrain the Profiles to the Grid_Definitions table using a normal foreign key. Out of interest do all Profiles have Definitions or can a profile exist without at least one?
Tony
Just re-reading your question you say "This user profiles are only allowed if a component is of type grid" but then later say "There could be other component's (form,layout) PK in PROFILES FK field". Which statement is correct? Can a profile only exist with a relationship to a Grid definition or can it reference any type of definition?
Tony
Sorry, I'm not a native english speaker. This statement is true: "profiles are only allowed if a component is of type grid". I was trying to say with this statement "There could be other component's (form,layout) PK in PROFILES FK field" that if this would happen, that would be bad.
With redundant I meant that there would be 4-5 tables with the same structure and if a new commponent will be added a new table must be created.
@user137348: "I'm not a native english speaker" It's not a problem, your English is fine; I just wanted to be sure I was understanding your requirement correctly.
Tony
There would be multiple tables with the same structure but different names; it doesn't mean your data is duplicated. And yes, you would have to add a new table if you created a new component. If you are going to be adding components frequently then the constraint based solution provided by Alexander will be fine but you'll have to remember to change the constraint each time you want to allow a Profile to reference another Definition type.
Tony
Have a read of this SO question (http://stackoverflow.com/questions/870808/entity-attribute-value-database-vs-strict-relational-model-ecommerce-question) it might help you decide which way to go with your design.
Tony
Thank you for your answers, their were very helpful. I'm gonna mark Alexander's answer as the accepted. I hope you won't be mad at me :-) I voted up some of your comments. Hope this is enough.
Don't worry, I'm not mad at you. If Alexander's answer fits your purpose you are correct to accept it. I'm just trying to help out :)
Tony
+1  A: 

For integrity you can use composite FK + CHECK constraint here:

CREATE TABLE Profiles(
   definition_id  INTEGER,
   component_type INTEGER DEFAULT 1 CHECK( component_type = 1 )
);

ALTER TABLE Profiles ADD (
   CONSTRAINT Profiles_FK_Definition 
      FOREIGN KEY (definition_id, component_type) 
      REFERENCES Definitions(definition_id, component_type)
);

Ideally, if it doesn't kill performance, I would always do everything to preserve integrity.
OTOH realistically, if user can't access underlying DB another way but your UI, I wouldn't care much

Alexander Malakhov