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

maxedison

I have a type of data called a chain. Each chain is made up of a specific sequence of another type of data called a step. So a chain is ultimately made up of multiple steps in a specific order. I'm trying to figure out the best way to set this up in MySQL that will allow me to do the following:

  1. Look up all steps in a chain, and get them in the right order
  2. Look up all chains that contain a step

I'm currently considering the following table set up as the appropriate solution:

TABLE chains
id   date_created

TABLE steps
id   description

TABLE chains_steps  (this would be used for joins)
chain_id   step_id   step_position

In the table chains_steps, the step_position column would be used to order the steps in a chain correctly. It seems unusual for a JOIN table to contain its own distinct piece of data, such as step_position in this case. But maybe it's not unusual at all and I'm just inexperienced/paranoid.

I don't have much experience in all this so I wanted to get some feedback. Are the three tables I suggested the correct way to do this? Are there any viable alternatives and if so, what are the advantages/drawback?

Jeremy Smyth

You're doing it right.

Consider a database containing the Employees and Projects tables, and how you'd want to link them in a many-to-many fashion. You'd probably come up with an Assignments table (or Project_Employees in some naming conventions).

At some point you'd decide you want not only to store each project assignment, but you'd also want to store when the assignment started, and when it finished. The natural place to put that is in the assignment itself; it doesn't make sense to store it either with the project or with the employee.

In further designs you might even find it necessary to store further information about the assignment, for example in an employee review process you may wish to store feedback related to their performance in that project, so you'd make the assignment the "one" end of a relationship with a Review table, which would relate back to Assignments with a FK on assignment_id.

So in short, it's perfectly normal to have a junction table that has its own data.

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 represent a many-to-many relationship in a relational database?

From Dev

How to JOIN tables in 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 set strong parameter for many to many relationship

From Dev

Many to many relationship between AspNetUsers (Identity) and other tables in a custom database

From Dev

Laravel 4 how to set up many-to-many relationship with the same table

From Dev

How do I set up a many-to-many relationship in iOS? (Core Data)

From Dev

how to get the third properties from many to many relationship tables?

From Dev

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

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 set the referenced collection of many-to-many relationship?

From Dev

Many to Many Relationship over multiple tables

From Dev

Joining two tables with many to many relationship in sql

From Dev

Many to Many Relationship over multiple tables

From Dev

Entity Framework with many to many relationship generetad tables

From Dev

Joining two tables with many to many relationship in sql

From Dev

How do I set up the rails models in a has_and_belongs_to_many relationship?

From Dev

How to set up an extra constraint for 1-to-many relationship without using triggers

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

Many to many relationship returns empty set

Related Related

  1. 1

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

  2. 2

    How to JOIN tables in many-to-many relationship

  3. 3

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

  4. 4

    How to set strong parameter for many to many relationship

  5. 5

    Many to many relationship between AspNetUsers (Identity) and other tables in a custom database

  6. 6

    Laravel 4 how to set up many-to-many relationship with the same table

  7. 7

    How do I set up a many-to-many relationship in iOS? (Core Data)

  8. 8

    how to get the third properties from many to many relationship tables?

  9. 9

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

  10. 10

    How to model one-to-many relationship in database

  11. 11

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

  12. 12

    How to set the referenced collection of many-to-many relationship?

  13. 13

    Many to Many Relationship over multiple tables

  14. 14

    Joining two tables with many to many relationship in sql

  15. 15

    Many to Many Relationship over multiple tables

  16. 16

    Entity Framework with many to many relationship generetad tables

  17. 17

    Joining two tables with many to many relationship in sql

  18. 18

    How do I set up the rails models in a has_and_belongs_to_many relationship?

  19. 19

    How to set up an extra constraint for 1-to-many relationship without using triggers

  20. 20

    how to flatten many to many relationship

  21. 21

    how to query a many to many relationship?

  22. 22

    How to save many to many relationship?

  23. 23

    how to flatten many to many relationship

  24. 24

    Code first many to many relationship with attributes in relational table

  25. 25

    Code first many to many relationship with attributes in relational table

  26. 26

    many to many relationship of social network (database)

  27. 27

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

  28. 28

    Database design for many to many relationship with optional columns?

  29. 29

    Many to many relationship returns empty set

HotTag

Archive