SQLcommitted

Committed with SQL

ANSI_NULL_DEFAULT Vs ANSI_NULL_DFLT_ON

ANSI_NULL_DEFAULT:

This option is set at database level.

When ANSI_NULL_DEFAULT option is ON, columns are comply with SQL-92 rules for column nullability. If this option is set to ON, if we don’t specify a column with allow NULL explicitly, It will allow NULL. If this option is set to off and if we are defining any new column without specifying nullability constraint then it will not allow NULL for the new column.

You can verify this database option value from is_ansi_null_default_on column of catalog view sys.databases.

ANSI_NULL_DFLT_ON:

This option is applies to set the environment of a session. It overrides the option ANSI_NULL_DEFAULT only if the ANSI_NULL_DEFAULT is set to OFF.

While creating or modifying a table (to add new Columns) if we are not explicitly specifying the nullability constraint, it will allow Null value for the new column if ANSI_NULL_DFLT_ON is set to ON.

Like ANSI_NULL_DFLT_ON we have ANSI_NULL_DFLT_OFF option too.

Note:

- Both ANSI_NULL_DFLT_ON and ANSI_NULL_DFLT_OFF can’t be set to ON at the same time but both options can be set to OFF at the same time.

- The SQL Server Native Client ODBC driver and SQL Server Native Client OLE DB Provider for SQL Server   automatically ANSI_NULL_DFLT_ON to ON when connecting.

- When ANSI_DEFAULTS is ON, ANSI_NULL_DFLT_ON is also ON.

- ANSI_NULL_DFLT_ON is set at run time not at parse time.

- This option doesn’t apply to tables created using Select Into statement.

Note: These setting are only affected when the nullabilty of columns are not specified explicity in Create or Alter table statement .

Let’s see how ANSI_NULL_DFLT_ON and ANSI_NULL_DEFAULT option works together

Example 1: When ANSI_NULL_DEFAULT is OFF

Alter Database SQLCommitted  SET ANSI_NULL_DEFAULT OFF;

GO

Create Table Table_1 (Column_1 Int)

GO

Here in Example 1, Database option ANSI_NULL_DEFAULT is set to OFF and also explicit nullability constraint is not specified. In this case it shouldn’t allow NULL value for column Column_1.

Let’s try to insert NULL into table Table_1

INSERT INTO Table_1 (Column_1) VALUES (NULL);

GO

Msg 515, Level 16, State 2, Line 1

Cannot insert the value NULL into column ‘Column_1′, table ‘SQLCommitted.dbo.Table_1′; column does not allow nulls. INSERT fails.

The statement has been terminated.

Example 2: When ANSI_NULL_DEFAULT is OFF and ANSI_NULL_DFLT_ON is ON.

Alter Database SQLCommitted  SET ANSI_NULL_DEFAULT OFF;

GO


SET ANSI_NULL_DFLT_ON ON;

GO


Create Table Table_2 (Column_2 Int)

GO

Here in example 2, the Database option ANSI_NULL_DEFAULT is off but ANSI_NULL_DFLT_ON is set to ON. In this case the database option will override by ANSI_NULL_DFLT_ON setting which will allow accepting NULL value.

Let’s try to insert NULL into table Table_2

INSERT INTO Table_2 (Column_2) VALUES (NULL);

GO

(1 row(s) affected)

Example 3: When ANSI_NULL_DEFAULT is OFF and ANSI_NULL_DFLT_ON is also OFF.

Alter Database SQLCommitted  SET ANSI_NULL_DEFAULT OFF;

GO


SET ANSI_NULL_DFLT_ON OFF;

GO


Create Table Table_3 (Column_3 Int)

GO


 

Here in example 3 both ANSI_NULL_DEFAULT and ANSI_NULL_DFLT_ON are OFF. Now if you try to insert NULL value it will throw error.

INSERT INTO Table_3 (Column_3) VALUES (NULL);

GO

Msg 515, Level 16, State 2, Line 2

Cannot insert the value NULL into column ‘Column_3′, table ‘SQLCommitted.dbo.Table_3′; column does not allow nulls. INSERT fails.

The statement has been terminated.

In rest of the examples we’ll see there will be no impact of ANSI_NULL_DFLT_ON and ANSI_NULL_DFLT_OFF setting if ANSI_NULL_DEFAULT is set to ON.

Example 4: When ANSI_NULL_DEFAULT is ON and ANSI_NULL_DFLT_ON is ON

SET ANSI_NULL_DFLT_ON ON;

GO

CREATE TABLE Table_4 (Column_4 TINYINT);

GO

INSERT INTO Table_4 (Column_4) VALUES (NULL);

GO

(1 row(s) affected)

In the above example 4, Both ANSI_NULL_DEFAULT and ANSI_NULL_DFLT_ON set to ON, in result it allow NULL value for Column_4 of Table_4.

Example 5: When ANSI_NULL_DEFAULT is ON and ANSI_NULL_DFLT_ON is OFF

Here in the below example we’ll see even though the ANSI_NULL_DFLT_ON value is set to OFF it will allow NULL because the database option ANSI_NULL_DEFAULT is set to ON. So it doesn’t matter whether you set ANSI_NULL_DFLT_ON to ON or OFF when ANSI_NULL_DEFAULT is set to ON.

SET ANSI_NULL_DFLT_ON OFF;

GO

CREATE TABLE Table_5 (Column_5 TINYINT);

GO

INSERT INTO Table_5 (Column_5) VALUES (NULL);

GO


(1 row(s) affected)

Tags: ANSI_NULL_DEFAULT,ANSI_NULL_DFLT_ON,NULL,environment,session,ANSI_NULL_DFLT_OFF,options,ANSI_DEFAULTS

Reference : MSDN

About these ads

May 29, 2012 Posted by | SQL Server Database properties | , , | 2 Comments

SQL Server 2008 R2 Database properties – Option Page – Part-1

SQL Server 2008 R2  Database properties – Option Page – Part-1

Not only DBA , I believe anybody working on SQL Server database whether as a SQL Developer or BI Developer all would have seen the below screen.

 

 

 

 

Using this screen we can set different SQL Server database settings.

In this page there are 4 sections i.e.

1-      Collation

2-      Recovery Model

3-      Compatibility Level

4-      Other options

 

Let’s try to explore different option available in Other Options section.

 

The Other Options section grouped into 6 categories as :

1-      Automatic

2-      Cursor

3-      Miscellaneous

4-      Recovery

5-      Service Broker

6-      State

 

In Part-1 we’ll cover Automatic category also called Auto options

These are the options which SQL Server might take it automatically. All attributes under this sections takes Boolean value only ( Either TRUE  or FALSE)

 

AUTO CLOSE :

When this option is set to TRUE, the database is closed and shut down cleanly when the last user of the database exits. In result it will free all resources  used by database.

It will reopen again once user tries to use the database. This options mostly used for personal SQL SERVER database as it allow to manage database file as normal files.

If the database is used by an application which connect and disconnect to SQL SERVER repeatedly  then it is advisable not to set this option TRUE to avoid over head of closing and reopening the database between each connection.

 

AUTO CREATE STATISTICS:

Statistics is used by query optimizer to determine how to evaluate a query.

Statistics is a different topic in SQL SERVER , I’ll be writing on Statistics for you all very soon.

By default this option is set to TRUE. If It is TRUE,  SQL SERVER automatically creates statistics on columns referenced in a “WHERE” clause or “ON” clause .

It creates statistics on column when any missing statistics needed by the query optimizer.

Adding statistics helps to improve query performance ( How it improve performance?, I’ll be writing on it very soon).

 

AUTO SHRINK:

If this option is TRUE then both Data files and  Log files can be shrunk by SQL Server. File shrinking happened periodically.

To shrink a log file, you have to take the backup the log file or the recovery model set to SIMPLE ( I’ll be writing on Recovery model very soon).

It is not recommended to use this option.

 

Now a Question for you ,  When to use AUTO_SHRINK option?

 

AUTO UPDATE STATISTICS:

By Default this option is set to TRUE.  If this option is on existing statistics are updated if the table data has changed.

SQL SERVER maintains a counter of the modification made to a table and it uses this counter to determine when the statistics are outdated.

It updates the statistics when any out of date statistics needed by the query optimizer during query optimization.

 

AUTO UPDATE STATISTICS ASYNCHRONOUSLY:

If It is set to TRUE, It will not wait for the query which initiate to update the out of date statistics before compiling the query.

Subsequent queries will use the updated statistics when they are available.

If it is set to false, The query which initiate to update the out of date statistics will wait until the statistics is updated. Once the statistics is updated the query will use the updated statistic to prepare the query plan.

 

 

 

 

 

 

 

January 5, 2012 Posted by | SQL Server Database properties | | Leave a Comment

   

Follow

Get every new post delivered to your Inbox.

Join 60 other followers

%d bloggers like this: