从 DBMS SQL 创建表脚本包含 [ ]
如果有人使用 Microsoft 的 SQL Server Management Studio 右键单击数据库中的给定表并创建脚本表到查询窗口,则会在窗口中显示创建表代码。我注意到类似以下内容
CREATE TABLE [dbo].[Login](
[用户 ID] [int] NOT NULL,
[LoginName] nvarchar COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
等等
)
在其他 DBMS 中也是这样吗?
这是特定于所使用的 DBMS 的东西吗?
难道这只是一个奇特的景色吗?
为什么在列名和表名周围使用“[]”。简单的 TSQL 定义看起来像
CREATE TABLE 表名 (
Column#1 数据类型 NOT NULL,
Column#2 数据类型 NOT NULL UNIQUE,
-- 等等...
)
请不要介意我愚蠢的问题。
预先感谢,
巴拉吉小号
If some one does right click on a given Table in a Database using SQL Server management Studio from Microsoft and create script table to query window, it displays the create table code in the window. I notice something like the following
CREATE TABLE [dbo].[Login](
[UserId] [int] NOT NULL,
[LoginName] nvarchar COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
etc
)
Is this how it appears in other DBMS too ?
Is this something specific to DBMS used ?
Is it just a fancy view only ?
Why are "[ ]" used around column names and table names. Simple TSQL definition would look something like
CREATE TABLE table_name (
Column#1 Datatype NOT NULL,
Column#2 Datatype NOT NULL UNIQUE,
-- etc...
)
Please dont mind my silly questions.
Thanks in advance,
Balaji S
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这些是标识符引号。它们将内容标记为数据库标识符(列名、表名等),并允许使用空格、特殊字符和保留字作为标识符。通常这些都不会出现在标识符中,因此严格来说,ID 引号是不必要的。当文本由于特殊字符或保留字而无法解析时,需要使用 ID 引号。
对于自动化工具来说,始终使用 ID 引号比弄清楚何时可以不用它们更容易。
不同的数据库产品使用不同的字符来引用 ID。您经常会看到用于此目的的反引号 (`)。
Those are identifier quotes. They mark the contents as a database identifier (column name, table name, etc) and allow spaces, special characters, and reserved words to be used as identifiers. Usually none of those appear in identifiers so the ID quotes are, strictly speaking, unnecessary. When the text cannot be parsed because of special characters or reserved words, the ID quotes are required.
It's easier for automated tools to simply always use the ID quotes than figure out when you could get away without them.
Different database products use different characters for ID quotes. You often see the back-tick (`) used for this.
只是为了补充 Larry Lustig 的出色答案,SQL Server 的“创建脚本”函数会输出 SQL 代码,其首要任务是解析器友好,这就是为什么它总是使用方括号作为首选分隔标识符字符,而不是说双引号由 SQL-92 标准使用(SQL Server 代码可以通过使用 SET QUOTED_IDENTIFIER ON 来支持标准分隔标识符,但我不认为这可以指定为输出选项)。易于人眼观察(使用空白、换行符等)只是次要考虑因素。
Just to add to Larry Lustig's excellent answer, SQL Server's 'create script' function spits out SQL code whose first priority is to be parser-friendly and that's why it always uses its preferred delimited identifier characters being square brackets, rather than say the double quotes used by the SQL-92 standard (SQL Server code can support the Standard delimited identifier by the use of
SET QUOTED_IDENTIFIER ON
but I don't think this can be specified as an option for output). Being easy on the human eye (use of whitespace, line breaks, etc) is merely a secondary consideration.