Skip to main content

Identity Column - SQL Server

 Identity column in SQL server

IDENTITY property used to create an identity column in a table. It will be used with the CREATE TABLE and ALTER TABLE Transact-SQL statements. This column should be integer based column which will be handled by SQL server.

Syntax

            IDENTITY [ (seed , increment) ]

seed
Is the value that is used for the very first row loaded into the table.

increment
Is the incremental value that is added to the identity value of the previous row that was loaded.

You must specify both the seed and increment or neither. If neither is specified, the default is (1,1).

Example

            CREATE TABLE Student

( 

 RollNumber int IDENTITY(1,1), 

 name varchar (50), 

 ..); 

You must specify both the seed and increment or neither. If neither is specified, the default is (1,1).

Remarks

Identity columns can be used for generating key values. The identity property on a column guarantees the following:

·       Each new value is generated based on the current seed & increment.

·       Each new value for a particular transaction is different from other concurrent transactions on the table.

The identity property on a column does not guarantee the following:

·       Uniqueness of the value - Uniqueness must be enforced by using a PRIMARY KEY or UNIQUE constraint or UNIQUE index.

 ·       Consecutive values within a transaction - A transaction inserting multiple rows is not guaranteed to get consecutive values for the rows because other concurrent inserts might occur on the table. If values must be consecutive then the transaction should use an exclusive lock on the table or use the SERIALIZABLE isolation level.

·       Consecutive values after server restart or other failures - SQL Server might cache identity values for performance reasons and some of the assigned values can be lost during a database failure or server restart. This can result in gaps in the identity value upon insert. If gaps are not acceptable then the application should use its own mechanism to generate key values. Using a sequence generator with the NOCACHE option can limit the gaps to transactions that are never committed.

·       Reuse of values - For a given identity property with specific seed/increment, the identity values are not reused by the engine. If a particular insert statement fails or if the insert statement is rolled back then the consumed identity values are lost and will not be generated again. This can result in gaps when the subsequent identity values are generated.

Reference :

              https://learn.microsoft.com/en-us/sql/t-sql/statements/create-table-transact-sql-identity-property?view=sql-server-ver16

             

Comments

Popular posts from this blog

SFTP Integration in SSIS package Using WinSCP DLL

  In this blog, I am planning to write about SSIS SFTP Task details and reference sites, upload and download the files from SFTP server using SSIS package with help of the winscp library. Steps to configure the winscp DLL and Download Files 1.        Please download the DLL and required files from below path https://winscp.net/eng/downloads.php#additional 2.        Once Downloaded, use the below comments to add in Local GAC "Path to the gacutil exe \gacutil.exe" /i WinSCPnet.dll 3.        Please use the below URL as guide to implement the SSIS script task to download the files from SFTP using winscp as library https://winscp.net/eng/docs/library_ssis 4.        I am just briefing the steps based on the above URL what we can try in SSIS package. 5.        Please create the below variables in SSIS package which we need ...

Geography Data Type – SQL Server

     Geography data type in SQL Server is a useful tool for storing and manipulating geographic data. It provides a set of functions and tools for working with geographical data, such as points, lines, and polygons. In this blog, we will explore the Geography data type in SQL Server with examples. Introduction to Geography data type Geography data type is a built-in data type in SQL Server that is designed to support the storage, manipulation, and analysis of geographic data. It is based on the Open Geospatial Consortium (OGC) Simple Feature Access specification, which provides a standard way to represent geographic data in a database. The Geography data type in SQL Server stores data in a geographic coordinate system, allowing you to represent points on the surface of the earth using longitude and latitude coordinates. You can also represent lines and polygons by defining a series of points that define the shape of the line or polygon. Creating a Geography data t...

XML data type Functions – SQL Server

 The XML data type in MSSQL Server is a powerful tool for handling and manipulating XML data within a relational database system. In addition to storing XML data as a column in a table, SQL Server provides a number of XML functions and methods that allow for easy parsing, querying, and transformation of XML data. In this blog post, we will cover all XML data type methods available in MSSQL Server. value() Method: The value() method is used to extract a single value from an XML instance. This method accepts an XQuery expression as a parameter, which is used to identify the value to be extracted. The syntax of the value() method is as follows: xml . value ( XQuery expression , Data type ) For example, the following query extracts the value of the 'name' element from an XML column called 'MyXMLColumn': SELECT MyXMLColumn . value ( '(/Root/Person/Name)[1]' , 'varchar(50)' ) AS Name FROM MyTable   query() Method: The query() method i...