Manni
Manni

Reputation: 1

Using IDENTITY while creating table

I am learning SQL and totally new to this world. What I have learnt / read about using identity is: it is used to create an identity column with Seed and increment value ~ Identity (Seed, increment). However going through some sample database available on net, I came across this table creation script:

create table Customer 
(
    Id                   int                  identity,
    FirstName            nvarchar(40)         not null,
    LastName             nvarchar(40)         not null,
    City                 nvarchar(40)         null,
    Country              nvarchar(40)         null,
    Phone                nvarchar(20)         null,
    constraint PK_CUSTOMER primary key (Id)
)
go

I tried creating table with this code and it was successful.

Can someone explain why IDENTITY here does not have seed and increment values? When should we use it like this (without seed and increment values) ?

TIA

Upvotes: 0

Views: 365

Answers (1)

Rahul Richhariya
Rahul Richhariya

Reputation: 514

When you use --> Id int identity

It is equivalent to ---> Id int identity(1,1)

So the first value will be inserted into table with Id = 1 and incremented by 1 for next row. so this is the default value for identity if nothing is specified. In case if you want to start your Id with some specific number, for ex - you want to create customerId column which should have at least 5 digit id than you have to define the column as ---> CustomerId int identity(10000 ,1)

Upvotes: 2

Related Questions