一定有你不知道的Mysql-進階篇-必知必會
-
爲什麼要使用存儲過程
答:封裝簡化操作;保證數據的完整性;提高性能,比單獨的SQL語句更快。
注意:如果在命令行中使用存儲過程,一定要使用DELIMITER對其中的;進行更改,如下,DELIMITER //告訴命令行實用程序使用//作爲新的語 句結束分隔符,可以看到標誌存儲過程結束的END定義爲END //而不是END;。
DELIMITER // create procedure propricing() begin select avg(prod_price) as priceavg from products; end // DELIMITER; call propricing(); drop propricing if exists;
-
所有MySQL變量都必須以@開始
call pro(@pricelow,@pricehigh); select @pricelow;
-
不像多數DBMS,MySQL遊標只能用於存儲過程(和函數)。
create producer processor() begin --declare 註釋 declare done boolean default 0; declare o int; --創建遊標 declare ordernumbers cursor for select order_num from orders; --停止條件的設定 declare continue handle for sqlstate '2000' set done=1; --打開遊標 open ordernumbers repeat fetch ordernumbers into o; until done end repeat; --顯式關閉遊標 close ordernumbers end;
-
只有表才支持觸發器,視圖不支持(臨時表也不 支持),並且只支持update,delete,insert操作。
create trigger neworder after insert on orders --每次操作 for each row --new是一個虛擬表 select new.order_num;
-
事務處理用來管理INSERT、UPDATE和 DELETE語句。你不能回退SELECT語句。(這樣做也沒有什麼意 義。)你不能回退CREATE或DROP操作。事務處理塊中可以使用 這兩條語句,但如果你執行回退,它們不會被撤銷。
-
決不能直接使用root賬戶,你可以創建一個賬戶並賦予其權限。
create user ben identified by 'pass'; grant select on table1.* to ben; show grants for ben; set password for ben = Password('123');
-
爲了保證所有數據被寫到磁盤(包括索引 數據),可能需要在進行備份前使用FLUSH TABLES語句。 然後再執行mysqldump語句。
-
Char類型爲定長串類型,必須指定長度,而Varchar爲變長串。如果在你知道無負值的情況下可以使用Unsigned關鍵字,可以有效減少存儲空間。 MySQL中沒有專門存儲貨幣的數據類 型,一般情況下使用DECIMAL(8, 2) 。