Page 1 of 1

which one to use and why?

Posted: Fri May 11, 2007 1:17 am
by PHPycho
Hello forums !!
I had seen the Insert Query can be done in two ways like
1>

Code: Select all

INSERT INTO table_name (field1,field2 etc) VALUES ('value1','value2' etc)
2>

Code: Select all

INSERT INTO table_name SET field1 = 'value1', field2 = 'value2' etc
I want to know
which one is more effective ??
Thanks in advance to all of you

Posted: Fri May 11, 2007 2:37 am
by Kieran Huggins
They both do the same thing at the same speed, it's just a matter of which query is more convenient to write. I've had many situations where each one was the clear choice - you will too!

Posted: Fri May 11, 2007 2:53 am
by webaddict
Phpyco,

They are both equally effective - as Kieran said. The former has an advantage though, because you can insert multiple rows in one statement, whereas the latter can't. I.e.:

INSERT INTO mytable ( foo, bar ) VALUES ( 'foo1', 'bar1' ), ( 'foo2', 'bar2' );

Posted: Mon May 14, 2007 8:00 am
by feyd
webaddict wrote:Phpyco,

They are both equally effective - as Kieran said. The former has an advantage though, because you can insert multiple rows in one statement, whereas the latter can't. I.e.:

INSERT INTO mytable ( foo, bar ) VALUES ( 'foo1', 'bar1' ), ( 'foo2', 'bar2' );
...provided the database supports this behavior. Not all do.