Introducing DotNetFunda.com on mobile http://m.dotnetfunda.com ! Be with DotNetFunda.com on the go !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 2871 |  Welcome, Guest!   Register  Login
Home > Articles > Sql Server > IDENTITY columns in SQL Server

IDENTITY columns in SQL Server

Article posted by Chikul on 12/23/2009 | Views: 6063 | Category: Sql Server | Level: Beginner red flag


The IDENTITY columns are auto incrementing columns provided by SQL Server.
There can only be one IDENTITY column per table.
SQL Server will take care of incrementing this column automatically.

Introduction

The IDENTITY columns are auto incrementing columns provided by SQL Server.
There can only be one IDENTITY column per table.
SQL Server will take care of incrementing this column automatically.

It has a name, initial seed and step.
When a row is inserted into a table the column will take the value of the current seed incremented by the step.

Disadvantage :

An Identity column is not guaranteed to be unique nor consecutive.
If any insert failure is there then also it can change the current seed value.
If we truncate a table (but not delete), It will update the current seed to the original seed value.
   

Identity Data types :

The identity must only contain integer values but the column can be of any numeric datatype (bigint, int, tinyint, numeric, decimal).
But ,We need to make sure identity column are not reaching the limit of their base data type.

An IDENTITY column of tinyint data type can go up to 255.
smallint can go up to 32767
int can go up to 2147483647 and
bigint can go up to 9223372036854775807.

Example :

If you created an IDENTITY column of smallint datatype, its values can go upto 32767.
If you try to insert anymore rows, you will get the following error:
Arithmetic overflow error converting IDENTITY to data type smallint.

So, We need to monitor these IDENTITY columns, to avoid getting into such problems. If you can see in advance, that an IDENTITY column is reaching its limit, then we could do something about it, before it reaches the limit.


Reset Identity column

DBCC CHECKIDENT('<tablename>', RESEED, 0)

Detecting Identity Columns And Their Properties


To find the last identity value allocated we can use Scope_identity()
Example :

INSERT INTO tbUserMst(UserName,Password) VALUES('Mr. John','YYYY')
GO
SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY]
GO
SELECT @@IDENTITY AS [@@IDENTITY]
GO

The existance of an identity column on a table can be checked as follows, Which will return 1 if an identity exists on the table.

SELECT OBJECTPROPERTY(OBJECT_ID('<tablename>'),'TableHasIdentity')

A column has the identity property or not can be checked as follows, Which will return 1 if the column has the identity property.
SELECT COLUMNPROPERTY(OBJECT_ID('<tablename>'),'<columnname>','IsIdentity')

A more useful way of obtaining this information is by using the catalog view sys.identity_columns which returns a row for each column in the database with an identity property.

SELECT TableName = OBJECT_NAME(OBJECT_ID) , ColumnName = name , OriginalSeed = seed_value , Step = increment_value , LastValue = last_value , IsNotForReplication = is_not_for_replication FROM sys.identity_columns

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Experience:1 year(s)
Home page:http://knowledgeparlour.blogspot.com/
Member since:Friday, November 20, 2009
Level:Bronze
Status: [Member]
Biography:
>> Write Response - Respond to this post and get points
Related Posts

This is part 5 of the series of article on SSIS. In this article I have described how to build and execute a package in SSIS.

This is part 8 of the series of article on SSIS

In this article we will first try to understand what is a SQL plan, how is it created and then we will move towards understanding how to read the SQL plan. As we read the SQL plan we will try to understand different operators like table scan, index seek scan, clustered scan, RID lookup etc. We will also look in to the best practices associated with clustered and non-clustered indexes and how they function internally. We will practically see how indexed views increase performance and in what scenarios we should use the same.

This article enable you to learn how to use Column Index Number instead of Column Name in the order by clause and where it should be used.

According to Microsoft best practice, User defined stored procedures should not be started with 'SP_'. The reasone is...

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/21/2012 7:34:09 AM