Monday, August 27, 2012

TSQL - IDENTITY Property

IDENTITY is pretty much a very important property of a database. In post i will different places the use of (Creation, Seeding and Reseeding) IDENTITY property.
1) SQL Server allows only ONE identity column per table.
       Ex:
       Create Table #Identity_Post (RankNumber int identity(1,1),
                                                    First_Name varchar(40))
2) Identity(seed,increment)
      Ex: identity(1,1) means starting number is 1 and it will add 1 to next number
      identity(1000,5) means starting number is 1000 and it will add 5 to next number

3) SET IDENTITY_INSERT [Table_Name] [ON/OFF]
This property is one very usefull, When we want to insert a deleted rowid  (identity number) again in the table we can use this property.
     Ex:
     CREATE TABLE #TempTable (Rowid int IDENTITY(1,1), Name nvarchar(20))
     INSERT INTO #TempTable (Name) VALUES ('BiSpecialist')
     INSERT INTO #TempTable (Name) VALUES ('BwSpecialist')
     INSERT INTO #TempTable (Name) VALUES ('IsSpecialist')
     INSERT INTO #TempTable (Name) VALUES ('RsSpecialist')

     ----Delete a row so that we can create a gap in between the rows.
     DELETE from #TempTable WHERE Name = 'RsSpecialist'
     SELECT * FROM #TempTable
     ----Now it will through an error    
     INSERT INTO #TempTable (Rowid, Name) VALUES(4, 'AsSpecialist')
     ----SET IDENTITY_INSERT to ON.    
     SET IDENTITY_INSERT #TempTable ON
     ----Now its a identity magic    
     INSERT INTO #TempTable (Rowid, Name) VALUES(4, 'AsSpecialist')
     ----Then SET IDENTITY_INSERT  OFF, so that next time it won't allow the explicit insert.    
     SET IDENTITY_INSERT #TempTable OFF
     SELECT * FROM #TempTable
     DROP TABLE #TempTable
 
4) DBCC CHECKIDENT  (Database Console Command)
http://msdn.microsoft.com/en-us/library/aa258817(v=sql.80).aspx
DBCC CHECKIDENT
( table_name   [, { NORESEED | { RESEED [, new_reseed_value ] } } ]  )  [WITH NO_INFOMSGS]
 
Argument Description
table_name Is the name of the table for which to check the current identity value. The table specified must contain an identity column. Table names must comply with the rules for identifiers.
NORESEED Specifies that the current identity value should not be changed.
RESEED Specifies that the current identity value should be changed.
new_reseed_value Is the new value to use as the current value of the identity column.
WITH NO_INFOMSGS Suppresses all informational messages.

Parameter Specifications:
DBCC CHECKIDENT statement Identity correction(s) made
DBCC CHECKIDENT ('table_name', NORESEED) The current identity value is not reset. DBCC CHECKIDENT returns a report indicating the current identity value and what it should be.
DBCC CHECKIDENT ('table_name')
(or)  DBCC CHECKIDENT ('table_name', RESEED)
If the current identity value for a table is lower than the maximum identity value stored in the column, it is reset using the maximum value in the identity column.
DBCC CHECKIDENT ('table_name', RESEED,new_reseed_value) Current identity value is set to the new_reseed_value. If no rows have been inserted into the table since the table was created, or if all rows have been removed by using the TRUNCATE TABLE statement, the first row inserted after you run DBCC CHECKIDENT uses new_reseed_value as the identity. Otherwise, the next row inserted uses new_reseed_value + the current increment value.
If the table is not empty, setting the identity value to a number less than the maximum value in the identity column can result in one of the following conditions:   If a PRIMARY KEY or UNIQUE constraint exists on the identity column, error message 2627 will be generated on later insert operations into the table because the generated identity value will conflict with existing values. If a PRIMARY KEY or UNIQUE constraint does not exist, later insert operations will result in duplicate identity values.

Exceptions: The following table lists conditions when DBCC CHECKIDENT does not automatically reset the current identity value and provides methods for resetting the value.
Conditions Reset Methods
The current identity value is larger than the maximum value in the table. Execute DBCC CHECKIDENT (table_name, NORESEED) to determine the current maximum value in the column, and then specify that value as the new_reseed_value in a DBCC CHECKIDENT (table_name, RESEED, new_reseed_value) command.  (or)   Execute DBCC CHECKIDENT (table_name, RESEED, new_reseed_value) with new_reseed_value set to a very low value, and then run DBCC CHECKIDENT (table_name, RESEED) to correct the value.
All rows are deleted from the table. Execute DBCC CHECKIDENT (table_name, RESEED, new_reseed_value) with new_reseed_value set to the desired starting value.

No comments:

Post a Comment