Mysql 数据库脚本更改规范

1、SQL文件每个月形成一个文件,以日期开头,便于发版执行(如果文件不存在创建一个,文件名格式为:201805.sql)。

2、SQL文件在做数据结构修改前,必须在开头单独起一行备注此次修改做的业务操作和日期。


例如: #=========2018/05/20  添加产品优惠券日期字段,jira #5247 ============。


3、SQL文件在做数据结构操作前,必须判断是否已经存在相应(字段,表,索引)等是否存在,然后再操作,保证幂等性。

4、数据结构更改需单独提交PR修改SQL文件(此PR只包含SQL文件修改),并注明关联的Jira issue,PR内容需注明SQL更改的内容,由指定人员审核通过后方能合并。

相关示例:

/*========== 创建操作日志表,操作日志表用来处理“添加修改数据”这种操作来保证幂等性使用======================*/
    create table if not exists sqloperate_log
    (
        Id varchar(50) not null
            primary key,
        Descriptions varchar(100) null,
        DateTime timestamp default CURRENT_TIMESTAMP not null,
        constraint uniq_Id
            unique (Id)
    );

/*==============示例,添加表=====================*/
    create table if not exists sqloperate_log
    (
        Id varchar(50) not null
    );

/*==============示例,添加列(在需要新增列的时候,使用过如下方式进行新增)=====================*/
    drop procedure if exists schema_change;

    delimiter ';;'    
    create procedure schema_change() begin

    /*add first column for sqloperate_log table */
    if not exists (select * from information_schema.columns where table_name = 'sqloperate_log' and column_name = 'test') then
        /*this is your sql scripts*/
        alter table sqloperate_log add column `test` varchar(255) NULL;      
    end if;


    if not exists (select * from information_schema.columns where table_name = 'sqloperate_log' and column_name = 'test2') then
        alter table sqloperate_log add column `test2` varchar(255) NULL;      
    end if;

    end;;

    delimiter ';'
    call schema_change();

    drop procedure if exists schema_change;

/*==============示例,添加索引(如果需要对表中的列添加索引,使用下面的示例脚本)=====================*/
    drop procedure if exists schema_change;

    delimiter ';;'    
    create procedure schema_change() begin

    if not exists (select * from information_schema.statistics where table_name = 'sqloperate_log' and index_name = 'descriptions_index') then
        /*this is your sql scripts*/
        create index descriptions_index on sqloperate_log(Descriptions);
    end if;

    end;;

    delimiter ';'
    call schema_change();

    drop procedure if exists schema_change;


/*==============示例,添加修改数据(在需要进行一些脚本更改数据或者初始化数据的时候,使用下面的脚本示例。其中title为当前的操作的描述)=====================*/
    drop procedure if exists sqldata_change;

    delimiter ';;'    
    create procedure sqldata_change() begin

    /*this is your sql scripts descriptions*/
    set @title = '向表appinfo中更新数据-示例';

    if not exists (select * from sqloperate_log where Id = sha1(@title)) then
        /*this is your sql scripts*/
        insert into appinfo(PhoneName,CreateTime) values('test phone name',now()); 

        /*add operate logs, don't delete*/
        insert into sqloperate_log(Id,Descriptions) values(sha1(@title),@title);
    end if;

    end;;

    delimiter ';'
    call sqldata_change();
    drop procedure if exists sqldata_change;


/*============清理示例数据==========================*/
 /*   alter table sqloperate_log drop column `test`;
    alter table sqloperate_log drop column `test2`;
    drop index `descriptions_index` on sqloperate_log;
    truncate table sqloperate_log;
    drop table sqloperate_log;
*/

 

 

 

你有困难我帮忙,我住隔壁我姓王。----------------- 你隔壁的老王宣。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

胡老汉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值