Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL - How to insert into table that has many-to-many relationship

I have a table of persons. Each person has a property and many persons may have a certain property. So this is a many-to-many relationship. This is the schema:

CREATE TABLE persons (   person_id int(11) NOT NULL AUTO_INCREMENT,   firstname varchar(30) NOT NULL,   lastname varchar(30) NOT NULL,   PRIMARY KEY (person_id) );  CREATE TABLE properties (   property_id int(11) NOT NULL AUTO_INCREMENT,   property varchar(254) NOT NULL UNIQUE,   PRIMARY KEY (property_id) );  CREATE TABLE has_property (   person_id int(11) NOT NULL,   property_id int(11) NOT NULL,   PRIMARY KEY (person_id,property_id),   FOREIGN KEY (person_id) REFERENCES persons (person_id),   FOREIGN KEY (property_id) REFERENCES properties (property_id) ); 

Now lets say i want to insert to the database this person:

  • firstname:'John'
  • lastname:'Doe'
  • properties:'property_A','property_B','property_C'

persons

+-----------+-----------+----------+ | person_id | firstname | lastname | +-----------+-----------+----------+ |         1 | John      | Doe      | +-----------+-----------+----------+ 

properties

+-------------+------------+ | property_id |  property  | +-------------+------------+ |           1 | property_A | |           2 | property_B | |           3 | property_C | +-------------+------------+ 

has_property

+-----------+-------------+ | person_id | property_id | +-----------+-------------+ |         1 |           1 | |         1 |           2 | |         1 |           3 | +-----------+-------------+ 

So far the best thing i have thought is to do a regular insert in the persons table:

INSERT INTO persons (firstname,lastname) VALUES ('John','Doe'); 

and then do a select to find the id of the person i just inserted

SELECT person_id FROM persons WHERE firstname='John' AND lastname='Doe'; 

in order to insert into the other two tables (because i need to know the person_id). But i think there must be a better way, isn't it?

like image 719
Christos Baziotis Avatar asked Oct 31 '13 18:10

Christos Baziotis


People also ask

How do you insert data into a many-to-many relationship in SQL?

When you need to establish a many-to-many relationship between two or more tables, the simplest way is to use a Junction Table. A Junction table in a database, also referred to as a Bridge table or Associative Table, bridges the tables together by referencing the primary keys of each data table.

How do you connect many-to-many relationships?

For those relationships, you simply connect the appropriate fields with a line. To create many-to-many relationships, you need to create a new table to connect the other two. This new table is called an intermediate table (or sometimes a linking or junction table).

How do you solve a many-to-many relationship in SQL?

Many-to-many (M:M) A relationship is many-to-many if and only if one record from table A is related to one or more records in table B and vice-versa. To establish a many-to-many relationship, create a third table called "ClassStudentRelation" which will have the primary keys of both table A and table B.

Can a table have a many-to-many relationship with itself?

A self-referencing many-to-many relationship exists when a given record in the table can be related to one or more other records within the table and one or more records can themselves be related to the given record.


1 Answers

Here is what i ended up doing. I hope it helps someone.

INSERT INTO persons (firstname,lastname) VALUES ('John','Doe'); SET @person_id = LAST_INSERT_ID();  INSERT IGNORE INTO properties (property) VALUES ('property_A'); SET @property_id = LAST_INSERT_ID(); INSERT INTO has_property (person_id,property_id) VALUES(@person_id, @property_id);  INSERT IGNORE INTO properties (property) VALUES ('property_B'); SET @property_id = LAST_INSERT_ID(); INSERT INTO has_property (person_id,property_id) VALUES(@person_id, @property_id);  INSERT IGNORE INTO properties (property) VALUES ('property_C'); SET @property_id = LAST_INSERT_ID(); INSERT INTO has_property (person_id,property_id) VALUES(@person_id, @property_id); 
like image 64
Christos Baziotis Avatar answered Oct 08 '22 02:10

Christos Baziotis