我的一个专栏叫。我不能改名字,因为不是我做的。 我是否允许做一些像SELECT from TableName或有一个特殊的语法,以避免SQL Server混淆?
当前回答
如果是在PostgreSQL中,在名字周围使用双引号,比如:
select "from" from "table";
注意:PostgreSQL内部自动将所有不带引号的命令和参数转换为小写。命令和标识符不区分大小写。sEleCt * from tAblE;解释为select * from table;。然而,双引号内的参数是原样使用的,因此是区分大小写的:select * from "table";and select * from "Table";从两个不同的表获取结果。
其他回答
一些可靠的答案——但投票最多的答案是狭隘的,只涉及SQL Server。总而言之:
If you have source control, the best solution is to stick to the rules, and avoid using reserved words. This list has been around for ages, and covers most of the peculiarities. One tip is that reserved words are rarely plural—so you're usually safe using plural names. Exceptions are DIAGNOSTICS, SCHEMAS, OCTETS, OFFSETS, OPTIONS, VALUES, PARAMETERS, PRIVILEGES and also verb-like words that also appear plural: OVERLAPS, READS, RETURNS, TRANSFORMS. Many of us don't have the luxury of changing the field names. There, you'll need to know the details of the RDBM you're accessing: For SQL Server use [square_braces] around the name. This works in an ODBC connection too. For MySQL use `back_ticks`. Postgres, Oracle and several other RDBMs will apparently allow "double_quotes" to be used.
在表名上加上冒犯性的单词也可以。
将列名像这样用括号括起来,from变成[from]。
select [from] from table;
也可以使用以下命令(在查询多个表时很有用):
select table.[from] from table;
简单的解决方案
假设列名是from;因此,查询中的列名可以通过表别名引用
Select * from user u where u.from="US"
你的问题似乎已经回答得很好了,但我想再补充一点。
那些设计数据库的人应该很清楚保留的关键字,并避免使用它们。如果你发现有人在使用它,告诉他们(以礼貌的方式)。这里的关键字是保留字。
更多信息:
不应使用保留关键字 作为对象名。数据库升级 从早期版本的SQL Server 可能包含标识符,包括 话不保留在前面 版本,但那是保留的话 SQL Server的当前版本。 可以使用引用对象 分隔标识符直到名称 是可以改变的。” http://msdn.microsoft.com/en-us/library/ms176027.aspx
and
"如果你的数据库确实包含名字 匹配保留关键字,您必须 使用分隔标识符时 引用这些对象。更多的 有关信息,请参见标识符(DMX)。 http://msdn.microsoft.com/en-us/library/ms132178.aspx
嗨,我在完全符合ANSI标准的Teradata系统上工作。使用双引号“”来命名这样的列。
例如,type是一个SQL保留关键字,当在引号中使用时,type被视为用户指定的名称。
参见下面的代码示例:
CREATE TABLE alpha1
AS
(
SEL
product1
type_of_product AS "type"
FROM beta1
) WITH DATA
PRIMARY INDEX (product1)
--type is a SQL reserved keyword
TYPE
--see? now to retrieve the column you would use:
SEL "type" FROM alpha1