OK, I am using SQL Server 2008 R2 and have a couple of relations I need to shuffle around a bit. Here is my example database design (not actual, but shows the problem).
Table - Products
Columns: ProductID - primary key
ProductInfo - irrelevant to problem
ModelID - foreign key to Models table
MfgrID - foreign key to Mfgrs table
Table - Models
Columns: ModelID - primary key
ModelInfo - irrelevant to problem
Table - Mfgrs
Columns: MfgrID - primary Key
MfgrInfo - irrelevant to problem
In this scenario, a Product is one of a Model and is made by a Mfgr. What I need to do is associate the Mfgrs table with the Models table, as a given Model is made by a Mfgr and having separate links to the final Product is, well, bad design?
This models an actual database with live data, so it’s not just an exercise. What I want to do is add a column to Models, call it MfgrID and create a relationship from Models to Mfgrs. Then Mfgr will have a one-to-many relationship to Models and Models will have a one-to-many relationship to Products, and the world will be a better place!
Well, my world will be a better place anyway.
I’d like to create the new relationship based on the old, so that each Model gets a MfgrID
that points to the Mfgr for any given Product of that Model. Once that is established I will go back and delete the Products to Mfgrs relationship.
Any questions? I appreciate any and all ideas, I keep getting myself confused trying to design a succinct query to do this…
Dave
If you only have one product per model, this should do it;
and possibly at the end if you want MfgrID to be mandatory and you have no nulls in that column;
And just to make sure, you should never run anyone’s random scripts from the Internet on your production database without testing first 😉