简体   繁体   English

我们应该使用float作为sql-server中的主键

[英]should we use float as primary key in sql-server

some one suggested me its a better idea to use float as a primary key of a table instead of using the BIGINT. 有人建议我使用float作为表的主键而不是使用BIGINT更好。 can we make float primary key to be identity ? 我们可以使浮动主键成为身份吗?

Considerations: 注意事项:

  • you CAN make a float field primary key. 你可以制作一个浮点字段主键。
  • you CANT make a float field IDENTITY. 你不能创建一个浮点字段IDENTITY。 Identity columns must be of data type int, bigint, smallint, tinyint, or decimal or numeric with a scale of 0 标识列必须是数据类型int,bigint,smallint,tinyint或十进制或数字,标度为0
  • you ABSOLUTELY SHOULD NOT use a float field as a PK. 你绝对不应该使用浮点字段作为PK。 Again, you CAN, SQL Server will allow you but its not recommended mainly because floats are inaccurate as @Andy said. 再一次,你可以,SQL Server会允许你,但不推荐它主要是因为浮动是不准确的,如@Andy所说。

why do you need a float as a PK anyway? 为什么你需要一个浮动作为PK呢? Do you need a value like 3,1234235234534 to uniquely identify your row? 您是否需要像3,1234235234534这样的值来唯一标识您的行?

Notice how if you do float a=1f and float b=1f they are the same right ? 请注意如果浮动a = 1f并浮动b = 1f它们是否相同?

However if(a==b) will likely not be true as floats are inaccurate. 但是,如果(a == b)可能不正确,因为浮点数不准确。

Why not use big data type for primary keys: 为什么不对主键使用大数据类型:

Keep the "width" of your indexes as narrow as possible. 保持索引的“宽度”尽可能窄。 This reduces the size of the index and reduces the number of disk I/O reads required to read the index, boosting performance. 这样可以减小索引的大小,并减少读取索引所需的磁盘I / O读取次数,从而提高性能。

If possible, try to create indexes on columns that have integer values instead of characters. 如果可能,尝试在具有整数值而不是字符的列上创建索引。 Integer values have less overhead than character values. 整数值的开销小于字符值。

Don't use FLOAT or REAL data types for primary keys, as they add unnecessary overhead and can hurt performance. 不要将FLOAT或REAL数据类型用于主键,因为它们会增加不必要的开销并且可能会损害性能。

Indexes on narrow columns are preferable to indexes on wide columns. 窄列上的索引优于宽列上的索引。 The narrower the index, the more entries SQL Server can fit on a data page, which in turn reduces the amount of I/O required to access the data. 索引越窄,SQL Server可以在数据页面上放置的条目就越多,这反过来又减少了访问数据所需的I / O量。

Reduce the size of the keys, thus decreasing read I/O during the join process, and increasing overall performance. 减小键的大小,从而减少连接过程中的读取I / O,并提高整体性能。

You can not make float as a identity. 你不能把浮动作为一个身份。

And it is very bad idea to use float as the primary key. 使用float作为主键是非常糟糕的主意。 You must go with the bigint. 你必须和bigint一起去。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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