MySQL:id号自动递增不一致
我想知道您是否遇到过这样的情况:当您点击 phpMyAdmin 上的“浏览”按钮时,自动递增的 id 编号没有按正确的顺序排列 - 是只有我这样吗?我在数据库表中设置不正确的东西?
例如,当您向表中插入一系列数据时,您也删除了其中一些数据,因此当您在 phpMyAdmin 上检查此表时,这些数据应该按这样的顺序出现,
id
2
24
28
296
300
但在我的大多数表中,它们没有不会出现在订单中,而是出现在类似的内容中,
id
24
300
2
296
28
如下图所示,
我们可以做点什么吗以便 ID 以正确的顺序出现?
编辑1:
我认为你们大多数人都误解了我在这里突袭的问题。我的意思是,当您单击 phpMyAdmin 上的“浏览器”按钮列出表中的所有数据时,而不是当您使用 SQL 查询通过使用 order by
列出输出时。
有道理吗?
编辑2:
这是我的表格结构之一 - 它可以帮助您查看这有什么问题吗?
CREATE TABLE IF NOT EXISTS `root_pages` (
`pg_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`pg_url` varchar(255) DEFAULT NULL,
`pg_title` varchar(255) DEFAULT NULL,
`pg_subtitle` varchar(255) DEFAULT NULL,
`pg_description` text,
`pg_introduction` text,
`pg_content_1` text,
`pg_content_2` text,
`pg_content_3` text,
`pg_content_4` text,
`pg_order` varchar(255) DEFAULT NULL,
`pg_hide` varchar(255) DEFAULT '0',
`pg_highlight` varchar(255) DEFAULT '0',
`pg_important` varchar(255) DEFAULT '0',
`pg_parent` varchar(255) DEFAULT '0',
`parent_id` varchar(255) DEFAULT NULL,
`pg_cat_id` varchar(255) DEFAULT NULL COMMENT 'page category id',
`ps_cat_id` varchar(255) DEFAULT NULL COMMENT 'post category id',
`tmp_id` varchar(255) DEFAULT NULL COMMENT 'template id',
`pg_backdate` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`pg_created` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`pg_updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`pg_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
I wonder if you have come across this that the id numbers of auto increment don't arrange in correct order when you click on Browse button on phpMyAdmin - is it just me? Something I have set incorrectly in the db table?
For instance, when you insert a series of data into a table and you have deleted some of them as well, so when you check this table on phpMyAdmin, these data should appear in an order like this,
id
2
24
28
296
300
but in most of my tables they don't appear in orders, instead they appear in something like this,
id
24
300
2
296
28
as in this picture below,
Can we do something about it so that the IDs appear in the correct order?
EDIT 1:
I think most of you have misunderstood the issue I raided here. I mean when you click on Browser button on the phpMyAdmin to list all the data in your tables - not when you use SQL query to list the output by using order by
.
Does it make sense?
EDIT 2:
This is one of my table structure - does it help you to see what is wrong in it?
CREATE TABLE IF NOT EXISTS `root_pages` (
`pg_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`pg_url` varchar(255) DEFAULT NULL,
`pg_title` varchar(255) DEFAULT NULL,
`pg_subtitle` varchar(255) DEFAULT NULL,
`pg_description` text,
`pg_introduction` text,
`pg_content_1` text,
`pg_content_2` text,
`pg_content_3` text,
`pg_content_4` text,
`pg_order` varchar(255) DEFAULT NULL,
`pg_hide` varchar(255) DEFAULT '0',
`pg_highlight` varchar(255) DEFAULT '0',
`pg_important` varchar(255) DEFAULT '0',
`pg_parent` varchar(255) DEFAULT '0',
`parent_id` varchar(255) DEFAULT NULL,
`pg_cat_id` varchar(255) DEFAULT NULL COMMENT 'page category id',
`ps_cat_id` varchar(255) DEFAULT NULL COMMENT 'post category id',
`tmp_id` varchar(255) DEFAULT NULL COMMENT 'template id',
`pg_backdate` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`pg_created` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`pg_updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`pg_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以按照自己喜欢的方式对输出进行排序:
ORDER BY id ASC
。表中的记录没有固有的顺序。
You can order your output however you like:
ORDER BY id ASC
.Records in a table have no inherent order.
如果您希望它们按 ID 排序,那么您在选择时必须使用
order by
子句。对于 SQL,
select
返回的集合是无序的,除非您指定它们的顺序。除非您指定,否则您应该假设实际顺序是随机的。实际上,它是任意的而不是随机的,但对表内容的更改可以轻松更改顺序。
If you want them ordered by the ID then you have to use an
order by
clause when selecting.With SQL, the sets returned by
select
are unordered unless you specify what order you want them in.You should assume that the actual order will be random unless you specify it. In reality, it's arbitrary rather than random but changes to the table content can easily change the order.
不确定这是否适用于您,但您的 PK 类型是 INT 吗?如果它是某种 VARCHAR 或 TEXT,它可以像您展示的示例那样排序。你确定那一栏真的是你的PK吗?发布该表的 SHOW CREATE TABLE 输出,它可能会变得更清晰。
以及您使用哪个存储引擎。如果我没记错的话,选择的默认顺序是 MYISAM 的插入顺序和 InnoDB 的 PK 。这也取决于您的 MySql 版本。为了确保在所有情况下您都应该在 select 语句中使用 ORDER BY。
Not sure if this applies to you but is your PK of type INT? If it's some sort of VARCHAR or TEXT it could order like the example you showed. And are you sure that column is actually your PK? Post the SHOW CREATE TABLE output of the table and it might become clearer.
And which storage engine are you using. The default order of a select is insert order for MYISAM and PK for InnoDB if I remember correctly. This would also depend of your version of MySql. To be sure in all circumstances you SHOULD use ORDER BY in your select statement.