在 MySQL 中替代列名“ order”

当我创建一个需要用户定义排序的新表时,我的第一个想法总是使用列名“ order”。当然,这是不好的,因为它是一个保留字。

在数据库模型中为该列命名哪个名称?

43680 次浏览

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

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.

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'.

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.

I use "position" in place of "order"