简体   繁体   中英

choosing primary key datatype numeric (18,0)

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 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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM