Ilya Gazman
Ilya Gazman

Reputation: 32271

How to create table of tables c# my sql

I got site for selling products. In my database I have 3 tables: Users, Countries, Products.

Each user can sale his products in many countries. When clients come to visit, my site, they can choose to search for product by country and by product price( same product sold by same user can have different prices in each country).

I thought about two implementations for this:

  1. Create a linked table with user_id, country_id, product_id. In this case each time I would like to add new product I will need to update two tables, the linked table and the products table.
  2. Or create new table for each country, that will have products in it. So when I will have to add new product I will only need to update one table.

I like my second solution more, because I think it will perform faster, for both reading and inserting, how ever it's management is hard, I will have lots of tables, and what if instead of counties I would like to use cities, I will get thousandths of tables.

Upvotes: 0

Views: 5818

Answers (1)

Aasmund Eldhuset
Aasmund Eldhuset

Reputation: 38010

Do NOT go for the second solution. Relational databases are meant to have a fixed number of tables, and you will run into a lot of problems if you try to have a variable amount of tables in the manner you describe.

If I understood your requirements correctly, you should probably use two linked tables: one that contains user_id and country_id (thus telling where each user may sell products), and one that contains country_id, product_id, and price (thus telling the price of each product in each country). (This assumes that a product costs the same within a country no matter who sells it.)

Upvotes: 3

Related Questions