2013-10-31 14 views
15

Ich habe eine Tabelle von Personen. Jede Person hat eine Eigenschaft und viele Personen können eine bestimmte Eigenschaft haben. Das ist also eine Viele-zu-Viele-Beziehung. Dies ist das Schema:MySQL - Wie man in eine Tabelle einfügt, die eine Viele-zu-Viele Beziehung hat

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) 
); 

Jetzt kann sagen, dass ich diese Person in die Datenbank eingefügt werden soll:

  • Vorname: 'John'
  • Nachname: 'Doe'
  • Eigenschaften:‘ property_A‘, 'property_B', 'property_C'

Personen

Eigenschaften

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

has_property

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

Bisher das Beste, was ich gedacht ist, einen regelmäßigen Einsatz in der Tabelle Personen zu tun:

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

und dann tun Wählen Sie die ID der Person, die ich gerade eingefügt habe

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

um in die anderen beiden Tabellen einzufügen (weil ich die person_id kennen muss). Aber ich denke es muss einen besseren Weg geben, oder?

+2

Sie können SELECT LAST_INSERT_ID() 'die ID der letzten eingefügten Zeile –

+2

Für diejenigen abzurufen, dies ist ein Duplikat sagen kann, ich diese Frage gelesen haben http://stackoverflow.com/questions/ 17767973/Daten-in-eine-Tabelle einfügen-das-hat-viele-zu-viele-Beziehung, aber es war vage und es hat mir nicht geholfen. –

Antwort

23

Hier ist, was ich getan habe. Ich hoffe es hilft jemandem.

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); 
Verwandte Themen