MySQL 中列名“order”的替代方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4716201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Alternative to column name "order" in MySQL
提问by Cyril N.
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.
当我创建一个需要用户定义的排序的新表时,我的第一个想法总是转到列名“order”。当然,这是不好的,因为它是一个保留字。
Which name are you giving to that column in your db models ?
您在数据库模型中为该列指定哪个名称?
Thanks for your help.
谢谢你的帮助。
采纳答案by eageranalyst
I often use simple synonyms, "sort" for example.
我经常使用简单的同义词,例如“排序”。
回答by CIRCLE
I use "position" in place of "order"
我用“位置”代替“顺序”
回答by Chris
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.
这允许使用特殊字符和关键字,至少这适用于当前版本的 MySql。
回答by Jonathan Wood
SQL Server, at least, allows you to use keywords if enclosed in square brackets, although I agree it's not a great idea.
至少,SQL Server 允许您使用方括号括起来的关键字,尽管我同意这不是一个好主意。
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.
我相信上次我这样做时,我使用了 SortOrder 作为名称。但是,我经常使用反映表的前缀,例如 UsrSortOrder,因此这并不总是一个问题。
回答by Nicholas Carey
In ANSI/ISO SQL, double quotes delimit keywords when used as column names; string literals are delimited by single quotes:
在 ANSI/ISO SQL 中,双引号在用作列名时分隔关键字;字符串文字由单引号分隔:
select "from" = 'from' from foo
Microsoft SQL Server allows the use of square brackets in lieu of the double quotes as well:
Microsoft SQL Server 也允许使用方括号代替双引号:
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'.
如果我需要一列用于排序结果,我通常将其称为“sequence_number”或“sort_sequence”。