Alternative to column name "order" in MySQL

MysqlSql

Mysql Problem Overview


When I create a new table that need an ordering defined by the user, my first idea always go to a column name "order". Of course, this is NOT good since it's a reserved word.

Which name are you giving to that column in your database models ?

Mysql Solutions


Solution 1 - Mysql

I use "position" in place of "order"

Solution 2 - Mysql

I often use simple synonyms, "sort" for example.

Solution 3 - Mysql

Just add the tick mark ` around the names of your tables and columns, for example:

  CREATE TABLE `order`
  (
         `order#` char(4) NOT NULL,
         `ord_date` DATE,
         Primary Key (`order#`)
  )
  ENGINE=InnoDB;

This allows for special characters and keywords to be used, at least this works for the current version of MySql.

Solution 4 - Mysql

In ANSI/ISO SQL, double quotes delimit keywords when used as column names; string literals are delimited by single quotes:

select "from" = 'from' from foo

Microsoft SQL Server allows the use of square brackets in lieu of the double quotes as well:

select [from] = 'from' from foo

But either way, it makes a dreadful mess of your code (try reading the above to someone.)

If I need an column for ordering results, I generally call it something like 'sequence_number' or 'sort_sequence'.

Solution 5 - Mysql

SQL Server, at least, allows you to use keywords if enclosed in square brackets, although I agree it's not a great idea.

I believe the last time I did this, I used SortOrder for the name. However, I often use prefixes that reflect the table such as UsrSortOrder so that's not always an issue.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCyril N.View Question on Stackoverflow
Solution 1 - MysqlCIRCLEView Answer on Stackoverflow
Solution 2 - MysqleageranalystView Answer on Stackoverflow
Solution 3 - MysqlChrisView Answer on Stackoverflow
Solution 4 - MysqlNicholas CareyView Answer on Stackoverflow
Solution 5 - MysqlJonathan WoodView Answer on Stackoverflow