mysql 下資料庫升級指令碼的編寫

來源:互聯網
上載者:User

標籤:info   exist   procedure   roc   table   power   圖片   alter   view   

1 升級時必須得預存程序

 1 /**/ 2 drop procedure if exists pro_upgrade; 3 DELIMITER // 4 CREATE DEFINER=`root`@`%` PROCEDURE  `pro_upgrade`(  5   exec_boolen    int   , 6   sql_str             VARCHAR(256)   7 ) 8 BEGIN     9     # 執行sql 操作10     IF sql_str <>‘‘ 11     THEN  12       set @sql1 =  sql_str;13       set @bexec = exec_boolen;      14         15         if @bexec = 016         then 17             PREPARE execsql FROM @sql1;  18             EXECUTE execsql ; 19         end if;20     END IF;21 END//22 DELIMITER ;
View Code

 

  這個預存程序主要是校正表欄位是否存在

2 判斷庫是否存在

1 CREATE DATABASE IF NOT EXISTS `fqmanagesysdb` /*!40100 DEFAULT CHARACTER SET utf8 */;2 USE `fqmanagesysdb`;
View Code

 3 判斷表是否存在建立

 1 CREATE TABLE IF NOT EXISTS `userinfo` ( 2   `id` int(11) NOT NULL DEFAULT ‘1‘, 3   `user_name` varchar(255) NOT NULL, 4   `user_pwd` varchar(255) NOT NULL, 5   `user_type` tinyint(4) NOT NULL DEFAULT ‘2‘, 6   `Power` int(11) NOT NULL DEFAULT ‘0‘, 7   `DeptID` int(11) NOT NULL DEFAULT ‘0‘ 8   PRIMARY KEY (`user_name`,`user_type`), 9   KEY `userinfo_user_name_index` (`user_name`)10 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
View Code

4 判斷視圖是否存在的

 1 DROP VIEW IF EXISTS `allcode_view`; 

5 判斷預存程序是否存在

 1 drop procedure if exists procedure_split; 

6 插入初始化資料

1 INSERT INTO `userinfo` (id,user_name,user_pwd,user_type,Power,DeptID)  select ‘1‘, ‘admin‘, ‘admin‘, ‘1‘, ‘0‘, ‘1‘ from DUAL where not exists (select * from userinfo where user_name = ‘admin‘);

7 判斷觸發器是否存在

 1 DROP TRIGGER IF EXISTS `trigger_delete_fucode`; 

8 向表裡添加新的欄位

1 SELECT count(*) FROM  information_schema.COLUMNS  WHERE TABLE_SCHEMA=‘fqmanagesysdb‘ AND table_name=‘userinfo‘ AND COLUMN_NAME=‘BrokerID‘ into @ret;2 call pro_upgrade(@ret,‘alter table userinfo ADD COLUMN BrokerID varchar(255) NOT NULL    ‘);

這裡用到了上面的預存程序,mysql裡面在非預存程序裡面不支援if not exists的判斷

9 其他 

待補充……

mysql 下資料庫升級指令碼的編寫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.