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

╄→гoц情女王★ 提交于 2020-01-11 05:16:27

问题


"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.


回答1:


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



回答2:


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?




回答3:


Data is stored in the database, keyed by the IDENTITY column. A single such column allows for a filesystem-like storage. Having multiple IDENTITY columns would confuse the issue.

My recommendation is to choose one of your columns to be the IDENTITY or PRIMARY KEY, and for the other to be a UNIQUE KEY. As a user there will be no difference, but it will make the DBMS happy.




回答4:


  • Oracle sequences are not SQL Server IDENTITY columns: you write some code for them. They don't work out of the box based on the CREATE TABLE DDL
  • Any subsequent IDENTITY columns can be worked out from the first one (edit: as other folk mentioned)



回答5:


The reason isn't explained in MSDN that I could find. However, I suspect that it has to do with the way sql server implements the identity column. Conceptually, I think it's more a setting or value at the table level than at the column level and the column is just a way to access the value. I think this because all the methods for interacting with identity values have the table as a parameter and not the column eg. IDENT_SEED, IDENT_CURRENT, and IDENT_INCR. If it was a column level setting, the parameters would include a column name. If you had more than one identity column on the table, those functions wouldn't work.

I'm just speculating though.




回答6:


It's a silly limitation I agree. It's not too difficult to work around it though. Just create a separate table with an IDENTITY column to use purely as a sequence generator. Insert into the sequence table, retrieve the value using SCOPE_IDENTITY() and then insert the value wherever you like. You can then support as many sequences as you need.



来源:https://stackoverflow.com/questions/3807331/there-can-only-be-one-identity-column-per-table-why

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!