Tuesday, March 3, 2015

Identity Function in SQL Server

12:50 AM - By ajay desai 0

Identity function

An identity function is used to provide unique values to a column like unique constraint but the difference is that, an identity column generates unique values for a column by incrementing the initial value by some number like 1 or 2 whereas a unique constraint only restricts duplicate values from being entered for a column.

Syntax for creating identity function: -
Colname<dtype>[width] Identity [(Seed,Incr)]

Here, ‘seed’ is the initial value given by the user and ‘Incr’ is the difference between subsequent values generated by the function. Both ‘seed’ and ‘incr’ are optional values, if not specified 1 and 1 are taken as values.
 A user cannot insert any value in an identity column directly.

As a table can have only one identity column, so identity columns are used only on primary key columns.

create table department(deptid int identity(101,1),dname varchar(50) not null,dloc varchar(50) not null)

insert into department(dname,dloc) values('Accounts','Kukatpally')

insert into department(dname,dloc) values('IT','Maredpally')

select *from department



Tags:
About the Author

I am Azeheruddin Khan having more than 6 year experience in c#, Asp.net and ms sql.My work comprise of medium and enterprise level projects using asp.net and other Microsoft .net technologies. Please feel free to contact me for any queries via posting comments on my blog,i will try to reply as early as possible. Follow me @fresher2programmer
View all posts by admin →

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

adsense

© 2014 Fresher2Programmer. WP Theme-junkie converted by Bloggertheme9
Powered by Blogger.
back to top