nativist.bill.cutting
nativist.bill.cutting

Reputation: 1382

Does ordering matter when doing an insert?

For this query:

"INSERT INTO credentials (h_token, h_file, h_pass, email, name, picture, privacy) VALUES (?, ?, ?, ?, ?, ?, ?)",

does the table structure matter in terms of the ordering of the column names.

Can h_token, h_file, etc. appear in any order? By order I'm referring to what phpmyadmin displays, I assume there is some internal order as well.

I'm 90% they can, but I wanted to make sure.

Upvotes: 10

Views: 17308

Answers (2)

MxLDevs
MxLDevs

Reputation: 19506

Columns can be specified in any order as long as they exist, but the values need to match your specified columns.

Upvotes: 2

Lavneet
Lavneet

Reputation: 626

If you're not specifying column names, then ordering matters (you must INSERT in the same order that the table is structured). If you are specifying the column names, the order doesn't matter.

For example:

INSERT INTO TABLE_NAME VALUES ('','','')
// Here the values needs to be in order of columns present in your table.

INSERT INTO TABLE_NAME (ID, NAME, EMAIL) VALUES ('','','')`
// Here, ordering can be changed as per requirement.

Upvotes: 27

Related Questions