+17  A: 

The first query breaks pretty much any time the table schema changes. The second query accomodates any schema change that leaves its columns intact.

People who do SELECT * queries and then rely on positional notation for extracting the values they're concerned about are software maintenance supervillains for the same reason.

chaos
+5  A: 

I don't really care about theoretical concepts in this regard (as in practice, a table does have a defined column order). The primary reason I would prefer the second one to the first is an added layer of abstraction. You can modify columns in a table without screwing up your queries.

Mehrdad Afshari
However, somebody who is unaware that the first query exist could add a query in the middle of the first two, and mess up the query.
Kibbee
@Kibbee: ... and that's why it shouldn't exist.
Mehrdad Afshari
Sorry, seems that I misread your response the first time.
Kibbee
+7  A: 

While the order of columns is defined in the schema, it should generally not be regarded as important because it's not conceptually important.

Also, it means that anyone reading the first version has to consult the schema to find out what the values are meant to mean. Admittedly this is just like using positional arguments in most programming languages, but somehow SQL feels slightly different in this respect - I'd certainly understand the second version much more easily (assuming the column names are sensible).

Jon Skeet
+2  A: 

You should try to make your SQL queries depend on the exact layout of the table as little as possible.

The first query relies on the table only having three fields, and in that exact order. Any change at all to the table will break the query.

The second query only relies on there being those three felds in the table, and the order of the fields is irrelevant. You can change the order of fields in the table without breaking the query, and you can even add fields as long as they allow null values or has a default value.

Although you don't rearrange the table layout very often, adding more fields to a table is quite common.

Also, the second query is more readable. You can tell from the query itself what the values put in the record means.

Guffa
+1  A: 

SQL gives you syntax for specifying the name of the column for both INSERT and SELECT statements. You should use this because:

  • Your queries are stable to changes in the column ordering, so that maintenance takes less work.
  • The column ordering maps better to how people think, so it's more readable. It's more clear to think of a column as the "Name" column rather than the 2nd column.
James Thompson
+1  A: 

I prefer to use the UPDATE-like syntax:

INSERT t SET one = 1 , two = 2 , three = 3

Which is far easier to read and maintain than both the examples.

Peter Boughton
I don't believe this is cross-platform - at least, T-SQL doesn't seem to support it.
Michael Madsen
Another reason not to use T-SQL. ;)Looks like this is primarily a MySQL extension. It's a pity other DBMSs haven't added support for it.
Peter Boughton
Language evolution is fairly slow, especially for languages that have been around for a while like SQL. It's too bad, I like your example.
James Thompson
+1  A: 

Long term, if you add one more column to your table, your INSERT will not work unless you explicitly specify list of columns. If someone changes the order of columns, your INSERT may silently succeed inserting values into wrong columns.

AlexKuznetsov
+2  A: 
Michael Madsen