0

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?

5 Answers 5

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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.

Comments

0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.