本文介绍了是否有可能在迟到的完整mysql表中添加自动增量主索引列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设该表有近5000000行

Assuming this table with nearly 5 000 000 rows

CREATE TABLE `author2book` (
  `author_id` int(11) NOT NULL,
  `book_id` int(11) NOT NULL,
  KEY `author_id_INDEX` (`author_id`),
  KEY `paper_id_INDEX` (`book_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

是否可以添加主索引列 id autoincrement 为第一名?我期望这样的事情:

is it possible to add a primary index column id with autoincrement as first place? I expect something like this:

CREATE TABLE `author2book` (
  `id` int(11) NOT NULL AUTO_INCREMENT,  <<<<  This is what I try to achieve!
  `author_id` int(11) NOT NULL,
  `book_id` int(11) NOT NULL,
  KEY `author_id_INDEX` (`author_id`),
  KEY `paper_id_INDEX` (`book_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

这可能吗?

编辑:我应该提一下,我想添加一列

I should mention, that I'd like the added column to be populated.

推荐答案

您可以使用ALTER TABLE在一个命令中添加列和索引。即:

You can use ALTER TABLE to add the column and index in one command. i.e.:

ALTER TABLE author2book ADD id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, ADD PRIMARY KEY (id);

请参见以获取更多信息。

See the MySQL docs for ALTER TABLE for more info.

这篇关于是否有可能在迟到的完整mysql表中添加自动增量主索引列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 12:07