Tekforums

Chat => Entertainment & Technology => Topic started by: Cheule on October 01, 2007, 09:50:18 AM

Title: MySQL column ordering
Post by: Cheule on October 01, 2007, 09:50:18 AM
Hi peeps, this promises to be the first of a few questions Ill most likely have about MySQL and PHP. Im starting to program a database and a simple stock checking service for my other half, who has set up a business based around cosmetics.

My first question:

When I originally made the database table, I added columns as I thought of them, instead of sitting down and writing down exactly what was needed (whoops!). Thus, the table is a bit mish-mash with regards to legibility. Is there any way I can move a column left or right in the table or do I have to start the table from fresh?
Title: MySQL column ordering
Post by: Sam on October 01, 2007, 23:09:19 PM
Why do you care about the order of the columns?
Title: MySQL column ordering
Post by: cornet on October 01, 2007, 23:37:47 PM
Quote from: SamWhy do you care about the order of the columns?

Indeed... If your writing your queries like this:

select * from .....

Then stop right away, always specify the column name. That way when you next re-order your columns or add a column then all your code wont break!

If you do want to re oder your columns then check out the "alter table" syntax for your database
Title: MySQL column ordering
Post by: Sam on October 02, 2007, 10:44:44 AM
Theres nothing massively wrong with select *, just access the data via the column name rather than index.
Title: MySQL column ordering
Post by: cornet on October 02, 2007, 12:44:54 PM
Quote from: SamTheres nothing massively wrong with select *, just access the data via the column name rather than index.

* Your returning data youre not using
* It makes the code easier to read if you specify your fields explicitly
* Itll stop you from making stupid mistakes if you get into the habbit of using it.

All companies Ive worked for have this as an SQL coding guideline, and it would seem IBM do the same.

http://www.ibm.com/developerworks/db2/library/techarticle/0210mullins/0210mullins.html

Title: MySQL column ordering
Post by: Cheule on October 02, 2007, 14:25:15 PM
Im not fussed about the column order per se, its just when Im looking at the raw database in phpmyadmin, its well, a bit mish-mash. Due to the fact Im a bit of a novice at it :)