SSMS 生成的命令(ansi nulls、quoted ident、ansi pad 等)的重要性是什么?

发布于 2024-11-01 10:21:50 字数 797 浏览 1 评论 0原文

这是从 SSMS“脚本表为”->“创建到”生成的脚本:

USE [DADatabaseMarch11]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[LoginName](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [name] [varchar](255) NOT NULL,
 CONSTRAINT [PK_LoginName] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY],
 CONSTRAINT [IX_LoginName] UNIQUE NONCLUSTERED 
(
    [name] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

我仅使用 SQL 2008 R2,不需要向后兼容性。

我的问题是:出于实际目的,上述内容中是否有可以省略的部分?

Here's the generated script from SSMS "Script Table as"->"Create To":

USE [DADatabaseMarch11]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[LoginName](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [name] [varchar](255) NOT NULL,
 CONSTRAINT [PK_LoginName] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY],
 CONSTRAINT [IX_LoginName] UNIQUE NONCLUSTERED 
(
    [name] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

I am using SQL 2008 R2 only without any need for backward compatability.

My QUESTION is: are there pieces of the above that, for practical purposes, can be left out?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失而复得 2024-11-08 10:21:50

ANSI_NULLS http://msdn.microsoft.com/en -us/library/ms188048.aspx

您没有对过滤索引、计算列等执行任何特殊操作,因此您可以删除它。

QUOTED_IDENTIFIER http://msdn.microsoft.com/en -us/library/ms174393.aspx

非常标准的名称,看不到任何引号,因此它在这里的效果为零。

ANSI_PADDING http://msdn.microsoft.com/en -us/library/ms187403.aspx

此设置应始终打开。仅当您出于某种原因将其设置为关闭时,才需要在此处使用它。即便如此,它也没有做太多事情,因为即使你在 LoginName.NAME 中保留尾随空格,

where name = 'abc'   => will match 'abc   '  (spaces stored)
len(name) = 3     => even if name is 'abc    '

因为在整个 SQL Server 中,它已经在使用尾随空格做一些有趣的事情了。

您需要的最低限度

CREATE TABLE dbo.LoginName(
    id int IDENTITY NOT NULL,
    name varchar(255) NOT NULL,
CONSTRAINT PK_LoginName PRIMARY KEY
(
    id
),
 CONSTRAINT IX_LoginName UNIQUE
(
    name
)
)
GO
  • 您通常只有一个名为“PRIMARY”的文件组,以便这些文件组可以进入
  • 索引选项是默认值,仅当它们对调整很重要时才包括或更改它们
  • 主键默认情况下总是聚集的,除非另一个已经
  • 是唯一键是非聚集的,因为主键已经聚集了
  • [] 是不必要的,因为没有使用特殊名称
  • ,我将 dbo. 留在那里,但说实话,它也可以在 99% 的情况下,
  • 不带说明符的 IDENTITY 是默认值 (1,1)

ANSI_NULLS http://msdn.microsoft.com/en-us/library/ms188048.aspx

You are not doing anything special with filtered indexes, computed columns etc, so you can drop it.

QUOTED_IDENTIFIER http://msdn.microsoft.com/en-us/library/ms174393.aspx

Very standard names with nary a quote in sight, so it has zero effect here.

ANSI_PADDING http://msdn.microsoft.com/en-us/library/ms187403.aspx

This setting should invariably be on. It is only needed here if you have for whatever reason you have set it to off. Even then, it does not do much, because even if you kept trailing spaces in LoginName.NAME,

where name = 'abc'   => will match 'abc   '  (spaces stored)
len(name) = 3     => even if name is 'abc    '

Because throughout SQL Server, it is already doing funny things with trailing spaces.

The bare minimum you need

CREATE TABLE dbo.LoginName(
    id int IDENTITY NOT NULL,
    name varchar(255) NOT NULL,
CONSTRAINT PK_LoginName PRIMARY KEY
(
    id
),
 CONSTRAINT IX_LoginName UNIQUE
(
    name
)
)
GO
  • You normally only have one filegroup, named "PRIMARY" so those can go
  • The index options are defaults, include or change them only if they matter for tuning
  • The primary key is by default always clustered unless another one already is
  • The unique key is non-clustered since the primary key is already clustered
  • []'s are not necessary since no special names are being used
  • I left dbo. in there, but to be honest, it can go too in 99% of cases
  • IDENTITY without specifiers is be default (1,1)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文