Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practices for ordering columns in SQL when creating the table

I have a table foo which contains the following columns

create table foo (
id integer not null auto_increment unique,
name varchar(255),
desc varchar(255),
modified_time datetime not null,
type tinyint(1)
)

Are there any best practices in following a ordering convention for all the columns (e.g. in a alphabetical fashion or not nulls at the top and rest of the columns at the bottom)

like image 515
priya Avatar asked Oct 29 '11 12:10

priya


People also ask

Does order of columns matter in SQL?

Column order does not matter while creating a table. We can arrange the columns while retrieving the data from the database. Primary key can be set to any column or combination of columns.

How do I order columns in SQL?

To change the column order In Object Explorer, right-click the table with columns you want to reorder and select Design. Select the box to the left of the column name that you want to reorder. Drag the column to another location within the table.

Does order matter in SQL create table?

Either way, the answer is - it doesn't matter. Columns, you should have some sort of logic so that someone approaching your table blind can understand how things flow ideally, but they can reconstruct it however they want in the select statement. Rows, it truly doesn't matter at all.


2 Answers

Certainly the primary key first.
The name of that column is usually tablename_id (traditional) or just 'id' (preferred by frameworks such as rails).

If used, usually a name/description field next, as you have it.

I tend to put foreign keys after that (parents first, children after that) as they tend to be more critical during development.

Then I group other data, e.g. address line, city, state, zip together.

When no other rule fits, I tend to prefer required fields higher up for increased visibility.

timestamps (some/all of created_on, updated_on, removed_on, etc.) usually last

So in your example I would actually do:

create table foo (
id integer not null auto_increment unique,
name varchar(255),
type tinyint(1),
desc varchar(255),
modified_time datetime not null
)

Note - As Kolink noted, use description over desc because desc is a reserved word meaning descending, e.g. order desc

However... big disclaimer...

if your table changes over time (i.e. real world) and you have existing production data, you will not have the fields ordered as initially 'planned'. This can be avoided by export and re-import but in many cases its best to accept that ordering is just a convention for initial creation for programmer convenience and that alone.

Another popular topic here is column name naming conventions. That's a whole 'nother topic but I'll tip my toe in by saying don't abbreviate unless forced!

like image 142
Michael Durrant Avatar answered Sep 21 '22 15:09

Michael Durrant


Not only is there no real convention, but it's best not to think about one. Sometimes insert queries, for example, will not specify field names when it's assumed that you're going to update every field. But then you have to specify values in an assumed order. This is error-prone. So the best way to think about it is, "these columns are present in this table, in no particular order."

like image 25
dnuttle Avatar answered Sep 21 '22 15:09

dnuttle