1

I have created a table called "abcd" with 3 attributes, so now I'm trying to insert some values into this table I created.

I found that to insert a value you do something like this

INSERT INTO abcd
VALUES (value1,value2,value3);

So this works fine but what if I have multiple things to enter, is there a way to easily insert multiple entries into a table that does something like the code below?

INSERT INTO abcd
VALUES (value1,value2,value3);
INSERT INTO abcd
VALUES (val1,val2,val3);
INSERT INTO abcd
VALUES (v1,v2,v3);
INSERT INTO abcd
VALUES (1,2,3);

Do I really need to do "insert into abcd" for every single line I need to insert?

Gurwinder Singh
  • 38,557
  • 6
  • 51
  • 76
BIGGEST
  • 209
  • 1
  • 3
  • 13
  • this question has the answer http://stackoverflow.com/questions/6889065/inserting-multiple-rows-in-mysql – sid-m Feb 25 '17 at 17:18

3 Answers3

0

You could do something like this:

INSERT INTO abcd
VALUES 
(value1, value2, value3),
(val1, val2, val3),
(v1, v2, v3),
(1, 2, 3);
Gurwinder Singh
  • 38,557
  • 6
  • 51
  • 76
0

You can combine the values lists:

INSERT INTO abcd
    VALUES (value1, value2, value3),
           (val1, val2, val3),
           (v1, v2, v3),
           (1, 2, 3);

I should note that if there is an error -- say an invalid foreign key reference, or a trigger that returns an error, or a unique constraint violation -- then nothing will be inserted at all. That is, this statement is all-or-nothing.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

you can repeat the values part of the statement like

insert into abcd values(1,2,3,4),(5,6,7,8)

as many times as required

sid-m
  • 1,504
  • 11
  • 19