选择主键数据类型 numeric (18,0)
我继承了一个 SQL Server 数据库,其中许多表都有 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我猜测 SQL Server 数据库最初是在 SQL Server 2000 之前的版本中设计的,这是他们获得大于标准 int 的“整数”的唯一方法。从那时起,bigint 就更合适了。
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?
大概是整数的最大精度。当然,我一直使用它作为主键,它是由生成 64 位数字的数据库生成器提供的。
我实际上并没有使用 SQL-Server,但是我的数据库中的整数是 32 位数字,而不是 64 位。正如另一个答案中所建议的,它很可能是从存在此类限制的数据库中开始的。
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.
如果您允许 Hibernate 生成您的架构,它会使用 jtds 将 Long 数据类型转换为 numeric(19,0)。您的情况可能也发生过类似的情况。
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.