3

Will this work? (i dont have a mysql server available to test >.< )

mytable:
myid : integer/primary key
mydata : text

INSERT INTO mytable VALUES (1,"new row"),(2,"brand new row"),(3,"yup another new row"), [.....more and more coma-separated parentheses with values], (1000,"guess what? yes new row") ON DUPLICATE KEY UPDATE mydata = "dang, this row already exists!";
2
  • what is the key? is the ID the primary key? if so, can you get a duplicate in the query? ON DUPLICATE KEY works only for unique values or key combinations Commented Mar 12, 2012 at 15:00
  • yes "myid" is the primary key. just assume there are already some rows in the table Commented Mar 12, 2012 at 15:07

3 Answers 3

4

Yes, it will work, but it is advisable to explicitly name the columns first:

INSERT INTO mytable
  (myid, mydata)
   VALUES (1,"new row"),(2,"brand new row"),(3,"yup another new row")

Also, single quotes are a little more syntactically portable, though MySQL will handle them correctly.

   VALUES (1,'new row'),(2,'brand new row'),(3,'yup another new row')

Note, if you already have values in mytable, you may encounter primary key collisions for id. You will need to decide what action should be taken in that circumstance, and apply ON DUPLICATE KEY accordingly.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you for your answer Michael! (i'll accept it as soon as i'm able to)
2

I don't see why not:

INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
    [INTO] tbl_name [(col_name,...)]
    {VALUES | VALUE} ({expr | DEFAULT},...),(...),...
    [ ON DUPLICATE KEY UPDATE
      col_name=expr
        [, col_name=expr] ... ]

from http://dev.mysql.com/doc/refman/5.5/en/insert.html. There's even an example here

INSERT INTO table (a,b,c) VALUES (1,2,3),(4,5,6)
  ON DUPLICATE KEY UPDATE c=VALUES(a)+VALUES(b);

1 Comment

Thank you for your answer and documentation Mr Eder!!! (Michael was a bit faster though ;) )
0

Yes it would. But if your running really large SQL statements - be careful of hitting the maximum SQL size (default is 1Mb I think) you may start to get some odd mysql errors when hitting this length

1 Comment

Yup that's correct. It is max_packet_size or max_allowed_packet if i remember correctly...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.