Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2018 March SQL Server Interview: Difference between Sequence Object and Identity Column

SQL Server Interview: Difference between Sequence Object and Identity Column

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

This is a common interview question for SQL Server Database Developer. SQL Database Developer must prepare for this.

Sequence and identity, both used to generate an auto number, but the major difference is Identity is a table dependant and Sequence is independent of a table.

A Sequence Object introduced in SQL Server 2012 and Identity column introduced in SQL Server 6.0.

A Sequence is a User defined object and Identity column is a table property.

The same Sequence object can use with the multiple tables. The same Identity column cannot use with multiple tables, we can define identity column on each table individually.

If you define the same Sequence Object to multiple tables, it generates the sequence number database-wide and maintains a unique sequence number across the multiple tables. An Identity column generates a unique number at table only.

Sequence syntax (Start with 1 and Increment by 1):

1
2
3
4
5
CREATE SEQUENCE
dbo.Sequence_Name AS INT
START WITH 1
INCREMENT BY 1
GO

Identity syntax (Start with 1 and Increment by 1):

1
2
3
4
CREATE TABLE dbo.Table_Name
( Id INT IDENTITY(1,1),
Name VARCHAR(50) )
GO

Check the next value of Sequence object:

1
SELECT (NEXT VALUE FOR dbo.Sequence_Name) AS SeqNextValue

Check the next value of Identity Column:

1
SELECT SCOPE_IDENTITY()
(write after use of Identity column)

Check the current value of Sequence Object:

1
2
3
SELECT Current_Value
FROM Sys.Sequences
WHERE name='Sequence_Name'

Check the current value of Identity Column:

1
2
SELECT IDENT_CURRENT('Table_Name')
AS 'IdentityCurrentValue'

You can modify a Sequence object using ALTER Statement.

1
2
3
4
ALTER SEQUENCE dbo.Sequence_Name
RESTART WITH 100
INCREMENT BY 5
GO

You can only reset the value of an Identity column using DBCC CHECKIDENT().

1
DBCC CHECKIDENT('Table_Name', RESEED,100)

In Sequence New values can be generated in an UPDATE statement, but in Identity new value cannot be generated in an UPDATE statement.

You can define a maximum value for Sequence Object (Using MAXVALUE). You cannot provide a maximum value to Identity Column.

Once your Sequence object reached to MAXVALUE, you can also define a CYCLE option to restart it from the beginning. You cannot restart an Identity column value.

You can also enable data CACHE for a Sequence Object. You cannot enable data CACHE for an Identity Column and it uses table related data cache.

A User must require an access permission for Sequence Object. For Identity column, User requires only table related permissions.

Performance wise, Identity column is 3x faster than Sequence object.

Mar 8, 2018Anvesh Patel
SQL Server 2016: Enable System-versioned Temporal Table on existing TableSQL Server: Optimize the Performance of TempDB

Leave a Reply Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

March 8, 2018 SQL Server, SQL Server InterviewAnvesh Patel, database, database research and development, dbrnd, IDENT_CURRENT, Identity Column, SCOPE_IDENTITY, Sequence object, SQL Query, SQL Server, SQL Server 2012, SQL Server Administrator, SQL Server Interview, SQL Server Monitoring, SQL Server Performance Tunning, SQL Server Tips and Tricks, TSQL
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....