1. 程式人生 > >Mysql 資料庫指令碼更改規範

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;
*/

相關推薦

Mysql 資料庫指令碼更改規範

1、SQL檔案每個月形成一個檔案,以日期開頭,便於發版執行(如果檔案不存在建立一個,檔名格式為:201805.sql)。 2、SQL檔案在做資料結構修改前,必須在開頭單獨起一行備註此次修改做的業務操作和日期。 例如: #=========2018/05/20  新增產品

MySql資料庫細節使用規範詳細解讀勝過千行程式碼優化

版權宣告:本文為博主原創文章,未經博主允許不得轉載。    https://blog.csdn.net/kwame211/article/details/76169262 適用場景:併發量大、資料量大的網際網路業務 一、基礎規範 (1)必須使用InnoDB儲存引擎

Xtrabackup定時備份mysql資料庫指令碼

定時備份MySQL資料庫 指令碼內容: #!/bin/bash ## 備份計劃任務 ## ## 每天凌晨1:30一次全量備份 ## 每天間隔1小時一次增量備份 ## 30 1 * * * backup.sh full ## 00 * * * * backup.

MySQL資料庫表設計規範

用盡量少的儲存空間來儲存一個欄位的資料;例如:能使用int就不要使用varchar、char,能用varchar(16)就不要使用varchar(255);預設情況下,InnoDB引擎單一欄位索引的長度最大為767位元組,當使用UTF-8字符集,每一個字元使用3位元組儲存,在text或者varchar型別的欄

linux 使用python3 定時備份mysql資料庫指令碼 修改配置直接可用。親測!

# Time : 2018/12/24 # Author : [email protected]#依賴包 pip install schedule#注意事項#1. linux中 使用os.system()包裹的命令有特殊字元如)(請使用轉義\)\(或'(' ')'#2. 輸入的資料庫密碼 -p+密

Java執行mysql資料庫指令碼

/** * 執行SQL指令碼檔案 * @param ip MySQL資料庫所在伺服器地址IP * @param host 資料庫埠號 * @param userName 進入資料庫所需要的使用者名稱 * @param password 進入資料庫所需要

mysql資料庫的建表語句修改成green plum資料庫中可執行的指令碼

#用來獲取輸入的對應的表名稱的建表語句 #首先輸入需要獲取的mysql或者sql server的資料庫表名稱,到對應的資料庫中抓出建表語句, #然後,將建表語句進行對應的修改,形成可以在pg中可用的語句 #連線mysql資料 import pymysql import sys impor

用python指令碼匯出mysql資料庫查詢結果到Excel表

最近需要導資料的情況太多,總用跳板機上的navicat工具有點效率低,也覺得挺麻煩的(由於跳板機無法連通外網 所以匯出資料檔案還得通過sftp傳到本機)anyway 還是寫個指令碼好了。之前寫過一個shell指令碼做的定時匯出任務,現在試試用python寫下 主要用到的庫有: pymysql -- 連資料庫

Mysql資料庫觸發器呼叫指令碼

一、資料庫觸發器 mysql觸發器trigger 例項詳解 對資料庫觸發器new和old的理解 示例 二、UDF mySql的UDF是什麼 三、安裝執行命令UDF mysql觸發器呼叫外部指令碼(安裝) Mysql安裝udf呼叫系統程式問題解決 四、測試呼

shell指令碼mysql資料庫的備份,並壓縮

db_user="root"db_passwd="xxx"db_host="localhost" databases="xxx-prod" backup_dir="/backup/mysql_backup" backip_test="/home/ttx/app/mysql_backup/today"

SQL指令碼用Navicat匯入MySQL資料庫出現編碼問題

    在用Navicat把sql指令碼匯入MySQL資料庫的時候,系統提示:      這是資料庫編碼格式和匯入的SQL指令碼編碼不一樣,需要修改統一。 解決辦法:可以選擇統一修改成UTF-8 1.開啟SQL檔案,另存為UTF-8

mysql資料庫執行效能檢查指令碼

只針對mysql 5.6.8以上版本select version();use information_schema;#查詢所有資料庫引數show VARIABLES;#查詢資料庫最大連線數show variables like '%max_connections%';#查詢當前資料庫連線數show full

centos6.7 部署seafile服務時候 通過setup-seafile-mysql.sh指令碼建立資料庫時候,編譯不過。

centos 下安裝python imaging PIL 依賴庫 yum install python-devel yum install libjpeg libjpeg-devel zlib zlib-devel freetype freetype-devel l

收集整理mysql資料庫設計規範與原則

1、 資料庫命名規範  採用26個英文字母(區分大小寫)和0-9的自然數(經常不需要)加上下劃線'_'組成;命名簡潔明確(長度不能超過30個字元);例如:user, stat, log, 也可以wifi_user, wifi_stat, wifi_log給資料庫加個字首;除非是備份資料庫可以加0-9的自然數:

Linux Mysql資料庫自動備份指令碼

方便你我他 #!/bin/bash #備份檔案儲存目錄 backupdir=/mnt/bak/dump #時間戳 time=_`date +%Y_%m_%d_%H_%M_%S` #資料庫連線資訊 db_name=資料庫名稱 db_user=資料庫賬戶 db_pass=資料

Mysql資料庫規範設計

先了解一下規範設計的規則吧 1、命名規範 最好不要用數字(雖然它允許) , 也不要使用駝峰命名,使用小寫字母 並且在不同的單詞之間使用下劃線    _   (包括有 資料庫,表,欄位) 2、索引和正規化 最好為每個表建立一個主鍵索引。 正規化瞭解一下 第一正規化:

shell指令碼實現mysql資料庫的增刪改查操作

一、shell指令碼實現mysql操作 通用的shell語句如下: mysql -hHOSTNAME−P{HOSTNAME} -PHOSTNAME−P{PORT} -uUSERNAME−p{USERNAME} -pUSERNAME−p{PASSWORD} -

python3.6指令碼備份mysql資料庫

環境cnetos7.3+python3.6+mysql5.7 安裝python3.6環境 1.安裝IUS軟體源 安裝EPEL依賴 sudo yum install epel-release 2.安裝python3.6 sudo yum install pyth

設定更改root密碼、連線mysqlmysql常用命令、mysql使用者管理、常用sql語句、mysql資料庫備份恢復

一、設定更改root密碼 首次直接使用mysql會提示‘該命令不存在’,原因是還沒有將該命令加入環境變數,如果要使用該命令,需要使用其絕對路徑:/usr/local/mysql/bin/mysql,為了方便,先將其加入系統環境變數: [[email p

MySQL資料庫開發規範50條

所有的資料庫物件名稱必須使用小寫字母並用下劃線分割(MySQL大小敏感,見名知意,最好不超過32字元) 所有的資料庫物件名稱禁止使用MySQL保留關鍵字(如 desc、range、match、delayed 等,請參考 MySQL 官方保留字http://dev.mysql.com/doc/refm