Question: SELECT * fields order

Question

SELECT * fields order

Answers 1
Added at 2016-12-26 02:12
Tags
Question

Does MySQL guarantee that a query like

SELECT * from mytable

will output its fields exactly in the same order as

DESCRIBE mytable

does?

We have a rule that the first column of all tables is always the PRIMARY KEY, and while it's maybe not always a good idea to rely on the columns order in a select *, it would be very helpful to be sure the first field provided is the PK.

This is a MySQL specific question and thus not a duplicate of the link given below.

Answers to

SELECT * fields order

nr: #1 dodano: 2016-12-26 02:12

The answer is no, mysql does not guarantee that the columns in a select * will be in the same order as in describe tablename output. Mysql documentation does not specify the order of the columns in either case. However, both statements are likely to list columns by their ordinal positions, so in reality the outputs should match.

But even if the order of the fields were the same in both cases, this would not mean that the pk fields are listed first. You can have pk defined on any columns, not just the leftmost ones.

Source Show
◀ Wstecz