How to represent a many-to-many relationship in a relational database?

ehmicky

I have two tables Country and Region. A Region is a set of one or several Countries. Some Countries are in no Region.

How should I represent this in a relational database? I thought of the two following possibilities:

  • Country has a Edition column that includes either null or the Edition that it belongs to. My problem with this: I have been taught nulls are evil in a database.
  • Edition has a Countries column that is an array of Country. My problem with this: I have been taught arrays are evil in a database.

What's the best practice?

RichJ

One way is to create a third table with two columns, one contains CountryID the other RegionID where these are respectively the unique identifiers of Country and Region.

A row in this table means a relationship between a Country and a Region. As you can have more than one row in the table, you can store many-to-many relationships. If there is no relationship ( some countries are in no region ), there is no row in the table.

Here is an example

Table 1 - Country
ID Name
1 Spain
2 France
3 Germany
4 Norway
5 Belguim

Table 2 - Region
ID Name
1 Europe
2 BeneLux
3 EU Trading Region
4 ASIA

Table 3 - CountryRegion
Country Region
1 1
2 1
3 1
4 1
5 1
1 3
2 3
3 3
5 2

Which has expressed the following -
Spain is in Europe ( Country 1, Region 1 )
France is in Europe
Germany is in Europe
Norway is in Europe
Belgium is in Europe
Spain is in EU
France is in EU
Germany is in EU
Belguim is in BeneLux

No countries are in ASIA

Ths may not be geographically complete, or correct but I hope it shows the principle.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to set up relational database tables for this many-to-many relationship?

From Dev

How do I define a one-to-one relationship over a one-to-many relationship in a relational database?

From Dev

how to represent a many to many relationship with many records with a HTML based user interface

From Dev

how to represent a many to many relationship with many records with a HTML based user interface

From Dev

Do all relational database designs require a junction or associative table for many-to-many relationship?

From Dev

Ember.js: How to represent a has-many model relationship?

From Dev

How do I represent a one to many relationship with the Entity Framework?

From Dev

How to model one-to-many relationship in database

From Dev

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

From Dev

how to flatten many to many relationship

From Dev

how to query a many to many relationship?

From Dev

How to save many to many relationship?

From Dev

how to flatten many to many relationship

From Dev

Code first many to many relationship with attributes in relational table

From Dev

Code first many to many relationship with attributes in relational table

From Dev

many to many relationship of social network (database)

From Dev

Prevent duplicates in the database in a many-to-many relationship

From Dev

Database design for many to many relationship with optional columns?

From Dev

Django - Database design for many to many relationship involving an attribute of the relationship

From Dev

How to set strong parameter for many to many relationship

From Dev

How to get many to many relationship items in laravel

From Dev

How to save tag with many to many relationship in ActiveRecord

From Dev

How many to many relationship in Entity Framework is working

From Dev

How to write activemodel serializer for many to many relationship?

From Dev

SQLAlchemy. How to order on many to many relationship?

From Dev

how to save Many to Many relationship in django

From Dev

How to use checkboxes in a many-to-many relationship?

From Dev

jpa - How to create a many to many relationship with an IdClass?

From Dev

how to define many to many relationship in laravel models?

Related Related

  1. 1

    How to set up relational database tables for this many-to-many relationship?

  2. 2

    How do I define a one-to-one relationship over a one-to-many relationship in a relational database?

  3. 3

    how to represent a many to many relationship with many records with a HTML based user interface

  4. 4

    how to represent a many to many relationship with many records with a HTML based user interface

  5. 5

    Do all relational database designs require a junction or associative table for many-to-many relationship?

  6. 6

    Ember.js: How to represent a has-many model relationship?

  7. 7

    How do I represent a one to many relationship with the Entity Framework?

  8. 8

    How to model one-to-many relationship in database

  9. 9

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

  10. 10

    how to flatten many to many relationship

  11. 11

    how to query a many to many relationship?

  12. 12

    How to save many to many relationship?

  13. 13

    how to flatten many to many relationship

  14. 14

    Code first many to many relationship with attributes in relational table

  15. 15

    Code first many to many relationship with attributes in relational table

  16. 16

    many to many relationship of social network (database)

  17. 17

    Prevent duplicates in the database in a many-to-many relationship

  18. 18

    Database design for many to many relationship with optional columns?

  19. 19

    Django - Database design for many to many relationship involving an attribute of the relationship

  20. 20

    How to set strong parameter for many to many relationship

  21. 21

    How to get many to many relationship items in laravel

  22. 22

    How to save tag with many to many relationship in ActiveRecord

  23. 23

    How many to many relationship in Entity Framework is working

  24. 24

    How to write activemodel serializer for many to many relationship?

  25. 25

    SQLAlchemy. How to order on many to many relationship?

  26. 26

    how to save Many to Many relationship in django

  27. 27

    How to use checkboxes in a many-to-many relationship?

  28. 28

    jpa - How to create a many to many relationship with an IdClass?

  29. 29

    how to define many to many relationship in laravel models?

HotTag

Archive