• mysql 数据库数据订正


    mysql 数据库数据订正

    http://blog.itpub.net/22664653/viewspace-717175/

    工作过程中时常遇到数据订正的需求,该操作本身不难。操作时要求能够保持回滚~对于能够满足回滚的要求,我通常执行备份表,然后执行变更!如果发送订正错误或者用户要求回滚的时候,就完全恢复整个表,这样做会有多余的操作,因为我们只要
    实际上可以选择备份要进行更正的数据!比如如下需求:
    将表resource中usage_type='unused' 并且 user_id=166 的记录更新为user_id=169 !更新100条!
    1 首先备份表
    create table test.resource_20120221 as 
    select * from resource where usage_type='unused' and user_id=166 order by id limit 100;
    2 执行数据订正操作
    update resource set user_id=169 where user_id=166 and id in ( select id from test.resource_20120221);
    如果需要回滚的话,只需执行
    update resource set user_id=166 where id in ( select id from test.resource_20120221);
    即可!
    这里涉及到小批量数据的订正,如果大量的数据,就要使用批量提交的方法了!
     
    附上:
     M-M架构的DDL变更技巧
    l  M-M 复制架构,只有一台提供全部或主数据服务
    推荐DDL语句,在当下M-M架构中属于备用数据库服务器上优先执行,有四项优点:
    1 不立即影响数据库提供的 数据服务;
    2 不阻塞主备之间数据复制的日志恢复操作;
    3 DDL语句执行过程,若出问题只影响备库的稳定性,而很少会影响主库;
    4 若有数据服务无缝切换功能(例如:heartbeat、自主数据层等),可以在备库执行完成后,进行数据服务提供的主备库切换,尤其是数据容量大的表变更时,可以减少对业务影响程度及减少停机维护时间和次数;
     
     


    数据订正前备份表
    将表resource中usage_type='unused' 并且 user_id=166 的记录更新为user_id=169 !更新100条!
    1、首先备份表
    create table test.resource_20120221 as
    select * from resource where usage_type='unused' and user_id=166 order by id limit 100;
    2、执行数据订正操作
    update resource set user_id=169 where user_id=166 and id in ( select id from test.resource_20120221);
    3、如果需要回滚的话,只需执行
    update resource set user_id=166 where id in ( select id from test.resource_20120221);


    如果是GTID不能用CTAS,用insert into select
    1、创建一张同结构空表
    show create table test22G;
    *************************** 1. row ***************************
    Table: test22
    Create Table: CREATE TABLE `test23` (
    `id` int(11) DEFAULT NULL,
    `name` varchar(20) DEFAULT NULL,
    `dy` datetime DEFAULT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8
    2、备份表
    insert into test23
    select * from test22 where name='w' order by id limit 100;
    3、执行数据订正操作
    update test22 set name='p' where name='w' and id in ( select id from test23);
    4、如果需要回滚的话,只需执行
    update test22 set name='w' where id in ( select id from test23);

  • 相关阅读:
    Oracle问题之ORA-12560TNS:协议适配器错误
    调用脚本的方式自动的创建或者是更新oracle数据库自带的Seq序列号的值
    linux在telnet情况下root登录提示login incorrect
    CentOS 7 中 Systemd详解
    CentOS7 下安装telnet服务
    linux 安装telnet命令及使用
    Linux安装telnet
    Linux系统xinetd服务启动不了
    linux服务安装与配置(二):安装xinetd服务
    Linux超级守护进程——xinetd
  • 原文地址:https://www.cnblogs.com/MYSQLZOUQI/p/4045316.html
Copyright © 2020-2023  润新知