简体   繁体   English

MySQL auto_increment列声明为float

[英]MySQL auto_increment column declared as float

I need some help with a table InnoDB stored in a database MySQL version 5.5 on a Ubuntu linux server. 我需要有关存储在Ubuntu Linux服务器上的MySQL 5.5版数据库中的表InnoDB的一些帮助。

In this table there is a primat key column "id" that has the attribute auto increment and is declared as float. 在此表中,有一个主键列“ id”,其属性自动递增,并声明为float。

Currently, the auto increment value stored in this table has reached high values (approximately, over 2 million). 当前,此表中存储的自动增量值已达到较高的值(大约超过200万)。

Now, when I dump (or select) a range of records from this table I cannot see the correct value reached from the increment but a rounded value (for example: 2233937 becames 2233940 for the next records and so on with other records). 现在,当我转储(或选择)该表中的一系列记录时,我看不到从增量中获得的正确值,而是四舍五入后的值(例如:2233937变为2233940,下一条记录,依此类推,其他记录也如此)。 This cause a malfunction when I import a MySQL dump file because the auto increment value is rounded and many records have the same incremental id. 当我导入MySQL转储文件时,这会导致故障,因为自动递增值被舍入并且许多记录具有相同的增量ID。

A side note. 旁注。 In this database I have other tables with the same situation (colums primary key declared as float with the auto increment attribute ) but with a minor incremental values stored in its. 在这个数据库中,我还有其他情况相同的表(列主键声明为float,具有auto增量属性),但表中存储了较小的增量值。 And in these tables the stored auto increment value has the correct number. 并且在这些表中,存储的自动增量值具有正确的数字。

Then, I have these question: 然后,我有以下问题:

1) Why with this table I have this behavior? 1)为什么在此表中有此行为? The column has reached the maximum size? 列已达到最大大小?

2) How I can resolve this issue with this table? 2)如何使用此表解决此问题?

Thanks a lot for any response. 非常感谢您的任何回复。

Lorenzo 洛伦佐

UPDATE UPDATE

Currently, I have adopt this method to solve the issue. 目前,我已经采用这种方法来解决此问题。

I have execute the command ALTER TABLE to modify the type of the column from float to integer and this seems to work. 我已经执行命令ALTER TABLE来将列的类型从float修改为integer,这似乎可行。

Please, can you confirm to me that this is the correct way (or perhaps only one of many ways) to solve this problem? 请,您能向我确认这是解决此问题的正确方法(或也许只是许多方法中的一种)吗?

As is well mentioned in the documentation: 正如文档中所提到的:

B.5.4.8 Problems with Floating-Point Values B.5.4.8浮点值问题

Floating-point numbers sometimes cause confusion because they are approximate and not stored as exact values. 浮点数有时会引起混淆,因为它们是近似值而不是作为精确值存储的。

A local test presents a couple of alternatives, but without guarantees (especially the second alternative): 本地测试提供了两种选择,但没有保证(尤其是第二种选择):

Alternative 1: 选择1:

mysql> DROP TABLE IF EXISTS `tbl_test`;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE IF NOT EXISTS `tbl_test` (
    ->   `id` FLOAT UNSIGNED AUTO_INCREMENT PRIMARY KEY
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> SHOW CREATE TABLE `tbl_test`\G
*************************** 1. row ***************************
       Table: tbl_test
Create Table: CREATE TABLE `tbl_test` (
  `id` float unsigned NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

mysql> INSERT INTO `tbl_test`
    ->   (`id`)
    -> VALUES
    ->   (2233937),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (16776999),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL);
Query OK, 32 rows affected (0.00 sec)
Records: 32  Duplicates: 0  Warnings: 0

mysql> SELECT `id`,
    ->        CONVERT(`id`, UNSIGNED)
    -> FROM `tbl_test`;
+----------+-------------------------+
| id       | CONVERT(`id`, UNSIGNED) |
+----------+-------------------------+
|  2233940 |                 2233937 |
|  2233940 |                 2233938 |
|  2233940 |                 2233939 |
|  2233940 |                 2233940 |
|  2233940 |                 2233941 |
|  2233940 |                 2233942 |
|  2233940 |                 2233943 |
|  2233940 |                 2233944 |
|  2233940 |                 2233945 |
|  2233950 |                 2233946 |
|  2233950 |                 2233947 |
|  2233950 |                 2233948 |
|  2233950 |                 2233949 |
|  2233950 |                 2233950 |
|  2233950 |                 2233951 |
|  2233950 |                 2233952 |
| 16777000 |                16776999 |
| 16777000 |                16777000 |
| 16777000 |                16777001 |
| 16777000 |                16777002 |
| 16777000 |                16777003 |
| 16777000 |                16777004 |
| 16777000 |                16777005 |
| 16777000 |                16777006 |
| 16777000 |                16777007 |
| 16777000 |                16777008 |
| 16777000 |                16777009 |
| 16777000 |                16777010 |
| 16777000 |                16777011 |
| 16777000 |                16777012 |
| 16777000 |                16777013 |
| 16777000 |                16777014 |
+----------+-------------------------+
32 rows in set (0.00 sec)

Example db-fiddle . 示例db-fiddle

Alternative 2: (be careful) 选择2 :(请小心)

mysql> DROP TABLE IF EXISTS `tbl_test`;
Query OK, 0 rows affected (0.01 sec)

mysql> CREATE TABLE IF NOT EXISTS `tbl_test` (
    ->   `id` FLOAT UNSIGNED AUTO_INCREMENT PRIMARY KEY
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> SHOW CREATE TABLE `tbl_test`\G
*************************** 1. row ***************************
       Table: tbl_test
Create Table: CREATE TABLE `tbl_test` (
  `id` float unsigned NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

mysql> INSERT INTO `tbl_test`
    ->   (`id`)
    -> VALUES
    ->   (2233937),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (16776999),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL),
    ->   (NULL);
Query OK, 32 rows affected (0.00 sec)
Records: 32  Duplicates: 0  Warnings: 0

mysql> ALTER TABLE `tbl_test` CHANGE `id`
    ->   `id` FLOAT(8, 0) UNSIGNED AUTO_INCREMENT;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> SHOW CREATE TABLE `tbl_test`\G
*************************** 1. row ***************************
       Table: tbl_test
Create Table: CREATE TABLE `tbl_test` (
  `id` float(8,0) unsigned NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=16777016 DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

mysql> SELECT `id`
    -> FROM `tbl_test`;
+----------+
| id       |
+----------+
|  2233937 |
|  2233938 |
|  2233939 |
|  2233940 |
|  2233941 |
|  2233942 |
|  2233943 |
|  2233944 |
|  2233945 |
|  2233946 |
|  2233947 |
|  2233948 |
|  2233949 |
|  2233950 |
|  2233951 |
|  2233952 |
| 16776999 |
| 16777000 |
| 16777001 |
| 16777002 |
| 16777003 |
| 16777004 |
| 16777005 |
| 16777006 |
| 16777007 |
| 16777008 |
| 16777009 |
| 16777010 |
| 16777011 |
| 16777012 |
| 16777013 |
| 16777014 |
+----------+
32 rows in set (0.00 sec)

Example db-fiddle . 示例db-fiddle

Other information of interest: 其他感兴趣的信息:

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

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