Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"There can only be one IDENTITY column per table" - Why?

"There can only be one IDENTITY column per table"

Why is it so? Take a scenario of a vehicle, there exists a chasis number which is unique as well as the registration number which turns out to be unique. To depict this scenario in sql server we need a custom implementation for on of the columns. Conversely, in Oracle you can have as many sequences as you want on a table. Why is there a restriction on the IDENTITY Column, any specific reasons?

The scenario of having a vehicle schema is something imaginary am questioning myself as to why there's a restriction on the identity column.

like image 526
Chaitanya Avatar asked Sep 27 '10 19:09

Chaitanya


People also ask

Can a table have more than one identity column?

Only one identity column per table is allowed. So, no, you can't have two identity columns.

How many identity can you have in a table?

Technically a table can have only one identity column and if you try to add another identity column to it, it will give you an error.

How many columns can be defined as identity column s in a table?

Note that SQL Server allows you to have only one identity column per table.

What is the purpose of identity column?

An identity column will automatically generate and populate a numeric column value each time a new row is inserted into a table. The identity column uses the current seed value along with an increment value to generate a new identity value for each row inserted.


2 Answers

An Identity Column in SQL Server has a seed and an auto increment. We could always calculate what the 2nd hypothetical id value should be if we knew the value of the first id column anyway.

e.g. If this was legal syntax

create table #foo
(
bar int identity(1,10),
baz int identity(1000,1)
)

We wouldn't need to store baz as it could be calculated from bar as follows.

baz = 1000 + (bar-1)/10
like image 145
Martin Smith Avatar answered Nov 15 '22 10:11

Martin Smith


Because it's would be the same value. if you had identity(1,1) as the primary, and identity(100,2), you would get these results:

1     100
2     102
3     104
4     106
5     108
6     110
7     112
8     114
9     116
10    118

you could get the second column by doing this:

((ID-1)*2)+100

it's a linear equation, so what would be the point other than for show?

like image 35
DForck42 Avatar answered Nov 15 '22 08:11

DForck42