创建新数据库时的最佳 ANSI 选项 [英] Best ANSI options when creating a new database

查看:25
本文介绍了创建新数据库时的最佳 ANSI 选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我们的生产环境在 SQL Server 2012 上创建一个新数据库.当我使用 SQL Server Management Studio 中的新建数据库..."选项并生成输出时:

I'm in the process of creating a new database onto SQL Server 2012 for our production environment. When I use the "New Database..." option from SQL Server Management Studio and generate the output I get:

CREATE DATABASE [AAA]
 CONTAINMENT = NONE
 ON  PRIMARY 
( NAME = N'AAA', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQL2014\MSSQL\DATA\AAA.mdf' , SIZE = 5120KB , FILEGROWTH = 1024KB )
 LOG ON 
( NAME = N'AAA_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQL2014\MSSQL\DATA\AAA_log.ldf' , SIZE = 1024KB , FILEGROWTH = 10%)
GO
ALTER DATABASE [AAA] SET COMPATIBILITY_LEVEL = 120
GO
ALTER DATABASE [AAA] SET ANSI_NULL_DEFAULT OFF 
GO
ALTER DATABASE [AAA] SET ANSI_NULLS OFF 
GO
ALTER DATABASE [AAA] SET ANSI_PADDING OFF 
GO
ALTER DATABASE [AAA] SET ANSI_WARNINGS OFF 
GO
ALTER DATABASE [AAA] SET ARITHABORT OFF 
GO
ALTER DATABASE [AAA] SET AUTO_CLOSE OFF 
GO
ALTER DATABASE [AAA] SET AUTO_SHRINK OFF 
GO
ALTER DATABASE [AAA] SET AUTO_CREATE_STATISTICS ON(INCREMENTAL = OFF)
GO
ALTER DATABASE [AAA] SET AUTO_UPDATE_STATISTICS ON 
GO
ALTER DATABASE [AAA] SET CURSOR_CLOSE_ON_COMMIT OFF 
GO
ALTER DATABASE [AAA] SET CURSOR_DEFAULT  GLOBAL 
GO
ALTER DATABASE [AAA] SET CONCAT_NULL_YIELDS_NULL OFF 
GO
ALTER DATABASE [AAA] SET NUMERIC_ROUNDABORT OFF 
GO
ALTER DATABASE [AAA] SET QUOTED_IDENTIFIER OFF 
GO
ALTER DATABASE [AAA] SET RECURSIVE_TRIGGERS OFF 
GO
ALTER DATABASE [AAA] SET  DISABLE_BROKER 
GO
ALTER DATABASE [AAA] SET AUTO_UPDATE_STATISTICS_ASYNC OFF 
GO
ALTER DATABASE [AAA] SET DATE_CORRELATION_OPTIMIZATION OFF 
GO
ALTER DATABASE [AAA] SET PARAMETERIZATION SIMPLE 
GO
ALTER DATABASE [AAA] SET READ_COMMITTED_SNAPSHOT OFF 
GO
ALTER DATABASE [AAA] SET  READ_WRITE 
GO
ALTER DATABASE [AAA] SET RECOVERY SIMPLE 
GO
ALTER DATABASE [AAA] SET  MULTI_USER 
GO
ALTER DATABASE [AAA] SET PAGE_VERIFY CHECKSUM  
GO
ALTER DATABASE [AAA] SET TARGET_RECOVERY_TIME = 0 SECONDS 
GO
ALTER DATABASE [AAA] SET DELAYED_DURABILITY = DISABLED 
GO
USE [AAA]
GO
IF NOT EXISTS (SELECT name FROM sys.filegroups WHERE is_default=1 AND name = N'PRIMARY') ALTER DATABASE [AAA] MODIFY FILEGROUP [PRIMARY] DEFAULT
GO

为什么这么多 ANSI 选项默认为 OFF?服务器实例是由 RackSpace 代表我们设置的.可能是因为他们在实例中设置了一些默认值?

Why are so many ANSI options defaulting to OFF? The server instance was set up by RackSpace on our behalf. Could it be because of some defaults they set up in the instance?

谢谢,克里斯

推荐答案

这些选项默认为 OFF,因为很可能这个数据库是在没有触及任何默认设置的情况下创建和编写脚本的.创建数据库时,它实际上是从 model 系统数据库克隆的,并且在全新安装的 SQL Server 上,数据库上的 ANSI 设置将 OFF,即使尽管其中一些设置(例如 ANSI_NULLS)是您绝对不想为任何现代数据库应用程序设置 OFF 的选项.事实上,特别是在 ANSI_NULLS 的情况下,文档指定完全关闭它的能力是 已弃用,尽管可能还需要几年时间才能真正成为现实.

The options are defaulting to OFF because, in all likelihood, this database was created and scripted without touching any of the defaults. When a database is created, it is essentially cloned from the model system database, and on a brand new install of SQL Server the ANSI settings on the database will be OFF, even though some of these settings (like ANSI_NULLS) are options you would really never want to be OFF for any modern database application. In fact, in the case of ANSI_NULLS in particular, the documentation specifies that the ability to turn it off at all is deprecated, although it will likely still be a few years before that's really the case.

这就是问题所在:为了旧应用程序的利益,这些设置仍然保持 OFF善良(和重大变化).如果会话没有为它们指定值,则应用数据库设置.

And therein lies the rub: these settings are still kept OFF for the benefit of old applications, who had to turn these options ON way back when to benefit from their goodness (and breaking changes). If the session specifies no values for them, the database settings are applied.

但大多数应用程序确实在会话中指定这些设置,如果不是显式地,则通过它们的数据访问库隐式地指定.根据 SET ANSI_DEFAULTS 上的文档,它会切换一系列设置一下子:

But most applications do specify these settings in a session, if not explicitly, then implicitly through their data access library. Per the documentation on SET ANSI_DEFAULTS, which toggles a bunch of settings at once:

SQL Server Native Client ODBC 驱动程序和 SQL Server Native ClientOLE DB Provider for SQL Server 自动将 ANSI_DEFAULTS 设置为 ON连接时.驱动程序和提供程序然后设置CURSOR_CLOSE_ON_COMMITIMPLICIT_TRANSACTIONSOFF.OFFSET CURSOR_CLOSE_ON_COMMITSET IMPLICIT_TRANSACTIONS 的设置可以在 ODBC 数据源中配置,在 ODBC 连接属性中,或在应用程序中设置的 OLE DB 连接属性中在连接到 SQL Server 之前.SET ANSI_DEFAULTS 的默认值是OFF 用于来自 DB-Library 应用程序的连接.

The SQL Server Native Client ODBC driver and SQL Server Native Client OLE DB Provider for SQL Server automatically set ANSI_DEFAULTS to ON when connecting. The driver and Provider then set CURSOR_CLOSE_ON_COMMIT and IMPLICIT_TRANSACTIONS to OFF. The OFF settings for SET CURSOR_CLOSE_ON_COMMIT and SET IMPLICIT_TRANSACTIONS can be configured in ODBC data sources, in ODBC connection attributes, or in OLE DB connection properties that are set in the application before connecting to SQL Server. The default for SET ANSI_DEFAULTS is OFF for connections from DB-Library applications.

DB-Library 是一个旧的访问库,但仍然被一些古老的应用程序使用,并且可以选择作为 FreeTDS 之类的支持源,所以你仍然经常会遇到故意或意外使用数据库设置的应用程序,但这种情况越来越少.

DB-Library is an old access library that is nevertheless still used by some ancient applications and optionally as a backing source for things like FreeTDS, so every so often you can still run into an application that deliberately or accidentally uses the database settings, but this is increasingly rare.

至于这些选项的最佳值,这完全取决于您的用例.如果您必须支持需要旧行为的旧应用程序,您可能别无选择,不得不将数据库设置保持在 OFF.如果您的应用程序通过旧库进行连接,但确实需要现代 SQL 语义,您可能希望将它们ON.对于所有其他应用程序,这些选项可能已经在每个会话的基础上由应用程序本身设置为它们的(不)正确值,无论如何您配置的内容都无关紧要.

As to the best value for these options, that depends entirely on your use case. If you must support old applications that expect old behavior, you may have no choice in having to leave the database settings on OFF. If you have an application that connects through an old library but really expects modern SQL semantics, you may want to turn them ON. For all other applications, these options are likely already set on a per-session basis to their (in)correct values by the application itself and what you configure won't matter anyway.

讨论每个选项以及何时将其打开 ONOFF 将超出合理答案的限制.查阅有关它们中的每一个的文档并制定您自己的最佳实践.您可以让诸如SET 计算列索引的选项要求 指导您,这需要一系列选项 ON 才能创建它们(它们通常被认为是一件好事).

A discussion on each individual option and when you'd want to turn it ON or OFF would exceed the limits of a reasonable answer. Consult the documentation on each of them and formulate your own best practices. You can let things like the SET option requirements for indexes on computed columns guide you, which require a bunch of options to be ON before you can even create them (and they're generally considered a nice thing to have).

这篇关于创建新数据库时的最佳 ANSI 选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆