views:

386

answers:

9

When I do SELECT statements in PHP code I always select named columns, like:

SELECT id, name from users;

rather than using:

SELECT * from users;

This has the advantage of being more informative and readable, and also avoids problems later if new columns are added to the table.

What I'm wondering is, is it possible to use the same idea in an INSERT statement? I'm imagining it might be something like this:

INSERT into people values (id=1, name="Fred");

The syntax as I've shown in this example doesn't work, but I wonder if something equivalent is possible? If not, does anyone know why not? Is it a deliberate omission?

Ben

+26  A: 
INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)

INSERT INTO people (id, name)
VALUES (1, 'Fred');
jmein
+5  A: 
INSERT into people(id,name) values (1, 'Fred');
Paul Tomblin
this is correct, except you want single quotes instead of double quotes.
Ryan Guill
Thanks, I cut and pasted and didn't fix that part. Plus jmien beat me to it by about 10 seconds.
Paul Tomblin
+4  A: 
INSERT INTO table (column_1, column_2, column_3, ...)
VALUES (value_1, value_2, value_3, ...)
Oli
+5  A: 

what you want is

INSERT INTO PEOPLE ( id, name ) VALUES ( 1, 'FRED' )
Ryan Guill
+3  A: 

Try it this way: INSERT INTO people (id, name) VALUES (1, "Fred");

Peter
+4  A: 

Use INSERT INTO <table> (col1, col2) VALUES (1,2).

Keltia
+3  A: 

the syntax for this case is

INSERT into people (id, name) values (1, 'Fred');

at least it works in the dbms i use (PostgreSQL)

chburd
+2  A: 

That's syntax is possible with MySQL only. Afaik, other RDBMS doesn't allow that. Here's the syntax:

INSERT INTO games SET yr = 2012, city = 'London';

I wish PostgreSQL facilitated this kind of insert syntax

Standard ANSI syntax, however, would be

INSERT into people (yr, city) values (2012, 'London');

Michael Buen
I like this much better than the other way -- thanks!
Ben
The other method, however, is standard SQL, and portable to any standard-compliant RDBMS.
Harper Shelby
@Harper: That is an important point, but for me the MySQL-only version is fine, as I'm not writing production software, just stuff for doing things for me, and I'm only ever going to use MySQL... I think... :-)
Ben
You're going to be kicking yourself some time in the future for not doing it the standard SQL way.
Paul Tomblin
It unportable and I can not see the gains in any way.
Friedrich
Even PostgreSQL team consider that kind of syntax, it is more readable. I can't believe that I answer the question in good faith, no malicious intent or whatsoever, yet I received downvote. Well.. that's life... here.
Michael Buen
@Michael Buen: I agree with you. I personally think your answer was a good answer. I was looking for a MySQL solution, and in my view the answer you proposed is more readable than the more standard one. It's a shame you were downvoted.
Ben
I guess this answer is receiving broken window syndrome. I just hoped in some future time, many RDBMS vendors would consider this syntax more easy to follow than the standard one. That the variable and its value should of closed proximity with each other.
Michael Buen
+2  A: 

Using standard syntax, you can insert multiple rows at once:

INSERT INTO Table (column1,...,columnN) VALUES
(r1v1, ..., r1vN),
(r2v1, ..., r2vN),
(r3v1, ..., r3vN).

This is MUCH faster way of importing data, than one-row-a-time (especially in MySQL)! I don't think you can do the same using

INSERT INTO Table SET column1 = r1v1, ..., columnN = r1vN

syntax.

(From personal experience, inserting 300000 rows one row a time took several minutes. When I changed the script to use few insert statements only, each inserting several thousands of rows, it took only few seconds to import it all, MySQL 5.0).

Peter Štibraný