Identity column

Identity column

An Identity column is a column ( also known as a field ) in a database table that (1) uniquely identifies every row in the table, and (2) is made up of values generated by the database. This is much like an AutoNumber field in Microsoft Access or a sequence in Oracle. Because the concept is so important in database science, all RDBMS systems implement some type of generated key, although each has its own terminology.

An identity column differs from a primary key in that its values are managed by the server and ( except in rare cases ) can't be modified. In many cases an identity column is used as a primary key, however this is not always the case.

Two types of identity columns are available in SQL Server: incremental ( where the user can set a seed and an increment ) and random ( where the server chooses a random numeric value and ensures that it hasn't already been used ).

Code Samples

Create Table Contacts ( FirstName varChar(30), LastName varChar(30), Phone varChar(16), ContactID identity(1, 1))

or

Create Table Contacts ( FirstName varChar(30), LastName varChar(30), Phone varChar(16))GOAlter Table Contacts Add ContactID identity(1, 1)

Related Functions

It is often useful or necessary to know what identity value was generated by an INSERT command. SQL Server provides several functions to do this: @@Identity provides the last value generated on the current connection, while Scope_Identity("tablename") provides the last value generated, regardless of the connection 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

External links

* [http://msdn.microsoft.com/library/default.asp?url=/library/en-us/replsql/repldata_078z.asp MSDN Article "Managing Identity"]


Wikimedia Foundation. 2010.

Игры ⚽ Нужна курсовая?

Look at other dictionaries:

  • Identity — may refer to:Philosophy* Identity (philosophy), the sameness of two things * Identity theory of mind, in the philosophy of mind, holds that the mind is identical to the brain * Personal identity (philosophy) * Identity (social science) * Identity …   Wikipedia

  • Column 88 — was a neo nazi paramilitary organization based in the United Kingdom. It was formed in the early 1970s, and disbanded in the early 1980s. The members of Column 88 undertook military training under the supervision of a former Royal Marine Commando …   Wikipedia

  • Identity Crisis (comics) — Infobox comic book title title = Identity Crisis caption = Identity Crisis #1, Coffin . Cover Art by Michael Turner. schedule = Monthly limited = y publisher = DC Comics date = June December 2004 issues = 7 Superhero = y main char team = The… …   Wikipedia

  • Identity matrix — In linear algebra, the identity matrix or unit matrix of size n is the n by n square matrix with ones on the main diagonal and zeros elsewhere. It is denoted by I n , or simply by I if the size is immaterial or can be trivially determined by the… …   Wikipedia

  • Identity of Junius — Junius was the pseudonym of a writer who contributed a series of political letters to the Public Advertiser [The Public Advertiser was a political newspaper run by Henry Sampson Woodfall, presumably based in London. His brother of William… …   Wikipedia

  • identity card — идентификационная карта wine card карта вин card key ключ карта key card ключ карта card scrap конфетти карт card column колонка карты …   English-Russian travelling dictionary

  • Lagrange's identity — In algebra, Lagrange s identity is the identity:iggl( sum {k=1}^n a k^2iggr) iggl(sum {k=1}^n b k^2iggr) iggl(sum {k=1}^n a k b kiggr)^2 = sum {i=1}^{n 1} sum {j=i+1}^n (a i b j a j b i)^2 iggl(= {1 over 2} sum {i=1}^n sum {j=1}^n (a i b j …   Wikipedia

  • Dear Prudence (advice column) — Dear Prudence is an advice column appearing weekly in the online magazine Slate and syndicated to over 200 newspapers. The column was initiated on 20 December 1997. Prudence was a pseudonym, and the author s true identity was not revealed at the… …   Wikipedia

  • Surrogate key — A surrogate 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 derived from application data. Definition Two definitions of a surrogate appear in the… …   Wikipedia

  • Enterprise JavaBeans — (также часто употребляется в виде аббревиатуры EJB)  спецификация технологии написания и поддержки серверных компонентов, содержащих бизнес логику. Является частью Java EE. Эта технология обычно применяется, когда бизнес логика требует как… …   Википедия

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”