SQL field with multiple id's of other table

前端 未结 5 506
暗喜
暗喜 2021-01-04 21:27

Could someone give me an idea how to create this database structure. Here is an example:

Table \"countries\":
id, countryname
1, \"US\"
2, \"DE\"
3, \"FR\"
4         


        
5条回答
  •  南笙
    南笙 (楼主)
    2021-01-04 22:04

    the best approach for relational databases is the following :

    One table for coutries, let's say country_id, country_desc (country_id is primary)

    one table for products, let's say product_id, product_desc and as many columns as you want (product_id is primary)

    if you had only one country for sure, it'd be enough to have a foreign key pointing to country_id in each product row. Having a foreign key asserts that there is an actual country behing a country_id referring to country table.

    In your case you have several countries for a product, so add a separate association table product_id, country_id

    both keys primary and both foreign as well.

提交回复
热议问题