This is a simple question, but I think I always used the wrong way.
I have three tables called Page -> Tag -> Attribute. The Tag table receives the Page_Id (foreign key) and the Attribute table receives the Tag_Id (foreign key).
The Attribute table must receive the Page_Id too?
I never did this, but I saw a database SQL generator that does this, and so, I realized that this turns all the work easier, but this is the right way too?
Edit with details of the tables: The Page table contains HTML pages and some attributes like the content, the title, doctype and all other single elements of a html page.
The Tag table contains the HTML tags of a html page with informations like the tag (a, p, br, h1, title, etc.) and the entire tag.
The Attribute table contains all the attributes of a tag and is like a Map with key and value where each entry is an attribute and a value.
Page contains multiple tags (1:N) and tag contains multiple attributes (1:N). Each table contains a unique identification.
This is one of those “it depends” situations – you don’t, logically need the value to be stored in
Attribute, since you can derive this information by joining toTag.But, in some circumstances, it may be useful for you to be able to “jump” from
AttributetoPagewithout having to join toTag. In such a situation, you can includePage_IdinAttribute, but you should add additional constraints to ensure that it isn’t inconsistent with the value stored inTag.You would enforce this consistency by having two keys declared in
Tag– the usual key you define (e.g.Tag_Id), and also a superkey onTag_IdandPage_Id. You would then declare a foreign key constraint inAttributethat includes both columns, and references this superkey. Whether you do that instead of the foreign key constraint on justTag_Id, or in addition to it, can also be a matter of preference/style.If it’s likely that
Tags will changePages, then you would normally declare the foreign key to the superkey as the one on which anUPDATE CASCADEoccurs – in this way, if thePage_Idchanges in theTagtable, that change will be automatically applied in theAttributetable.