0

谁能给我解释一下?

    mysql> select * from heartbeat order by id desc limit 20;
    +---------+---------------------+---------------------------------------------------------+
    | id      | date                | node                                                    |
    +---------+---------------------+---------------------------------------------------------+
    | 1944993 | 2013-03-22 12:06:29 | CN=kurian2,O=Nemesis Clinics Group,DC=nemesis,DC=com |
    | 1944992 | 2013-03-22 12:05:59 | CN=kurian2,O=Nemesis Clinics Group,DC=nemesis,DC=com |
    | 1944991 | 2013-03-22 12:05:29 | CN=kurian2,O=Nemesis Clinics Group,DC=nemesis,DC=com |
    | 1944990 | 2013-03-22 12:05:07 | CN=kurian2,O=Nemesis Clinics Group,DC=nemesis,DC=com |
    | 1944989 | 2013-03-22 12:05:07 | CN=kurian2,O=Nemesis Clinics Group,DC=nemesis,DC=com |
    | 1944988 | 2013-03-22 12:05:07 | CN=kurian2,O=Nemesis Clinics Group,DC=nemesis,DC=com |
    | 1944987 | 2013-03-21 21:12:59 | CN=kurian2,O=Nemesis Clinics Group,DC=nemesis,DC=com |
    | 1944986 | 2013-03-21 21:12:29 | CN=kurian2,O=Nemesis Clinics Group,DC=nemesis,DC=com |
    | 1944985 | 2013-03-21 21:11:59 | CN=kurian2,O=Nemesis Clinics Group,DC=nemesis,DC=com |
    | 1944984 | 2013-03-21 21:11:29 | CN=kurian2,O=Nemesis Clinics Group,DC=nemesis,DC=com |
    | 1944983 | 2013-03-21 21:10:59 | CN=kurian2,O=Nemesis Clinics Group,DC=nemesis,DC=com |
    | 1944982 | 2013-03-21 21:10:29 | CN=kurian2,O=Nemesis Clinics Group,DC=nemesis,DC=com |
    | 1944981 | 2013-03-21 21:09:59 | CN=kurian2,O=Nemesis Clinics Group,DC=nemesis,DC=com |
    | 1944980 | 2013-03-21 21:09:29 | CN=kurian2,O=Nemesis Clinics Group,DC=nemesis,DC=com |
    | 1944979 | 2013-03-21 21:08:59 | CN=kurian2,O=Nemesis Clinics Group,DC=nemesis,DC=com |
    | 1944978 | 2013-03-21 21:08:29 | CN=kurian2,O=Nemesis Clinics Group,DC=nemesis,DC=com |
    | 1944977 | 2013-03-21 21:07:59 | CN=kurian2,O=Nemesis Clinics Group,DC=nemesis,DC=com |
    | 1944976 | 2013-03-21 21:07:29 | CN=kurian2,O=Nemesis Clinics Group,DC=nemesis,DC=com |
    | 1944975 | 2013-03-21 21:06:59 | CN=kurian2,O=Nemesis Clinics Group,DC=nemesis,DC=com |
    | 1944974 | 2013-03-21 21:06:29 | CN=kurian2,O=Nemesis Clinics Group,DC=nemesis,DC=com |
    +---------+---------------------+---------------------------------------------------------+
    20 rows in set (0.01 sec)

试图插入下一个值..

insert into heartbeat values(1944994, '2013-03-22 12:06:29','CN=kurian2,O=Nemesis Clinics Group,DC=nemesis,DC=com');

错误 1062 (23000):密钥 1 的重复条目“1944994”

确保值不存在..

select * from heartbeat where id=1944994;
Empty set (0.00 sec)

    mysql> desc heartbeat;
    +-------+--------------+------+-----+---------+----------------+
    | Field | Type         | Null | Key | Default | Extra          |
    +-------+--------------+------+-----+---------+----------------+
    | id    | bigint(20)   | NO   | PRI | NULL    | auto_increment |
    | date  | datetime     | YES  |     | NULL    |                |
    | node  | varchar(255) | YES  |     | NULL    |                |
    +-------+--------------+------+-----+---------+----------------+
    3 rows in set (0.05 sec)

    mysql> SHOW VARIABLES LIKE "%version%";
    +-------------------------+-------------------------------+
    | Variable_name           | Value                         |
    +-------------------------+-------------------------------+
    | protocol_version        | 10                            |
    | version                 | 5.0.51b-community-nt          |
    | version_comment         | MySQL Community Edition (GPL) |
    | version_compile_machine | ia32                          |
    | version_compile_os      | Win32                         |
    +-------------------------+-------------------------------+
    5 rows in set (0.00 sec)

有没有人遇到同样的问题?为什么我在 1944994 出现异常?

4

1 回答 1

2

插入行时不要为该列提供值,因为id它是 auto_increment。所以 MySQL 会自动分配序列号。您也可以显式地将 NULL 或 0 分配给列以生成序列号。

于 2013-03-22T10:21:38.023 回答