服務項目:網(wǎng)站建設、仿站、程序開發(fā)、APP開發(fā)設計、移動網(wǎng)站開發(fā)設計、企業(yè)網(wǎng)站設計、電子商務網(wǎng)站開發(fā)、網(wǎng)站維護、網(wǎng)站推廣、UX/UI 、HTML5、CSS3、JS / Jquery ...
四川浚??萍加邢薰?></a></div>
                    <div   id=四川浚??萍加邢薰? title=
四川???萍加邢薰?(開發(fā)設計官網(wǎng))TEL : 15308000360 / QQ : 38585404

您的位置:首頁 > 技術(shù)經(jīng)驗 > 數(shù)據(jù)庫 > 正文

MySql學習心得之存儲過程
技術(shù)支持服務電話:15308000360 【7x24提供運維服務,解決各類系統(tǒng)/軟硬件疑難技術(shù)問題】

之前總是在MSSQL上寫存儲過程,沒有在MYSQL上寫過,也基本沒有用過,今天需要用到MYSQL,研究了下,把項目的需要的存儲過程寫了一部分,寫一下工作總結(jié)。這里沒有給出數(shù)據(jù)庫結(jié)構(gòu),不討論SQL語句的細節(jié),主要探討存儲過程語法,適合有基礎的人。

先來看段mysql查詢文章回復語句:

#查詢文章回復
-- ----------------------------
-- Procedure structure for `sp_select_reply_article`
-- ----------------------------
DROP PROCEDURE IF EXISTS `sp_select_reply_article`;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_select_reply_article`(IN `ra_id` int,IN `pagefrom` int,IN `pagesize` int)
BEGIN
 #Routine body goes here...
 SET @ra_id = ra_id;
 SET @pagefrom = pagefrom;
 SET @pagesize = pagesize;
 SET @ssra = CONCAT('SELECT * FROM gk_article WHERE id = ? LIMIT ?,?');
 PREPARE sqlquery FROM @ssra;
 EXECUTE sqlquery USING @ra_id,@pagefrom,@pagesize;
END

DELIMITER ;

#技術(shù)點1:MySql5.1不支持LIMIT參數(shù)(MySql5.5就支持了),如果編寫存儲過程時使用LIMIT做變量,那是需要用動態(tài)SQL來構(gòu)建的,而這樣做性能肯定沒有靜態(tài)SQL好。主要代碼如下:

 SET @ssra = CONCAT('SELECT * FROM gk_article WHERE id = ? LIMIT ?,?');
 PREPARE sqlquery FROM @ssra;
 EXECUTE sqlquery USING @ra_id,@pagefrom,@pagesize;
 
#技術(shù)點2:如果同時需要返回受影響行數(shù)需要在語句后面添加語句:ROW_COUNT()函數(shù),兩條語句之間需要“;”分隔。
#更新數(shù)據(jù)
-- ----------------------------
-- Procedure structure for `sp_update_permission`
-- ----------------------------
DROP PROCEDURE IF EXISTS `sp_update_permission`;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_update_permission`(IN `puser_uid` varchar(20),IN `plevel` int,IN `ppower` int)
BEGIN
 #Routine body goes here...

 SET @puser_uid = puser_uid;
 SET @plevel = plevel;
 SET @ppower = ppower;
 UPDATE gk_permission SET `level` = @plevel, power = @ppower WHERE user_uid = CONVERT(@puser_uid USING utf8) COLLATE utf8_unicode_ci;
END

DELIMITER ;

#技術(shù)點3:MySQL進行字符串比較時發(fā)生錯誤(Illegal mix of collations (utf8_unicode_ci,IMPLICIT) and (utf8_general_ci,IMPLICIT) for operation '='),解決方法:將比較等式一邊進行字符串轉(zhuǎn)換,如改為“CONVERT(b.fullCode USING utf8) COLLATE utf8_unicode_ci”,主要代碼如下:

 UPDATE gk_permission SET `level` = @plevel, power = @ppower WHERE user_uid = CONVERT(@puser_uid USING utf8) COLLATE     utf8_unicode_ci;
 
#插入數(shù)據(jù)
-- ----------------------------
-- Procedure structure for `sp_insert_user`
-- ----------------------------
DROP PROCEDURE IF EXISTS `sp_insert_user`;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_insert_user`(IN `uid` varchar(20),IN `upw` varchar(32),IN `name` varchar(20),IN `sex` int,IN `phone` varchar(20),IN `u_id` int,IN `s_id` int,IN `j_id` int)
BEGIN
 #Routine body goes here...
 SET @uid = uid;
 SET @upw = upw;
 SET @uname = uname;
 SET @sex = sex;
 SET @phone = phone;
 #由于外鍵約束,所以添加的外鍵字段需要在對應外鍵所在表有相應數(shù)據(jù)
 SET @u_id = u_id;
 SET @s_id = s_id;
 SET @j_id = j_id;
 SET @verifytime = DATE('0000-00-00');
 INSERT INTO gk_user(uid,upw,uname,sex,phone,u_id,s_id,j_id,verifytime)
       VALUES(@uid,@upw,@uname,@sex,@phone,@u_id,@s_id,@j_id,@verifytime);
 #查詢結(jié)果會自動返回受影響行數(shù)
END

DELIMITER ;
#根據(jù)ID刪除數(shù)據(jù)
-- ----------------------------
-- Procedure structure for `sp_delete_exchange_by_id`
-- ----------------------------
DROP PROCEDURE IF EXISTS `sp_delete_exchange_by_id`;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_delete_exchange_by_id`(IN `eid` int)
BEGIN
 #Routine body goes here...
 SET @eid = eid;
 DELETE FROM gk_exchange WHERE id = @eid;
END

DELIMITER ;
#通過賬號查詢用戶或者管理員
-- ----------------------------
-- Procedure structure for `sp_select_user_by_uid`
-- ----------------------------
DROP PROCEDURE IF EXISTS `sp_select_user_by_uid`;
DELIMITER ;;
CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_select_user_by_uid`(IN `uid` varchar(20),IN `getAdmin` int)
BEGIN
 #Routine body goes here...
 SET @uid = uid;
 #SET @getadmin = getAdmin;
 #查詢管理員
 IF (getAdmin = 1) THEN
SELECT us.*, un.`name`, se.`name`, jo.`name`, pe.`level`, pe.power FROM gk_user AS us, gk_unit AS un, gk_section AS se, gk_jobtitle AS jo, gk_permission AS pe WHERE us.u_id = un.id AND us.s_id = se.id AND us.j_id = jo.id AND us.uid = pe.user_uid AND us.uid = CONVERT(@uid USING utf8) COLLATE utf8_unicode_ci;
 END IF;
 #查詢用戶
 IF (getAdmin = 0) THEN
SELECT us.*, un.`name`, se.`name`, jo.`name` FROM gk_user AS us, gk_unit AS un, gk_section AS se, gk_jobtitle AS jo WHERE us.u_id = un.id AND us.s_id = se.id AND us.j_id = jo.id AND us.uid = CONVERT(@uid USING utf8) COLLATE utf8_unicode_ci;
 END IF;
END

DELIMITER ;

#技術(shù)點4:這個存數(shù)過程需要用到控制語句(if else elseif while loop repeat leave iterate)。

 IF (getAdmin = 1) THEN
#語句…
 END IF;
 

#技術(shù)點5:在傳入?yún)?shù)不匹配的情況下報錯(Column count doesn't match value count at row 1),這個就是細心問題了,詳細檢查參數(shù)吧。

#技術(shù)點6:獲取當前時間的函數(shù):NOW()

#技術(shù)點7:“`”這個符號是反單引號,兩個反單引號夾起來的會被當做變量,一般是在定義字段時遇到關(guān)鍵字沖突的時候會用到。



上一篇:使用sym文件來分布mysql讀寫i/o
下一篇:MySQL數(shù)據(jù)類型和常用字段屬性總結(jié)

相關(guān)熱詞搜索:mysql