mysql - Implementing association tables -
mysql - Implementing association tables -
i wondering if help me organize mysql tables in way consider right (i read somewhere association tables i'm looking for). i'm having problem implementing them.
here's example:
location type event date location 1 bar, disco event1 fri location 1 bar, disco event2 saturday location 2 bar, restaurant event3 fri
how go if wanted have 'location 1' in database once, , have events associated stored elsewhere? same applies type section. having problem deciding how should set multiple variables 1 location, such bar beingness restaurant, etc...
the relationship between locations
, events
illustration of 1-to-many relationship. means each individual location can have many events associated it. these types of relationships implemented adding foreign key 'many' table (events) references primary key of 'one' table (locations).
the relationship between 'locations' , 'types' illustration of many-to-many relationship. means location can have many types , type can related many locations. these types of relationships implemented link table, contains foreign keys related rows. link table has composite primary key of 2 foreign keys, means 1 location can't linked 'bar' type twice.
so, next table structures might suitable you:
location: id (primary key), locationname, ... events: id (primary key), locationid (foreign key), date, name, ... locationtypes: locationid (fk), typeid (fk) types: id (pk), name, ...
to query info across several of tables, have utilize joins. 1-to-many relationship, next query work:
select l.locationname, e.name, e.date location l bring together events e on l.id = e.locationid
for many-to-many relationship, next query bring together information.
select l.locationname, t.name typename location l bring together locationtypes lt on l.id = lt.locationid bring together types t on lt.typeid = t.id
these examples show standard inner join, there other bring together types may improve suit needs.
mysql
Comments
Post a Comment