How to create, validate, and modify the Identity column in Microsoft SQL Server

Microsoft SQL Server has a built-in Identity column, used to generate important values. It can reference an AutoNumber field in Microsoft Access or a range in Oracle. This article from TipsMake will guide you on how to create, check, and modify the Identity column in Microsoft SQL Server.

An Identity attribute in a column will generate a new value based on the current value and increments when a new record is inserted into the table. Each new value generated for a specific transaction will differ from other concurrent transactions in the table. This Identity column can be used as a single column when it is designated as the primary key.

Readers can find detailed instructions on how to create, check, and modify the Identity column in Microsoft SQL Server in the article below by TipsMake.

How to create, validate, and modify the Identity column in Microsoft SQL Server

Step 1: Create a table using the Identity property.

In this tutorial, TipsMake will create a new table called TechJourney, which includes three columns: TechJourneyID, TechJourneyCode, and Description.

- SQL syntax:

Create Table TableName
( ID int IDENTITY(1,1) NOT NULL,
Column1 datatype,
Column2 datatype,
Column3 datatype,
PRIMARY KEY (ID))

Run the following SQL command in SQL Management Studio:

CREATE TABLE TechJourney (
TechJourneyID int IDENTITY(1,1) NOT NULL,
TechJourneyCode [nvarchar](10) NULL,
Description [nvarchar](100) NULL,
PRIMARY KEY(TechJourneyID))

Next, insert the two records below into the TechJourney table to check the Identity value:

Insert into TechJourney(TechJourneyCode, Description)

Values('TJ1', 'Tech Journey 1')

And:

Insert into TechJourney( TechJourneyCode, Description)
Values( 'TJ2', 'Tech Journey 2')

Step 2: Check the current Identity value

- SQL syntax:

DBCC CHECKIDENT(table_name, NORESEED)

Run the following SQL command in SQL Management Studio:

Note : NORESEED is the current Identity value and should not be changed.

The next step is to check the current value after inserting the two records.

Run the following SQL command in SQL Management Studio:

DBCC CHECKIDENT(TechJourney, NORESEED)

It will return the following message:

"Checking identity information: current identity value '2'.
DBCC execution completed. If DBCC printed error messages, contact your system administrator".

Next, select the records in the TechJourney table to view the TechJourneyID value as shown below:

Step 3: Force the current Identity value to the new value.

- SQL syntax:

DBCC CHECKIDENT(table_name, RESEED, new_reseed_value)

Suppose you want to change the current value from 2 to 20. Run the following SQL command in SQL Management Studio:

DBCC CHECKIDENT(TechJourney, RESEED, 20)

It will return the following message:

"Checking identity information: current identity value '2'.

DBCC execution completed. If DBCC printed error messages, contact your system administrator".

Verify the current Identity rental price again by running the following SQL command in SQL Management Studio:

DBCC CHECKIDENT(TechJourney, NORESEED)

It will return the following message:

"Checking identity information: current identity value '20', current column value '2'.
DBCC execution completed. If DBCC printed error messages, contact your system administrator".

In the previous step, we inserted two records into the table, and the Identity value is now 2.

Insert the two records into the TechJourney table again by running the SQL command below:

Insert into TechJourney(TechJourneyCode, Description)

Values('TJ3', 'Tech Journey 3')

And

Insert into TechJourney(TechJourneyCode, Description)

Values('TJ4', 'Tech Journey 4')

Select the records in the TechJourney table to view the TechJourneyID value as shown below:

TechjourneyID for T3 is 21 = 20 (new identity value) + 1 (increase value).


The article on TipsMake just guided you on how to create, check, and modify the Identity column in Microsoft SQL Server. Other SQL Server tutorials, such as how to reset a lost SA password on SQL Server, will help you gain a better understanding of this system. If you have any questions, please leave your comments in the section below the article.

Related posts
Other SQL articles
  • How to log in to SQL Server if the SA account is disabled.

    trong trường hợp nếu tài khoản sa bị vô hiệu hóa và bạn không thể đăng nhập sql server. bài viết này tipsmake sẽ hướng dẫn bạn cách đăng nhập sql server nếu tài khoản sa bị vô hiệu hóa.
  • Instructions on how to install Laravel, supporting web programming.

    với những ai đang học lập trình không thể nào không biết đến được laravel, vậy làm thế nào để cài đặt laravel trên máy tính của bạn, chạy trơn chu các ứng dụng web cũng như giúp bạn học tập và thực hành tốt.
  • How to install SQL Server 2019 on Windows

    phiên bản microsoft sql server 2019 mới nhất mang đến rất nhiều tính năng mới hấp dẫn đáp ứng được người dùng hiện nay, nhất là giúp tổ chức đánh giá, kiểm tra dữ liệu và tích hợp nhiều phần mềm khác nhau. Để cài đặt microsoft sql server 2019, các bạn làm theo hướng dẫn trong bài viết sau đây.
  • The AND, OR, and NOT operators in SQL

    toán tử and và or được sử dụng để kết hợp nhiều điều kiện nhằm thu hẹp dữ liệu trong câu lệnh sql. hai toán tử này được gọi là toán tử liên hợp trong sql. and và or cho phép tạo nhiều so sánh với các toán tử khác trong cùng một lệnh sql.
  • DROP DATABASE command in SQL

    drop database trong sql được dùng như thế nào? bài viết sẽ hướng dẫn bạn chi tiết cách xóa database trong sql.
  • Learn about the most popular RDBMSs

    rdbms có nhiều loại như mysql, oracle, sql server, mongo db, sybase,... Để hiểu hơn về rdbms, trong bài viết này chúng ta sẽ tìm hiểu về một số rdbms phổ biến nhất, so sánh tính năng cơ bản của chúng. mời các bạn cùng theo dõi.
Category

System

Windows XP

Windows Server 2012

Windows 8

Windows 7

Windows 10

Wifi tips

Virus Removal - Spyware

Speed ​​up the computer

Server

Security solution

Mail Server

LAN - WAN

Ghost - Install Win

Fix computer error

Configure Router Switch

Computer wallpaper

Computer security

Mac OS X

Mac OS System software

Mac OS Security

Mac OS Office application

Mac OS Email Management

Mac OS Data - File

Mac hardware

Hardware

USB - Flash Drive

Speaker headset

Printer

PC hardware

Network equipment

Laptop hardware

Computer components

Advice Computer

Game

PC game

Online game

Mobile Game

Pokemon GO

information

Technology story

Technology comments

Quiz technology

New technology

British talent technology

Attack the network

Artificial intelligence

Technology

Smart watches

Raspberry Pi

Linux

Camera

Basic knowledge

Banking services

SEO tips

Science

Strange story

Space Science

Scientific invention

Science Story

Science photo

Science and technology

Medicine

Health Care

Fun science

Environment

Discover science

Discover nature

Archeology

Life

Travel Experience

Tips

Raise up child

Make up

Life skills

Home Care

Entertainment

DIY Handmade

Cuisine

Christmas

Application

Web Email

Website - Blog

Web browser

Support Download - Upload

Software conversion

Social Network

Simulator software

Online payment

Office information

Music Software

Map and Positioning

Installation - Uninstall

Graphic design

Free - Discount

Email reader

Edit video

Edit photo

Compress and Decompress

Chat, Text, Call

Archive - Share

Electric

Water heater

Washing machine

Television

Machine tool

Fridge

Fans

Air conditioning

Program

Unix and Linux

SQL Server

SQL

Python

Programming C

PHP

NodeJS

MongoDB

jQuery

JavaScript

HTTP

HTML

Git

Database

Data structure and algorithm

CSS and CSS3

C ++

C #

AngularJS

Mobile

Wallpapers and Ringtones

Tricks application

Take and process photos

Storage - Sync

Security and Virus Removal

Personalized

Online Social Network

Map

Manage and edit Video

Data

Chat - Call - Text

Browser and Add-on

Basic setup