HOW TO SET IDENTITY KEY/AUTO INCREMENT ON VARCHAR COLUMN IN SQL SERVER

Create the table as follows


CREATE TABLE Student_Register(
SNo INT NOT NULL IDENTITY(1000, 1),
Student_Id AS 'STU_'+CAST( SNo as varchar(10)) PERSISTED PRIMARY KEY,
First_Name VARCHAR(20),
Last_Name VARCHAR(20),
Date_Of_Birth DATE,
Address VARCHAR(100),
Mobile_No BIGINT
)

Here while inserting there is no need to pass values of Sno,Student_Id column.
Then insert the fields like as shown below.


INSERT INTO Student_Register VALUES('sameer','shaik','1990/10/19','hyderabad',9999999999)
INSERT INTO Student_Register VALUES('shafi','shaik','1991/05/27','india',9898989898)

Now displaying the inserted records

SELECT * FROM Student_Register

you can see the inserted records below

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s