MySQL - retrospectively add an identifier that AUTO_INCREMENTs

I have a table with a lot of rows and no id column. I would like to come back and:

  • add id column as PRIMARY KEYusingAUTO_INCREMENT
  • more importantly, retrospectively add an identifier for all existing rows, from the oldest to the newest (there is an "updatetime" column).

Any suggestions?

+3
source share
1 answer

Consider the following example:

CREATE TABLE your_table (some_value int, updatetime datetime);

INSERT INTO your_table VALUES (100, '2010-08-11 12:09:00');
INSERT INTO your_table VALUES (300, '2010-08-11 12:08:00');
INSERT INTO your_table VALUES (200, '2010-08-11 12:07:00');
INSERT INTO your_table VALUES (400, '2010-08-11 12:06:00');
INSERT INTO your_table VALUES (600, '2010-08-11 12:05:00');
INSERT INTO your_table VALUES (500, '2010-08-11 12:04:00');
INSERT INTO your_table VALUES (800, '2010-08-11 12:03:00');

First we can add a column id:

ALTER TABLE your_table ADD id int unsigned;

Now the table looks like this:

SELECT * FROM your_table;
+------------+---------------------+------+
| some_value | updatetime          | id   |
+------------+---------------------+------+
|        100 | 2010-08-11 12:09:00 | NULL |
|        300 | 2010-08-11 12:08:00 | NULL |
|        200 | 2010-08-11 12:07:00 | NULL |
|        400 | 2010-08-11 12:06:00 | NULL |
|        600 | 2010-08-11 12:05:00 | NULL |
|        500 | 2010-08-11 12:04:00 | NULL |
|        800 | 2010-08-11 12:03:00 | NULL |
+------------+---------------------+------+
7 rows in set (0.00 sec)

Then we can have a UPDATEcolumn idwith a row number when the result set is ordered by a column updatetime:

SET @row_number := 0;

UPDATE    your_table
SET       your_table.id = (@row_number := @row_number + 1)
ORDER BY  your_table.updatetime;

Now the table looks like this:

SELECT * FROM your_table ORDER BY id;
+------------+---------------------+----+
| some_value | updatetime          | id |
+------------+---------------------+----+
|        800 | 2010-08-11 12:03:00 |  1 |
|        500 | 2010-08-11 12:04:00 |  2 |
|        600 | 2010-08-11 12:05:00 |  3 |
|        400 | 2010-08-11 12:06:00 |  4 |
|        200 | 2010-08-11 12:07:00 |  5 |
|        300 | 2010-08-11 12:08:00 |  6 |
|        100 | 2010-08-11 12:09:00 |  7 |
+------------+---------------------+----+

id NOT NULL AUTO_INCREMENT:

ALTER TABLE your_table 
MODIFY id int unsigned NOT NULL AUTO_INCREMENT, ADD PRIMARY KEY (id);

:

DESCRIBE your_table;
+------------+------------------+------+-----+---------+----------------+
| Field      | Type             | Null | Key | Default | Extra          |
+------------+------------------+------+-----+---------+----------------+
| some_value | int(11)          | YES  |     | NULL    |                |
| updatetime | datetime         | YES  |     | NULL    |                |
| id         | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
+------------+------------------+------+-----+---------+----------------+
3 rows in set (0.04 sec)

INSERT , , AUTO_INCREMENT :

INSERT INTO your_table (some_value, updatetime)
VALUES (900, '2010-08-11 12:10:00');

SELECT * FROM your_table ORDER BY id;
+------------+---------------------+----+
| some_value | updatetime          | id |
+------------+---------------------+----+
|        800 | 2010-08-11 12:03:00 |  1 |
|        500 | 2010-08-11 12:04:00 |  2 |
|        600 | 2010-08-11 12:05:00 |  3 |
|        400 | 2010-08-11 12:06:00 |  4 |
|        200 | 2010-08-11 12:07:00 |  5 |
|        300 | 2010-08-11 12:08:00 |  6 |
|        100 | 2010-08-11 12:09:00 |  7 |
|        900 | 2010-08-11 12:10:00 |  8 |
+------------+---------------------+----+
8 rows in set (0.00 sec)

, , , , .

+2

Source: https://habr.com/ru/post/1759163/


All Articles