Showing posts with label Database Design. Show all posts
Showing posts with label Database Design. Show all posts
Wednesday, May 26, 2010
Controlling SQL Server 2005 behavior by setting database options regarding NULL values
To access SQL Server there are many Tools and interfaces available, these interfaces can have some default settings to control the database behavior from front end. Right now I am going to talk about most familiar database front end interface SQL Server Management Studio.
To open database Options,
Right Click your database >> Properties >> Options, you will get below screen shot.
ANSI NULL Default:
The default option ANSI NULL DEFAULT corresponds to two session settings ANSI_NULL_DFLT_ON and ANSI_NULL_DFLT_OFF. When ANSI null default database option is false, then the new columns created with the ALTER TABLE and CREATE TABLE statements are by default NOT NULL if Nullability for the column is not explicitly defined.
When this option is set to ON, columns comply with the ANSI SQL-92 rules for column nullability. That is if you don't specifically indicate whether a column in a table allows NULL values, NULLs are allowed. When this option is set to OFF, newly created columns do not allow NULLs if no nullability constrains is specified.
ANSI_NULLS:
Database option ANSI Nulls corresponds to the session settings SET ANSI_NULLS. When this option is set to true, all comparison to a null value evaluate to false. When it is set to false, comparison of non-Unicode values to a null evaluate to true if both values are NULL.
In addition if this option is set to true then your code must use the function IS NULL to determine whether a column has a NULL value. When this option is set to false, SQL Server allows=NULL as a synonym for IS NULL and <> NULL as a synonym for IS NOT NULL.
Below is the code snippet to demonstrate this behavior.
I have a user table in which Email column is having NULL values,
-- This query returns all the rows where email = null, so column to null comparison works
set
ANSI_NULLS
OFF
GO
select
*
from Users where email =
NULL
-- This query doesnt return any row, colum to NULL comparison doesnt work
set
ANSI_NULLS
ON
GO
select
*
from Users where email =
NULL
ANSI_PADDING:
When this option is set to ON, string being compared with each other are set to the same length before the comparison take place. When this option is OFF, no padding takes place.
ANSI_WARNING:
When this option is set to ON, errors or warnings are issued when conditions such as division by zero or arithmetic overflow occurs.
CONCAT_NULL_YEILDS_NULL:
When this option is set to ON, concatenating two strings results in a NULL string if either of the string is NULL. When this option is set to OFF, a NULL string treated as an empty (zero-length) string for the purpose of concatenation.
Thursday, May 13, 2010
Table Variable Vs Temp table in SQL Server
In many scenarios we need some temporary table for the processing of data. Now you have two option 1) Table Variable, 2) temp Table. Which one do you choose? Let's talk about differences between these two and make sure our decision to use one of them is best for our requirement.
| Table Variable | Temp Table |
| Performance differences | |
| Table variables don't participate in transactions, logging or locking. This means they're faster as they don't require the overhead, but conversely you don't get those features. | Temporary Tables are real tables so you can do things like CREATE Indexes, etc. If you have large amounts of data for which accessing by index will be faster than temporary tables are a good option |
| You can pass table variables back from functions, enabling you to encapsulate and reuse logic much easier (e.g. make a function to split a string into a table of values on some arbitrary delimiter). | You can create a temp table using SELECT INTO, which can be quicker to write (good for ad-hoc querying) and may allow you to deal with changing data types over time, since you don't need to define your temp table structure upfront. |
| A table variable can only have a primary index, | A temp table can have indexes |
| If speed is an issue Table variables can be faster | But if there are a lot of records, or the need to search the temp table of a clustered index, then a Temp Table would be better. |
| A table variables don't have column statistics, This means that the query optimizer doesn't know how many rows are in the table variable (it guesses 1), which can lead to highly non-optimal plans been generated if the table variable actually has a large number of rows | Whereas temp tables do have column statistics so the query optimizer can choose different plans for data involving temp tables |
| You cannot alter a Table variable with DDL statement (so you cannot create a non clustered index on a table variable). That makes every table variable a heap, or at best a table with a single, clustered index, and every table variable access a table scan (or clustered index scan) | Temp tables can be altered with DDL statements |
| User table variable if there less records for processing | Use temp table if you have huge record for processing |
| Syntactical difference | |
| -To create table variable declare @T table (firstColumn varchar(100)) -insert operation insert into @T select 'some value' -select statement select * from @T | -You can create temp table create table #T (firstColumn varchar(100)) -Insert Operation insert into #T select 'some value' -Select statement select * from #T |
So now I think you can make wise decision which one to use when. Have fun -J
Happy Coding!!!
Subscribe to:
Posts (Atom)