Reputation: 23
When I get a insert statement from PHPMyAdmin it looks like this
INSERT INTO `users`(`user_id`, `user_name`, `user_passwd`) VALUES ([value-1],[value-2],[value-3])
But wouldn't this also work:
INSERT INTO `users` VALUES ([value-1],[value-2],[value-3])
Why does the extra part do?
Upvotes: 0
Views: 50
Reputation: 467
It will only work if you specify the values in the same order as they would appear in the table. If you want to specify values in some other order or only insert values to a few of the columns across the table you must use the full version.
Upvotes: 0
Reputation: 32
It allows you to put the items in any order and it also allows you to exclude auto-increment fields. You can exclude it if you are inserting data to every field in that order.
Upvotes: 1
Reputation: 12305
In the first case you can insert your values in any order and not necessarily the order in which you have defined your table's schema. Also you may chose to skip nullable columns.
In the second case you have to insert values as they are defined by your table schema and you cannot skip nullable columns. You'll have to explicitly provide null
values for them.
Upvotes: 1
Reputation: 300
With the first approach you can define your own column order to insert values. Without specyfing it (as in second example) you have to insert values in the same order as the table project
Upvotes: 0
Reputation: 42440
The 'extra part' is defining the columns (in order) to which you are inserting.
You can omit that part IF AND ONLY IF you are inserting values in to every single column. That is, if the number of values specified, matches the number of columns in the table.
Big, bold note:
Even if you are inserting a value into every column, it is a very good idea to still specify the columns. The reason being, if tomorrow you decide you need to add another column to your table, all existing code where you did not specify the columns will break.
Upvotes: 3