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

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

MySql學(xué)習心得之存儲過(guò)程
技術(shù)支持服務(wù)電話(huà):15308000360 【7x24提供運維服務(wù),解決各類(lèi)系統/軟硬件疑難技術(shù)問(wèn)題】

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

先來(lái)看段mysql查詢(xún)文章回復語(yǔ)句:

#查詢(xún)文章回復
-- ----------------------------
-- 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ù)點(diǎn)1:MySql5.1不支持LIMIT參數(MySql5.5就支持了),如果編寫(xiě)存儲過(guò)程時(shí)使用LIMIT做變量,那是需要用動(dòng)態(tài)SQL來(lái)構建的,而這樣做性能肯定沒(méi)有靜態(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ù)點(diǎn)2:如果同時(shí)需要返回受影響行數需要在語(yǔ)句后面添加語(yǔ)句:ROW_COUNT()函數,兩條語(yǔ)句之間需要“;”分隔。
#更新數據
-- ----------------------------
-- 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ù)點(diǎn)3:MySQL進(jìn)行字符串比較時(shí)發(fā)生錯誤(Illegal mix of collations (utf8_unicode_ci,IMPLICIT) and (utf8_general_ci,IMPLICIT) for operation '='),解決方法:將比較等式一邊進(jì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;
 
#插入數據
-- ----------------------------
-- 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;
 #由于外鍵約束,所以添加的外鍵字段需要在對應外鍵所在表有相應數據
 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);
 #查詢(xún)結果會(huì )自動(dòng)返回受影響行數
END

DELIMITER ;
#根據ID刪除數據
-- ----------------------------
-- 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 ;
#通過(guò)賬號查詢(xún)用戶(hù)或者管理員
-- ----------------------------
-- 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;
 #查詢(xún)管理員
 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;
 #查詢(xún)用戶(hù)
 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ù)點(diǎn)4:這個(gè)存數過(guò)程需要用到控制語(yǔ)句(if else elseif while loop repeat leave iterate)。

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

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

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

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



上一篇:使用sym文件來(lái)分布mysql讀寫(xiě)i/o
下一篇:MySQL數據類(lèi)型和常用字段屬性總結

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