Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does ordering matter when doing an insert?

Tags:

mysql

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.

like image 660
nativist.bill.cutting Avatar asked Nov 12 '13 16:11

nativist.bill.cutting


2 Answers

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.
like image 87
Lavneet Avatar answered Oct 27 '22 15:10

Lavneet


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

like image 33
MxLDevs Avatar answered Oct 27 '22 15:10

MxLDevs