MySQL - 如何插入具有多对多关系的表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19714308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 19:17:52  来源:igfitidea点击:

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

mysqlmany-to-manyrelationshipsql-insert

提问by Christos Baziotis

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'
  • 名字:'约翰'
  • 姓氏:'Doe'
  • 属性:'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

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

然后做一个选择来找到我刚刚插入的人的id

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?

为了插入其他两个表(因为我需要知道 person_id)。但我认为一定有更好的方法,不是吗?

回答by Christos Baziotis

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