views:

138

answers:

3

Hey guys 8D

I'm in a bit of trouble trying to define the type of a relationship between 3 tables. They are PRODUCTS, SUPPLIERS, and the third, QUANTITY, where I have a combination of product and supplier and a field storing the quantity of products of that combination.

I can have one product from one or more suppliers, that's why the table QUANTITY exists. Eg.: I can buy the same notebook A from the supplier X and supplier Y.

The table QUANTITY have the fields prod_id, supp_id and qty. Both PRODUCTS and SUPPLIERS have an ID field. I can't figure it out, so I'm asking for a little help from smarter guys than me, meaning you all. :D

Thanks a lot.

[EDIT]

I'm very sorry, I should've been more clear from the start. This is about the modelling. :D

A: 

You appear to be puzzled by a relationship between entities having additional attributes. I agree that it is fairly uncommon, but there's nothing invalid about it and you appear to be modeling it correctly.

reinierpost
Thank you :D It's a relief to know.
Bored Elf
+1  A: 

The situation described in your posting seems to answer whatever question you are asking. That is, you state a correct solution to your problem in the question.

Table PRODUCTS will have field id (I prefer to call it prop_id, but that's just me). SUPPLIERS will have id (or, in my world, supp_id). Table QUANTITY will have (prop_id, supp_id, and quantity).

Ideally, prop_id in PRODUCTS will be protected as primary key or a unique index. The same with supp_id in SUPPLIERS. In QUANTITY, you will create a primary key or unique index on the combination of prop_id and supp_id (since each combination should occur only once, even though each column considered alone might have duplicates).

Finally, I generally prefer to have a single-column integer primary key on every table, so (if it were me, others disagree) I would make (prop_id, supp_id) a unique index and create a new primary key column quantity_id in the QUANTITY table.

Larry Lustig
Why? Why do you like to have a single column PK on EVERY TABLE? What purpose does it serve? To me this is a purely emotional/foolish consistency concept. If a table is a Parent, it needs a PK, that PK is easiest to manage as a single column. If it's not a parent, like the mapping table, why build a useless column, with a useless number and maintain a useless index?
Stephanie Page
I like to have a single column PK on every table because when writing the user interface it is generally much easier to use a single value to uniquely identify each row. In desktop programs, you can generally attach an integer value to each item in a ListView control so that, when the user selects one of several semi-duplicate items and clicks DELETE, you easily know which row must go. Similarly, when writing web interfaces, this saves the trouble of encoding and decoding into the identifier the multiple columns that uniquely identify the row.
Larry Lustig
@Stephanie - Most database purists will agree with you. Use natural keys where possible. However, many database tools require (or at least work better with) an identity column.
Mystere Man
I did this :D created a unique index on QUANTITY table. Good to know we think alike. I didn't create a PK for this table because a where clause of product and supplier sounded a solution simple enough for me. Thank you for your anwser.
Bored Elf
@Mystere Man - That's true. Navicat, for instance.
Bored Elf
Also, SQL Server itself sometimes requires a unique identity of every column for some features, such as replication.
Mystere Man
That's so sad. The RDBMS can't manage replication unless the user adds a column.
Stephanie Page
I'm not saying use natural keys when possible. I'm saying don't create a primary key if it's not going to participate in a relationship. ID 1,2,3,4,5 on the m-n mapping table has no meaning and no utility TO THE APP... maybe MSSS can't deal with that and I'm forced to add things to my data model for the sake of the platform, then that's a limitation of the platform. I would leave it off my model and/or hide it, and just generate it at forward engineering.
Stephanie Page
+1  A: 

Since you ask about relationships, here's a little more information which may help. A many-to-many table exists at the level of design (concept), but is built in the db by building two one-to-many relationships.

Although you don't have to, you probably should declare that quantity.supp_id is a foreign key from suppliers, establishing a one-to-many relationship from suppliers to quantity; and declare that products.prod_id is a foreign key from products, establishing a one-to-many relationship from products to quantity. Doing so may help the db to optimize queries. It will also enable you to set up cascading deletes so that deleting a supplier from the suppliers table will automatically also delete all of that supplier's records in the quantity table (if you want that to happen).

Sid_M
:D Your answer made me realize my mistake of not being clear about if the question was about modelling and the first paragraph enlightened me. Thank you.
Bored Elf