mysql - Simple many-to-many relationships -


apologies, haven't slept, want clarify in head.

a team can have many users, user can member of several teams.

is many-to-many relationship? if so, need third table join them in mysql?

many many relationships

many many relationships exist , you're right point works somehow differently many 1 / 1 many. it's not at all stupid question.

most of time, need add fields characteristics relation. because you'll want informations relationships themselves.

whether or not need information (and fields) determine if need third-part table or not.

real life example : men & women

let's have men , women tables. man can date many women through course of live, , reciprocally. so, have typical many many relationship. can without third table.

but now, suppose want add information: each relationship (be in romantic sense or in database sense (never thought sentence 1 day in life)), want tell when union started, , when ended.

then need third part table.

structure

let's have @ our real life example structure like.

i wrote in simplified doctrine2-inspired yaml, model runs symfony2 default:

tablename: man     fields:         id:             type: int             id: true         firstname:             type: string         dateofbirth:             type: datetime         favsport:             type: string         womenheprefers:             type: string     onetomany:         relationships:             target: manmeetswoman             mappedby: man             cascade: remove  tablename: woman     fields:         id:             type: int             id: true         firstname:             type: string         dateofbirth:             type: datetime         favauthor:             type: string         mensheprefers:             type: string     onetomany:         relationships:             target: manmeetswoman             mappedby: woman             cascade: remove  tablename: manmeetswoman     fields:         id:             type: int             id: true         dateofencounter:             type: datetime         dateofbreakup:             type: datetime     manytoone:         man:             target: man             inversedby: relationships         woman:             target: woman             inversedby: relationships 

key points

  • your man , woman tables both have onetomany relationship manmeetswoman table
  • your manmeetswoman table have manytoone relationship both other tables
  • ideally, place cascading rules ensure when man deleted man table, relationships referencing him disappear third table.

what if don't want table

then have declare actual many many relationships.

it work pretty same, you won't have additional information existed between man , woman, other yeah, happened between them. don't know what, don't know when, don't know how long.

to this, replace onetomany , manytoone this:

# man:     manytomany:         woman:             target: woman  # woman:     manytomany:         man:             target: man 

but begins tricky manage, without support of orm. so, third-part table remains best , easiest solution. plus, allows add informations relationship itself.


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -