# pt-online-schema-change add auto\_increment pk

**URL:** <https://forums.percona.com/t/pt-online-schema-change-add-auto-increment-pk/5804>\
**Category:** Percona Toolkit\
**Created:** [August 9, 2017, 7:58pm UTC](https://forums.percona.com/t/pt-online-schema-change-add-auto-increment-pk/5804 "2017-08-09T19:58:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![RyanE](https://avatars.discourse-cdn.com/v4/letter/r/8e7dd6/32.png) [@RyanE](https://forums.percona.com/u/RyanE)\
**Post date:** [August 9, 2017, 7:58pm UTC](https://forums.percona.com/t/pt-online-schema-change-add-auto-increment-pk/5804/1 "2017-08-09T19:58:53Z")

</div>

Hello,  
I would like to add an auto\_increment PK in an existing table without stopping the service.

The original table looks as below.

CREATE TABLE `service` (  
 `service_id` varchar(40) COLLATE utf8mb4\_bin NOT NULL,  
 `user_id` varchar(10) COLLATE utf8mb4\_bin NOT NULL,  
 `device_id` varchar(64) COLLATE utf8mb4\_bin NOT NULL,  
PRIMARY KEY(`service_id`),  
KEY `idx_service_001`(`user_id`,`device_id`)  
) ENGINE=InnoDB;

And this is the target schema.

CREATE TABLE `service` (  
 `id` int unsigned not null  
 `service_id` varchar(40) COLLATE utf8mb4\_bin NOT NULL,  
 `user_id` varchar(10) COLLATE utf8mb4\_bin NOT NULL,  
 `device_id` varchar(64) COLLATE utf8mb4\_bin NOT NULL,  
PRIMARY KEY(`id`),  
UNIQUE KEY `uk_service_001`(`service_id`),  
KEY `idx_service_001`( `user_id`,`device_id`)  
) ENGINE=InnoDB;

I will first create an unique key as below with MySQL OSC.  
create unique index `uk_service_001` on `service`(`service_id`);

Then add the auto\_increment pk column like ‘drop primary key, add column id int unsigned not null auto\_increment primary key’ with pt-osc.  
Do you think if there is any problem with it?  
I choose pt-osc because MySQL OSC blocks DML while adding auto\_increment column. ( [url][https://dev.mysql.com/doc/refman/5.7/en/innodb-create-index-overview.html[/url]](https://dev.mysql.com/doc/refman/5.7/en/innodb-create-index-overview.html%5B/url%5D) )  
I use MySQL 5.7.16 community.

Best Regards,  
Hyuk Lee

---

<div class="post-metadata">

**Author:** ![carlos.salguero](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.percona.com/carlos.salguero/32/20_2.png) [@carlos.salguero](https://forums.percona.com/u/carlos.salguero)\
**Post date:** [August 10, 2017, 8:10am UTC](https://forums.percona.com/t/pt-online-schema-change-add-auto-increment-pk/5804/2 "2017-08-10T08:10:32Z")

</div>

Hello,

Please read [url][https://bugs.launchpad.net/percona-toolkit/+bug/1709650[/url]](https://bugs.launchpad.net/percona-toolkit/+bug/1709650%5B/url%5D).  
pt-online-schema-change sets NO\_AUTO\_VALUE\_ON\_ZERO and that might affect adding a new AUTO\_INCREMENT index.

Regards

---

<div class="post-metadata">

**Author:** ![RyanE](https://avatars.discourse-cdn.com/v4/letter/r/8e7dd6/32.png) [@RyanE](https://forums.percona.com/u/RyanE)\
**Post date:** [August 10, 2017, 10:39pm UTC](https://forums.percona.com/t/pt-online-schema-change-add-auto-increment-pk/5804/3 "2017-08-10T22:39:45Z")

</div>

Hello,

I appreciate your advice.

Regards
