Identity column
   HOME

TheInfoList



OR:

An identity column is a
column A column or pillar in architecture and structural engineering is a structural element that transmits, through compression, the weight of the structure above to other structural elements below. In other words, a column is a compression member ...
(also known as a field) in a
database In computing, a database is an organized collection of data or a type of data store based on the use of a database management system (DBMS), the software that interacts with end users, applications, and the database itself to capture and a ...
table Table may refer to: * Table (database), how the table data arrangement is used within the databases * Table (furniture), a piece of furniture with a flat surface and one or more legs * Table (information), a data arrangement with rows and column ...
that is made up of values generated by the database. This is much like an AutoNumber field in
Microsoft Access Microsoft Access is a database management system (DBMS) from Microsoft that combines the relational database, relational Access Database Engine (ACE) with a graphical user interface and software-development tools. It is a member of the Microsof ...
or a
sequence In mathematics, a sequence is an enumerated collection of objects in which repetitions are allowed and order matters. Like a set, it contains members (also called ''elements'', or ''terms''). The number of elements (possibly infinite) is cal ...
in
Oracle An oracle is a person or thing considered to provide insight, wise counsel or prophetic predictions, most notably including precognition of the future, inspired by deities. If done through occultic means, it is a form of divination. Descript ...
. Because the concept is so important in
database In computing, a database is an organized collection of data or a type of data store based on the use of a database management system (DBMS), the software that interacts with end users, applications, and the database itself to capture and a ...
science Science is a systematic discipline that builds and organises knowledge in the form of testable hypotheses and predictions about the universe. Modern science is typically divided into twoor threemajor branches: the natural sciences, which stu ...
, many
RDBMS A relational database (RDB) is a database based on the relational model of data, as proposed by E. F. Codd in 1970. A Relational Database Management System (RDBMS) is a type of database management system that stores data in a structured forma ...
systems implement some type of generated key, although each has its own terminology. Today a popular technique for generating identity is to generate a random
UUID A Universally Unique Identifier (UUID) is a 128-bit nominal number, label used to uniquely identify objects in computer systems. The term Globally Unique Identifier (GUID) is also used, mostly in Microsoft systems. When generated according to the ...
. An identity column differs from a
primary key In the relational model of databases, a primary key is a designated attribute (column) that can reliably identify and distinguish between each individual record in a table. The database creator can choose an existing unique attribute or combinati ...
in that its values are managed by the server and usually cannot be modified. In many cases an identity column is used as a primary key; however, this is not always the case. It is a common misconception that an identity column will enforce uniqueness; however, this is not the case. If you want to enforce uniqueness on the column you must include the appropriate constraint too. In
Microsoft SQL Server Microsoft SQL Server is a proprietary relational database management system developed by Microsoft using Structured Query Language (SQL, often pronounced "sequel"). As a database server, it is a software product with the primary function of ...
you have options for both the seed (starting value) and the increment. By default the seed and increment are both 1.


Code samples

Create Table Contacts ( FirstName varChar(30), LastName varChar(30), Phone varChar(16), ContactID int identity(1, 1) ) or Create Table Contacts ( FirstName varChar(30), LastName varChar(30), Phone varChar(16) ) GO Alter Table Contacts Add ContactID int identity(1, 1) In
PostgreSQL PostgreSQL ( ) also known as Postgres, is a free and open-source software, free and open-source relational database management system (RDBMS) emphasizing extensibility and SQL compliance. PostgreSQL features transaction processing, transactions ...
CREATE TABLE contact ( contact_id bigint GENERATED ALWAYS AS IDENTITY, first_name varchar, last_name varchar, phone varchar );


Related functions

It is often useful or necessary to know what identity value was generated by an INSERT command.
Microsoft SQL Server Microsoft SQL Server is a proprietary relational database management system developed by Microsoft using Structured Query Language (SQL, often pronounced "sequel"). As a database server, it is a software product with the primary function of ...
provides several functions to do this: @@IDENTITY provides the last value generated on the current connection in the current scope, while IDENT_CURRENT(''tablename'') provides the last value generated, regardless of the connection or scope it was created on. Example: Insert Into Contacts ( FirstName, LastName ) Values ( 'Test', 'User' ) -- Select @@Identity -- OR -- Declare @ID int Select @ID = @@Identity Update Contacts Set Phone = 'XXX-YYY-ZZZZ' Where ContactID = @ID


See also

*
Surrogate key A surrogate key (or synthetic key, pseudokey, entity identifier, factless key, or technical key) in a database is a unique identifier for either an ''entity'' in the modeled world or an ''object'' in the database. The surrogate key is ''not'' deri ...
* Unique key


External links


MSDN Article "Managing Identity"
{{dead link, date=June 2023 Databases Articles with example SQL code