標籤: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 下資料庫升級指令碼的編寫