0

I know this is possible using transaction but I want to do it using single mysql query.

this is which common format transaction

START TRANSACTION;
SELECT @A:=SUM(salary) FROM table1 WHERE type=1;
UPDATE table2 SET summary=@A WHERE type=1;
COMMIT;

but I need to know a single mysql query is possible?

A.A Noman
  • 5,244
  • 9
  • 24
  • 46
Shafiqul Islam
  • 5,570
  • 2
  • 34
  • 43

5 Answers5

1

No, it can't be done in single statement like

insert into table1,table2

either you do separately like

insert into table1 ...

insert into table2 ...

(OR)

Wrap the insert statements in stored procedure and call that procedure like

create procedure sp_insert_multiple
as
begin
    insert into table1 ...

    insert into table2 ...
end

Call the SP

exec sp_insert_multiple
Rahul
  • 76,197
  • 13
  • 71
  • 125
1

Normally it is not possible to insert multiple table in single query. you can insert multiple row in a single table . like as

INSERT INTO tbl_test
(a1,a2,a3)
VALUES
(1,2,3),
(4,5,6),
(7,8,9);

you can do this in Oracle

by using procedure you can insert

create procedure insert_query
as
begin
insert into tbl_test1(a1,a2,a3) VALUES (1,2,3)
insert into tbl_test2 (b1,b2,b3) VALUES (1,2,3)
end
1

You can't do this. However, you can use a transaction and have both of them be contained within one transaction.

START TRANSACTION;
INSERT INTO table_1 VALUES ('1','2','3');
INSERT INTO table_2 VALUES ('one','two','three');
COMMIT;

See the following rule

A.A Noman
  • 5,244
  • 9
  • 24
  • 46
1

Normally, you can not do this in MySQL. But you can do this using concat query.

INSERT INTO your_table
(value1,value2,value3)
  VALUES
(a,b,c),
(d,e,f),
(i,j,k);

But this is not your question answer. so Your question answer is NO. MYSQL is not support it still now.

-2

you can do that like coz

MySQL doesn't support multi-table insertion in a single INSERT statement.

INSERT INTO NAMES VALUES(...)
INSERT INTO PHONES VALUES(...)

here is a link of detail answer..

sql - insert into multiple tables in one query

Community
  • 1
  • 1
Haroon
  • 480
  • 4
  • 14