Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. can we make float primary key to be identity ?

like image 976
Buzz Avatar asked May 24 '12 11:05

Buzz


2 Answers

Considerations:

  • you CAN make a float field primary key.
  • you CANT make a float field IDENTITY. Identity columns must be of data type int, bigint, smallint, tinyint, or decimal or numeric with a scale of 0
  • you ABSOLUTELY SHOULD NOT use a float field as a PK. Again, you CAN, SQL Server will allow you but its not recommended mainly because floats are inaccurate as @Andy said.

why do you need a float as a PK anyway? Do you need a value like 3,1234235234534 to uniquely identify your row?

like image 111
Diego Avatar answered Sep 20 '22 19:09

Diego


Notice how if you do float a=1f and float b=1f they are the same right ?

However if(a==b) will likely not be true as floats are inaccurate.

like image 29
Andrew Avatar answered Sep 23 '22 19:09

Andrew