which one to use and why?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

which one to use and why?

Post 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
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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!
webaddict
Forum Commoner
Posts: 60
Joined: Wed Mar 14, 2007 6:55 am
Location: The Netherlands

Post 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' );
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Post Reply