Mysql表列出的行不按顺序排列

时间:2014-09-10 17:25:11

标签: mysql phpmyadmin

今天我在一个mysql表中插入了一行,但是发生了一些奇怪的事情,因为最后添加的行具有较高的主要ID是在具有较低id的其他行之前以及其他具有较低id的行之后,如下所示:

+---------+
| user_id |
+---------+
|   1     |
|   50    |
|   69    | <=== Wrong place
|   63    |
+---------+

这是它在phpmyadmin和我的php脚本中列出的方式。我明白,如果我不在我的PHP脚本中定义列出行的顺序,但在phpmyadmin我不知道为什么会发生这种情况。顺便说一下,这是我第一次看到这样的东西。

这是查询的结果:SHOW CREATE TABLE mytable

CREATE TABLE `users` (
    `user_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'auto incrementing user_id of each user,
     unique index',  `web_id` varchar(166) COLLATE utf8_unicode_ci NOT NULL,
    `user_name` varchar(64) COLLATE utf8_unicode_ci NOT NULL COMMENT 'user''s name',
    `user_password_hash` varchar(255) COLLATE utf8_unicode_ci NOT NULL COMMENT 'user''s password in salted and hashed format',
    `user_email` varchar(64) COLLATE utf8_unicode_ci NOT NULL COMMENT 'user''s email',
    `domain` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
    `address` varchar(166) COLLATE utf8_unicode_ci NOT NULL,
    `billing_firstname` varchar(166) COLLATE utf8_unicode_ci NOT NULL,
    `billing_lastname` varchar(166) COLLATE utf8_unicode_ci NOT NULL,
    `billing_phone` varchar(90) COLLATE utf8_unicode_ci NOT NULL,
    `billing_country` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
    `git_auto_update` enum('Y','N') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'Y',
    `server_id` int(16) NOT NULL,
    `trial` enum('0','1') COLLATE utf8_unicode_ci NOT NULL,
    `purchase_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`user_id`),
    UNIQUE KEY `user_name` (`user_name`)
) ENGINE=MyISAM AUTO_INCREMENT=39 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='user data'

1 个答案:

答案 0 :(得分:1)

这只是猜测,但在这里......

您的表格布局是

CREATE TABLE `users` (
    `user_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'auto incrementing user_id of each user,
     unique index',  `web_id` varchar(166) COLLATE utf8_unicode_ci NOT NULL,
    `user_name` varchar(64) COLLATE utf8_unicode_ci NOT NULL COMMENT 'user''s name',
    `user_password_hash` varchar(255) COLLATE utf8_unicode_ci NOT NULL COMMENT 'user''s password in salted and hashed format',
    `user_email` varchar(64) COLLATE utf8_unicode_ci NOT NULL COMMENT 'user''s email',
    `domain` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
    `address` varchar(166) COLLATE utf8_unicode_ci NOT NULL,
    `billing_firstname` varchar(166) COLLATE utf8_unicode_ci NOT NULL,
    `billing_lastname` varchar(166) COLLATE utf8_unicode_ci NOT NULL,
    `billing_phone` varchar(90) COLLATE utf8_unicode_ci NOT NULL,
    `billing_country` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
    `git_auto_update` enum('Y','N') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'Y',
    `server_id` int(16) NOT NULL,
    `trial` enum('0','1') COLLATE utf8_unicode_ci NOT NULL,
    `purchase_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`user_id`),
    UNIQUE KEY `user_name` (`user_name`)
) ENGINE=MyISAM AUTO_INCREMENT=39 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='user data'

我的直觉表示user_id值以user_name顺序出现。当存在PRIMARY KEY和UNIQUE KEY时,有时会发生这种情况。您可以通过运行

来验证这一点
SELECT user_id,user_name FROM users;
SELECT user_id,user_name FROM users ORDER BY user_id;
SELECT user_id,user_name FROM users ORDER BY user_name;

并比较输出