4
INSERT INTO `table` VALUES ('val1','val2','val3')

INSERT INTO `table` SET a='val1',b='val2',c='val3'

Both are used for the same purpose.but which i should use? in which case? and why?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Petru Lebada
  • 2,167
  • 8
  • 38
  • 59

3 Answers3

4

They are identical, but the first one is standard SQL. As the SQL parser treats them as synonyms there is no difference in performance, usage or anything. As always, prefer using the standardized form, the first syntax in this case, for portability and compatibility.

Refer to the SQL-92 standard syntax.

Niels Keurentjes
  • 41,402
  • 9
  • 98
  • 136
  • yes,i already know that,but i've just been asked by my superior,those questions,and i dont know what to answer.... she said,is not a definition or something but a logic thing,a choice of my own,as a programmer.When to use one and when to use the other? – Petru Lebada Aug 27 '14 at 10:47
  • As said, they are functionally identical. But the first one is standard, the second one is proprietary. And you should always prefer standardized solutions over proprietary, when completely identical like in this case. Not much more to say about it. – Niels Keurentjes Aug 27 '14 at 10:49
  • well,i told this to my superior,and she said is not what she meant – Petru Lebada Aug 27 '14 at 10:54
  • 1
    Well she's not quite superior then. – Niels Keurentjes Aug 27 '14 at 10:57
  • she didn't said that is wrong your statement,i told you that i already know that,and i gave her this answer,among others,and she said to think again.... – Petru Lebada Aug 27 '14 at 11:00
  • This is a Q&A site, not a riddle site. You got several perfectly fine and correct answers to the question you asked. If this is not what she wants to hear you're apparently asking the wrong question. – Niels Keurentjes Aug 27 '14 at 11:13
  • im sure i didnt since,she keeps repeating it. – Petru Lebada Aug 27 '14 at 11:23
  • Let her ask her own question then in her own words. – Niels Keurentjes Aug 27 '14 at 11:25
0

As far as I can tell, both syntaxes are equivalent.

The first is SQL standard, the second is MySQL's extension.

So they should be exactly equivalent performance wise.

http://dev.mysql.com/doc/refman/5.6/en/insert.html says:

INSERT inserts new rows into an existing table. 
The INSERT ... VALUES and INSERT ... SET forms of the statement 
insert rows based on explicitly specified values. 
The INSERT ... SELECT form inserts rows selected from another 
table or tables.
Smruti Singh
  • 565
  • 1
  • 4
  • 14
-2

Fields are not always mandatory in a table. Sometime you just want to set some fields not all of them. Then you will use the second form.

The first form is usually auto generated with a script like:

$query = "INSERT INTO `table` VALUES ("
for(@values) {
   $query .= "'$_'";
}
$query .= ")";

mysql_query($query);

But the correct way would be to use a more usual form which is:

@fields = (qw/name firstname address/);
@values = (qw/Wall Larry Paris/);

$query = "INSERT INTO `table` (";
$query .= join(',', @fields);    
$query .= ") VALUES (";
$query .= join(',', @values);
$query .= ")";



mysql_query($query);
nowox
  • 25,978
  • 39
  • 143
  • 293
  • 1
    -1 for factual errors - the standard way of setting a limited number of values is `insert into (fields) values (values)`. It is most certainly not a reason to 'need' the second form.
    – Niels Keurentjes Aug 27 '14 at 10:47
  • I know this @coin,but this is not what i was looking for...I was asked by my superior which one i should use as a programmer and why,she said is not definition or something like that,but a logic thing that i should think as a programmer – Petru Lebada Aug 27 '14 at 10:52
  • You should use the first one and show this link to your superior http://www.w3schools.com/sql/sql_insert.asp – nowox Aug 27 '14 at 10:58
  • Please, never link to w3schools - it's the worst site on the internet for any kind of reference. http://www.w3fools.com/ exists for a reason. – Niels Keurentjes Aug 27 '14 at 11:16