Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert Query: Why is it a bad idea to not include column names?

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.

like image 775
Yatrix Avatar asked Jan 17 '23 13:01

Yatrix


1 Answers

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.

like image 188
Sergey Kalinichenko Avatar answered Jan 25 '23 22:01

Sergey Kalinichenko