HOW TO SET IDENTITY KEY/AUTO INCREMENT ON VARCHAR COLUMN IN SQL SERVER
Create the table as follows
1 2 3 4 5 6 7 8 9 |
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.
1 2 |
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
1 |
SELECT * FROM Student_Register |
you can see the inserted records below