snippetsqlModerate
How to insert values in junction table for many to many relationships?
Viewed 0 times
insertjunctionformanyhowvaluesrelationshipstable
Problem
I have three tables in database:
Since many trips can have many places, I have one junction table as above.
If a user inserts places to the trip, the places will be added to
So, if I write a query like:
then how to store
trips(trip_id(pk), trip_name(unique), user_id(fk))
places(place_id(pk), place_name(unique))
trips_places_asc(trip_id(fk), place_id(fk))Since many trips can have many places, I have one junction table as above.
If a user inserts places to the trip, the places will be added to
places table and the trip will be associated with the places in trips_places_asc table.So, if I write a query like:
INSERT INTO places (place_name)
VALUES ('XYZ')
INSERT INTO trips (trip_name)
VALUES ('MyTrip')then how to store
trip_id and place_id in Junction or Association table trips_places_asc? Will I have to fire two queries?Solution
You want to maintain atomicity of the action of storing a new place and trip, along with the intersection between the two. In MySQL, you do this with a
You use the MySQL function
To add a place and a trip and associate the two all at once, you would write code like this:
You should add error handling to the above and if an error occurs part way through, use the
You should also read up on LAST_INSERT_ID() to make sure you know how it works. It can give you undesirable results in some cases, such as when you insert multiple rows with a single insert statement.
START TRANSACTION / COMMIT transactional wrapper.You use the MySQL function
LAST_INSERT_ID() (See documentation; similar to SQL Server variable @@IDENTITY) to get the value of the most recent auto increment value assigned. To add a place and a trip and associate the two all at once, you would write code like this:
START TRANSACTION
DECLARE placeKey int
INSERT INTO places (place_name)
VALUES ('XYZ')
SET placeKey = LAST_INSERT_ID()
DECLARE tripKey int
INSERT INTO trips (trip_name)
VALUES ('MyTrip')
SET tripKey = LAST_INSERT_ID()
INSERT INTO trips_places_asc(trip_id, place_id)
VALUES (tripKey, placeKey)
COMMITYou should add error handling to the above and if an error occurs part way through, use the
ROLLBACK command to undo the partially completed work.You should also read up on LAST_INSERT_ID() to make sure you know how it works. It can give you undesirable results in some cases, such as when you insert multiple rows with a single insert statement.
Code Snippets
START TRANSACTION
DECLARE placeKey int
INSERT INTO places (place_name)
VALUES ('XYZ')
SET placeKey = LAST_INSERT_ID()
DECLARE tripKey int
INSERT INTO trips (trip_name)
VALUES ('MyTrip')
SET tripKey = LAST_INSERT_ID()
INSERT INTO trips_places_asc(trip_id, place_id)
VALUES (tripKey, placeKey)
COMMITContext
StackExchange Database Administrators Q#81604, answer score: 10
Revisions (0)
No revisions yet.