问题
I was told at my last position never to do this; it wasn't explained why. I understand it enhances the opportunity for mistakes, but if there are just a couple of columns and I'm confident of their order, why can't I shorthand it and just insert the values in the right order without explicitly matching up the column names? Is there a large performance difference? If so, does it matter on a small scale?
If there's no performance hit and this isn't a query that will be saved for others to view, why shouldn't I?
Thanks in advance.
回答1:
This is acceptable only when you type your query by hand into an interactive DB tool. When your SQL statement is executed by your program, you cannot be absolutely confident about the order of columns in a table, unless you are the only developer who has access to your database. In other words, in any team environment there is an opportunity that someone would break your query simply by re-ordering columns in your database. Logically, your table would remain the same, but your program would still break.
回答2:
Devil's advocate: if there are only a couple of columns, what does short-handing it gain you? You saved a few keystrokes, big deal.
For ad hoc queries you're writing once and throwing away, it's not a big deal. I suspect you were told to never do this in production code or anything anyone else would later have to reverse engineer (either simply to understand it, or to account for underlying schema changes). Remember that code that you write may only ever be viewed and maintained by you right now but you should be writing code with the intention that it will outlast you.
Another reason including the column list is good is if you later want to search for all references to a specific column name in your data model...
回答3:
The reason is to make the code more robust.
Specifying the fields makes the code less dependant on that the table layout stays exactly the same, and also gives you the ability to add fields to the table without the need to change the code as long as you provide default values for the new field.
It also makes it easier to see that the query is supposed to do, without the need to look up the table layout to see where the data will end up.
回答4:
In most development shops, you will not be the only developer working on a given project. In which case, you run the risk of unintended inserts. You may be confident now that there won't be any schema changes and things can change... now or after you're gone.
Also, if a column is added, you inserts will fail.
回答5:
Its mostly for readability so if its just you its fine but if you do muck up the order it can be a hard one to explain if people find out you were not using good practices, especially if someone modifies the structure while you were writing your query... Also if you have complex things to do in a column it can help visualisation and if as you say it is small then adding a few column names is hardly going to take long so you may as well.
回答6:
Because things change and if you code that query in your program, thinking that the column order will never change and then comes along someone else that decides to alter the table adding a new column your query will fail.
回答7:
Also, there's a quick trick if you don't want to type column names out. In management studio, you can set it up so that your result set returns in a CSV (with column headers). CTRL-T.
Then do a quick,
select top 1 * from <tablename>
and copy and paste the column list from the resultset window.
回答8:
A relation has no left-to-right ordering of columns. The same cannot be said of a SQL table, not a good thing. However, just because SQL has non-relational features doesn't mean you have to use them! Specifying a column list as part of a table or row value constructor is one way of mitigating against SQL's column ordering.
Consider that the following SQL statements are semantically equivalent:
INSERT INTO Table1 (col1, col2, col3) VALUES (1, 2, 3);
INSERT INTO Table1 (col2, col1, col3) VALUES (2, 1, 3);
INSERT INTO Table1 (col3, col2, col1) VALUES (3, 2, 1);
来源:https://stackoverflow.com/questions/9183339/insert-query-why-is-it-a-bad-idea-to-not-include-column-names