业务运行一段时间,发现原来的主键设置并不合理,这个时候,想变更主键。这种需求在实际生产中还是蛮多的。

下面,看看pt-online-schema-change解决这类问题的处理方式。

首先,创建一张测试表

create table t2(c1 int primary key, c2 int);

构造测试数据

delimiter //
create procedure p1()
begin
  ;
  ;
   do
    );
    ;
      then
      commit;
    end if;
  end while;
end //
delimiter ;
call p1;

下面,开始使用pt-online-schema-change对t2表进行主键变更

1. 对c1列加上unique key

# pt-online-schema-change --execute --alter "modify c1 int unique key"  --print D=test,t=t2 

此时,t2表的表结构如下:

mysql> show create table t2\G
. row ***************************
       Table: t2
Create Table: CREATE TABLE `t2` (
  `c1` ) ',
  `c2` ) DEFAULT NULL,
  PRIMARY KEY (`c1`),
  UNIQUE KEY `c1` (`c1`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
 row in set (0.03 sec)

2. 删除c1列上的主键

# pt-online-schema-change --execute --alter "drop primary key"  --no-check-alter --print D=test,t=t2

注意:删除主键需加上 --no-check-alter选项

此时,t2的表结构如下:

mysql> show create table t2\G
. row ***************************
       Table: t2
Create Table: CREATE TABLE `t2` (
  `c1` ) ',
  `c2` ) DEFAULT NULL,
  UNIQUE KEY `c1` (`c1`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
 row in set (0.05 sec)

3. 添加c2列上的主键

# pt-online-schema-change --execute --alter "modify c2 int primary key" --print D=test,t=t2

此时,t2的表结构如下:

mysql> show create table t2\G
. row ***************************
       Table: t2
Create Table: CREATE TABLE `t2` (
  `c1` ) ',
  `c2` ) NOT NULL,
  PRIMARY KEY (`c2`),
  UNIQUE KEY `c1` (`c1`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
 row in set (0.02 sec)

4. 删除c1列上的unique key

# pt-online-schema-change --execute --alter "drop key c1"  --print D=test,t=t2

此时,t2的主键变更完成

mysql> show create table t2\G
. row ***************************
       Table: t2
Create Table: CREATE TABLE `t2` (
  `c1` ) ',
  `c2` ) NOT NULL,
  PRIMARY KEY (`c2`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
 row in set (0.02 sec)

如何利用pt-online-schema-change进行MySQL表的主键变更的更多相关文章

  1. PHP查询MYSQL表的主键

    $sql = "SELECT * from Person"; $result = mysql_query($sql,$con); while ($property = mysql_ ...

  2. sqoop从hive导入数据到mysql时出现主键冲突

    今天在将一个hive数仓表导出到mysql数据库时出现进度条一直维持在95%一段时间后提示失败的情况,搞了好久才解决.使用的环境是HUE中的Oozie的workflow任何调用sqoop命令,该死的o ...

  3. Online Schema Change for MySQL

    It is great to be able to build small utilities on top of an excellent RDBMS. Thank you MySQL. This ...

  4. mysql数据库表操作-表的主键索引和普通索引

    数据库索引就象书的目录一样,如果在字段上建立了索引,那么以索引列为查询条件时可以加快查询数据的速度.查询数据库,按主键查询是最快的,每个表只能有一个主键列,但是可以有多个普通索引列,主键列要求列的所有 ...

  5. MySQL基础2-创建表和主键约束

    1.创建表 在操作数据表之前,应该使用"USE 数据库名"指定操作是在哪个数据库中进行 主键约束(唯一标识) ****非空*** ****唯一*** ****被引用****(学习外 ...

  6. Mysql,SqlServer,Oracle主键自动增长的设置

    1.把主键定义为自动增长标识符类型 MySql 在mysql中,如果把表的主键设为auto_increment类型,数据库就会自动为主键赋值.例如: )); insert into customers ...

  7. MySQL与Oracle主键Query性能测试结果

    测试结果总结如下: 1. 按主键读:SQL形式:SELECT * FROM table WHERE id=?. 1.1. 主键为数字.如果所有ID均不存在,纯比较SQL解析能力.MySQL解析SQL的 ...

  8. Mysql 创建联合主键

    Mysql 创建联合主键2008年01月11日 星期五 下午 5:21使用primary key (fieldlist)      比如:   create table mytable (       ...

  9. mysql insert插入时实现如果数据表中主键重复则更新,没有重复则插入的四种方法

    [CSDN下载] Powerdesigner 设计主键code不能重复等问题 [CSDN博客] Oracle中用一个序列给两个表创建主键自增功能的后果 [CSDN博客] MySQL自增主键删除后重复问 ...

随机推荐

  1. 先说IEnumerable,我们每天用的foreach你真的懂它吗?

    我们先思考几个问题: 为什么在foreach中不能修改item的值? 要实现foreach需要满足什么条件? 为什么Linq to Object中要返回IEnumerable? 接下来,先开始我们的正 ...

  2. Entity Framework 6 Recipes 2nd Edition 译 -> 目录 -持续更新

    因为看了<Entity Framework 6 Recipes 2nd Edition>这本书前面8章的翻译,感谢china_fucan. 从第九章开始,我是边看边译的,没有通读,加之英语 ...

  3. X86和X86_64和X64有什么区别?

    x86是指intel的开发的一种32位指令集,从386开始时代开始的,一直沿用至今,是一种cisc指令集,所有intel早期的cpu,amd早期的cpu都支持这种指令集,ntel官方文档里面称为&qu ...

  4. Maven 整合FreeMarker使用

    pom.xml <!-- freemarker jar --> <dependency> <groupId>org.freemarker</groupId&g ...

  5. 设计模式C#合集--工厂方法模式

    简单工厂,代码: public interface ISpeak { public void Say(); } public class Hello : ISpeak { public void Sa ...

  6. kali linux下的arp攻击

    这是我第一篇博客,写的不好请谅解 ____________________________(分割线)_______________________________ 在kali linux系统下自带工具 ...

  7. Linux根文件系统分析之init和busybox

    Hi,大家好!我是CrazyCatJack.今天给大家讲解Linux根文件系统的init进程和busybox的配置及编译. 先简单介绍一下,作为一个嵌入式系统,要想在硬件上正常使用的话.它的软件组成大 ...

  8. #26 fibonacci seqs

    Difficulty: Easy Topic: Fibonacci seqs Write a function which returns the first X fibonacci numbers. ...

  9. SpringMvc中的数据校验

    SpringMvc中的数据校验 Hibernate校验框架中提供了很多注解的校验,如下: 注解 运行时检查 @AssertFalse 被注解的元素必须为false @AssertTrue 被注解的元素 ...

  10. Selenium-java-获取当前时间

    1 获取当前时间 // 获取当前时分秒 Calendar now = Calendar.getInstance(); int is = now.get(Calendar.HOUR_OF_DAY); i ...