Use float as primary key in sql server

someone suggested I better use float as the primary key of the table instead of using BIGINT. can we make a primary key float for identification?

+6
source share
4 answers

Questions:

  • you can make the primary key of the float field.
  • You CANNOT GET THE POLITICAL IDENTITY field. Identity columns must be int, bigint, smallint, tinyint, or decimal or numeric with a scale of 0
  • You MUST use the float field as a PC. Again, you CAN SQL Server allows you, but is not recommended, because Floats are inaccurate, as @Andy said.

why do you need a float like a PC anyway? Do you need a value, for example, 3.1234235234534, to uniquely identify your string?

+8
source

Please note that if you do float a = 1f and float b = 1f, then they have the same right?

However, if (a == b) is most likely not true, since the float is inaccurate.

+3
source

Why not use a large 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, increasing performance.

If possible, try creating indexes on columns with integer values ​​instead of characters. Integer values ​​have less overhead than character values.

Do not use the FLOAT or REAL data types for primary keys, as they add extra overhead and can hurt performance.

Indexes on narrow columns are preferable to indexes on wide columns. The smaller the index, the more SQL Server records can fit on the data page, which in turn reduces the amount of I / O required to access the data.

Reduce the size of the keys, thereby reducing I / O reading during the connection process and increasing overall performance.

+2
source

You cannot make a float as an identifier.

And a very bad idea to use float as your primary key. You have to go with bigint.

0
source

Source: https://habr.com/ru/post/916554/


All Articles