I inherited a SQL Server database where many tables have a primary key of type numeric(18,0)
.
What reasons (historical perhaps?) would someone choose this datatype for 开发者_JS百科a primary key?
I would guess that the SQL Server database was originally designed in a version prior to SQL Server 2000, and this was the only way they could get an 'integer' bigger than the standard int. Since then, a bigint would have been more appropriate.
There is no right or wrong type for a key. You should choose attributes and types that accurately represent the business domain or match the input data. If the data you are recording is of numeric type then why wouldn't you store it as a numeric?
Maximum precision for an integer presumably. I certainly use this all the time for primary keys, which are supplied from a database generator which generates 64 bit numbers.
I don't actually use SQL-Server, but integer on my database is a 32 bit number, not 64 bit. As suggested in the other answer, it is probable that it started in a database where this sort of limitation was in place.
If you allow Hibernate to generate your schema, it will convert a Long data type to numeric(19,0) with jtds. Something similar may have happened in your case.
精彩评论