MS-SQL 表设计器中方括号 [] 的含义?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9917196/
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
Meaning of square brackets [] in MS-SQL table designer?
提问by Bonk
I have a copy of an existing database with existing records. when i was playing around with the table designer and noticed some of the column names have [] around them. they all seem to be arbitrary typed (float, datetime, netext, nvarchar etc) and there is nothing in column properties that gets rid of the []. I tried to rename delete the [] but it reappaears as soon as I exit edit.
我有一个现有数据库的副本,其中包含现有记录。当我和表设计师一起玩时,注意到一些列名周围有 []。它们似乎都是任意类型的(float、datetime、netext、nvarchar 等),并且列属性中没有任何内容可以去掉 []。我试图重命名删除 [] 但它在我退出编辑后立即重新出现。
according to thispost, it is a keyword column for xml columns? but none of those columns are xml columns. Would someone kindly explain the purpose of this to a ms-sql newbie? thanks
根据这篇文章,它是xml列的关键字列?但这些列都不是 xml 列。有人会向ms-sql新手解释这个目的吗?谢谢
回答by Mark Byers
The square brackets []
are used to delimit identifiers. This is necessary if the column name is a reserved keyword or contains special characters such as a space or hyphen.
方括号[]
用于分隔标识符。如果列名称是保留关键字或包含特殊字符(如空格或连字符),则这是必需的。
Some users also like to use square brackets even when they are not necessary.
一些用户也喜欢使用方括号,即使它们不是必需的。
From MSDN:
从MSDN:
Delimited identifiers
Are enclosed in double quotation marks (") or brackets ([ ]). Identifiers that comply with the rules for the format of identifiers may or may not be delimited.
SELECT * FROM [TableX] --Delimiter is optional. WHERE [KeyCol] = 124 --Delimiter is optional.
Identifiers that do not comply with all of the rules for identifiers must be delimited in a Transact-SQL statement.
SELECT * FROM [My Table] --Identifier contains a space and uses a reserved keyword. WHERE [order] = 10 --Identifier is a reserved keyword.
分隔标识符
用双引号 (") 或括号 ([ ]) 括起来。符合标识符格式规则的标识符可能会或可能不会被分隔。
SELECT * FROM [TableX] --Delimiter is optional. WHERE [KeyCol] = 124 --Delimiter is optional.
不符合所有标识符规则的标识符必须在 Transact-SQL 语句中定界。
SELECT * FROM [My Table] --Identifier contains a space and uses a reserved keyword. WHERE [order] = 10 --Identifier is a reserved keyword.
回答by Michael Capobianco
Square brackets may be placed around objects (e.g. views, databases, columns etc)
方括号可以放在对象周围(例如视图、数据库、列等)
Their primary purpose is, as Mark mentioned, to encapsulate the objects so that special characters such as a space or period do not interfere with your syntax.
正如 Mark 所提到的,它们的主要目的是封装对象,以便特殊字符(例如空格或句点)不会干扰您的语法。
Many applications by default use the bracketed notation, to further reduce risk of syntax errors or anything of that sort.
默认情况下,许多应用程序使用括号表示法,以进一步降低语法错误或任何此类错误的风险。
It's typically good practice to not include any spaces
不包含任何空格通常是一个好习惯
Database_Name < GOOD PRACTICE
Database Name < GENERALLY TRY TO AVOID
In any case, if the latter is used, you may use square brackets i.e.
在任何情况下,如果使用后者,您可以使用方括号即
select * from [Database Name]
回答by mson
the brackets are special characters in sql server that are used to explicitly delimit information.
括号是 sql server 中用于显式分隔信息的特殊字符。
they can be used in xml as per the article, they can also be used to specify meta names (column, table, etc.) that have spaces or other key words.
它们可以按照文章在 xml 中使用,也可以用于指定带有空格或其他关键字的元名称(列、表等)。
declare my column nvarchar(50)
would be invalid syntax, but you could do
将是无效的语法,但你可以这样做
declare [my column] nvarchar(50)
just think of them as explicit delimiters.
只需将它们视为显式分隔符即可。