开发者

problem in many to many relationship

I have two tables jewelry(j_id,j_name,Description) and style(style_id,style_name,image) each table has a many to many relationship with the other table, as one jewelry product can have multiple styles and there are more product of same style, so the relationship is many to many.

Now my question is how can I relate these tables i.e I want to insert a single record in jewelry table and there should be multiple styles for one jewelry product.

On jewelry html page I wan开发者_运维技巧t to put the style in multiple select dropdown list which populated dynamically from style table and if the user want to select two style for the same product, they can.


I would recommend 4 tables, 3 for the jewellery and style and the fourth one for orders.

Jewellery : jid (PKEY),jname,description

Style: sid (PKEY), sname, description

Product : pid (PKEY), sid(FKEY), jid(FKEY) - One Product defines a unique combination of style and kewellery

Order : oid(PKEY), pid(FKEY), other details(name, address etc.)

For each jewellery, recover associated styles using the product table and populate the fields on the html page. For each order, store the product id, which uniquely identifies both the jewellery and style.


Not sure that this is a database-design question. To me it looks more like a programming question and in that case it would be helpful to know what programming language and database you are using.

Anyway here is some pseudo code that does what I think it is you are asking about.

Add one row in table jewelry
Store the new jewelry.j_id to a variable @jid

for each @StyleID in SelectedStyleList
begin
  Add one row to table jewelry_style 
   set jewelry_style.j_id=@jid, jewelry_style.style_id = @StyleID
end


your question is not about designing your database for this many-to-many relationship but about the inserting strategy when creating new jewelry / style objects and relations between them?

In that case your strategy has to be a transaction inserting your main data and after that relating each other. In case of abortion you can just rollback the transaction and everything you created is removed. As long as you're in a transaction and your isolation level is correct nobody will see the temporarily created data

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜