E
E
evilelf2015-10-08 11:27:13
MySQL
evilelf, 2015-10-08 11:27:13

How to make multiple insert into 2 tables at once?

Hello!
Given:
There are table1 and table1_meta.
Relationship 1(table1) to many(table1_meta). By column table1_id
Task:
It is necessary to make an insert in table1 and a lot of rows in table1_meta at once.
Questions:
How to make it most competently for insert 1 of the table1 row?
How to do it most competently for insert 10 rows of table1 ?
Thanks everyone for the replies.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
T
Taxist410, 2015-10-08
@Taksist410

At first you do insert 10 rows of table1 and only then set of rows in table1_meta.

I
Ivanq, 2015-10-08
@Ivanq

You won't be able to insert directly into different tables. Only two requests.

M
Max, 2015-10-08
@MaxDukov

wow-insert-in-one-query is called LOAD DATA INFILE - will insert values ​​from a file. But in 1 table.
so 2 times - it is necessary.

A
Alex Safonov, 2015-10-08
@elevenelven

It is possible to try through triggers . But strongly depends on what data goes to the second table.
Perhaps it is possible through transactions.
Give examples of requests.

A
Artem Klimenko, 2015-10-08
@aklim007

dev.mysql.com/doc/refman/5.6/en/optimizing-innodb-...
you need a plot:

SET foreign_key_checks=0;
... SQL import statements ...
SET foreign_key_checks=1;

you just need to understand what exactly you are doing, and that the data you insert will not violate the integrity of foreign keys, if everything is ok, then just shove the data into the tables as you like, at least mixed up.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question