Best way to implement many-to-many relationship in MySQL

dats

I have 2 tables, users(~1000 users) and country(~50 countries). A user can support many countries so I am planning to create a mapping table, user_country. However, since I have 1000 users and 50 countries, I will have a maximum of 50000 entries for this table. Is this the best way to implement this or is there a more appropriate method for this?

If this is the best way, how can i add a user supporting many countries to this table using only one SQL statement? For ex:

INSERT INTO user_country(userid, countrycode)
VALUES ('user001','US'),
('user001','PH'),
('user001','KR'),
('user001','JP')

Above SQL statement will be too long if a user supports all 50 countries. And I have to do it for 1000 users. Anyone have any ideas the most efficient way to implement this?

Jan

Personally I'd do the insert based on a select:

INSERT INTO user_country SELECT 'user001', countryid from countries WHERE countryid IN ('US', 'PH', 'KR', 'JP');

You need to adapt to your column names.

The alternative of storing list of countries in a single column usercountries varchar (255) as US,FR,KR and so on would be possible as well - but you'd lose the ability to select users based on the country they support. In fact you don't lose it - but

SELECT * FROM users WHERE usercountries like '%KR%';

Is not a good query in terms of index usage. But as you only have 1000 users a tablescan will be mighty quick as well.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What the best way to model a many to many relationship

From Dev

Many to many relationship and MySQL

From Dev

Mysql Many to Many relationship

From Dev

Best way to query a Many to Many Relationship using pg-promise

From Dev

Best way to generate many to many relationship in Excel 2007?

From Dev

denormalize many to many relationship in MySQL

From Dev

MySQL, Best way to select entries with many one-to-many relationships?

From Dev

CoreData many to many relationship Best practices

From Dev

How to implement one to many relationship

From Dev

What's the best way of implementing a many-to-many relationship, that contains meta, RESTfully?

From Dev

elegant way to update many-to-many relationship

From Dev

Two way one to many relationship

From Dev

Querying many-to-many relationship in MySql

From Dev

MySQL, join many to many relationship query

From Dev

Mysql - Is there a way to query a many-to-many relationship and include rows not in the linking table?

From Dev

"Best" way for loading many items

From Dev

MySQL filter many elements from many-to-many relationship

From Dev

Rails 4: how to implement routes for a many-to-many relationship?

From Dev

How to implement many to many relationship in oracle database correct?

From Dev

Best effective SQL update on one to many relationship

From Dev

Best practice for preloading core data with many to many relationship

From Dev

Best way to structure Core Data fetch for items, based on attribute(s) at other end of to-many relationship

From Dev

What is best way to fetch data from two table with one to many relationship?

From Dev

Is this a many-to-many relationship?

From Dev

Many to Many relationship in Ecto

From Dev

implementing a many to many relationship

From Dev

Many to many relationship optimization

From Dev

many to many powerpivot relationship

From Dev

Sqlalchemy many to many relationship

Related Related

  1. 1

    What the best way to model a many to many relationship

  2. 2

    Many to many relationship and MySQL

  3. 3

    Mysql Many to Many relationship

  4. 4

    Best way to query a Many to Many Relationship using pg-promise

  5. 5

    Best way to generate many to many relationship in Excel 2007?

  6. 6

    denormalize many to many relationship in MySQL

  7. 7

    MySQL, Best way to select entries with many one-to-many relationships?

  8. 8

    CoreData many to many relationship Best practices

  9. 9

    How to implement one to many relationship

  10. 10

    What's the best way of implementing a many-to-many relationship, that contains meta, RESTfully?

  11. 11

    elegant way to update many-to-many relationship

  12. 12

    Two way one to many relationship

  13. 13

    Querying many-to-many relationship in MySql

  14. 14

    MySQL, join many to many relationship query

  15. 15

    Mysql - Is there a way to query a many-to-many relationship and include rows not in the linking table?

  16. 16

    "Best" way for loading many items

  17. 17

    MySQL filter many elements from many-to-many relationship

  18. 18

    Rails 4: how to implement routes for a many-to-many relationship?

  19. 19

    How to implement many to many relationship in oracle database correct?

  20. 20

    Best effective SQL update on one to many relationship

  21. 21

    Best practice for preloading core data with many to many relationship

  22. 22

    Best way to structure Core Data fetch for items, based on attribute(s) at other end of to-many relationship

  23. 23

    What is best way to fetch data from two table with one to many relationship?

  24. 24

    Is this a many-to-many relationship?

  25. 25

    Many to Many relationship in Ecto

  26. 26

    implementing a many to many relationship

  27. 27

    Many to many relationship optimization

  28. 28

    many to many powerpivot relationship

  29. 29

    Sqlalchemy many to many relationship

HotTag

Archive