MySQL´¥·¢Æ÷
MySQL´¥·¢Æ÷
Ò»¡¢ MySQL´¥·¢Æ÷¸ÅÄî
MySQL5.0ÒÔÉÏ°æ±¾Ö§³Ö´¥·¢Æ÷£¬´¥·¢Æ÷ÊÇÓë±íÓйصÄÃüÃûÊý¾Ý¿â¶ÔÏ󣬵±±í³öÏÖÌض¨Ê¼þʱ£¬½«¼¤»î¸Ã¶ÔÏó¡£
¶þ¡¢ MySQL´¥·¢Æ÷Óï·¨
CREATE TRIGGER trigger_name trigger_time trigger_event
ON tbl_name FOR EACH ROW trigger_stmt
trigger_time£º´¥·¢¶¯×÷ʱ¼ä£¬¿ÉÒÔÊÇBEFORE»òAFTER£¬ÒÔÖ¸Ã÷´¥·¢³ÌÐòÊÇÔÚ¼¤»îËüµÄÓï¾ä֮ǰ»òÖ®ºó´¥·¢¡£BEFORE updateÄÜ·ñÈ¡µ½new.×Ö¶ÎÖµ£¿BEFORE AFTERʹÓÃʱµÄÇø±ð?
BeforeÓëAfterÇø±ð£ºbefore£º(insert¡¢update)¿ÉÒÔ¶Ônew½øÐÐÐ޸ģ¬after²»ÄܶÔnew½øÐÐÐ޸ġ£Á½Õ߶¼²»ÄÜÐÞ¸ÄoldÊý¾Ý¡£
ÔÚ±¾±í²åÈë×Ö¶ÎÖµÐèÒªÓÃnew¼ÆË㣬ֻÄÜÓÃbefore£¬ÔÚ±ðµÄ±íÖмǼ²åÈë³É¹¦¼Ç¼»òͳ¼Æ²åÈëµÄÌõÊýÓÃafter¡£
trigger_event£º¼¤»î´¥·¢³ÌÐòµÄÓï¾äÀàÐÍ¡£
INSERT£º½«ÐÂÐвåÈë±íʱ¼¤»î´¥·¢³ÌÐò£¬ÈçINSERT¡¢LOAD DATAºÍREPLACEÓï¾ä¡£
UPDATE£º¸ü¸ÄijһÐÐʱ¼¤»î´¥·¢³ÌÐò£¬ÈçUPDATEÓï¾ä¡£
DELETE£º´Ó±íÖÐɾ³ýijһÐÐʱ¼¤»î´¥·¢³ÌÐò£¬ÈçDELETEºÍREPLACEÓï¾ä
trigger_stmt£º´¥·¢³ÌÐò¼¤»îʱִÐеÄÓï¾ä¡£Èç¹ûÄã´òËãÖ´Ðжà¸öÓï¾ä£¬¿ÉʹÓÃBEGIN ... END¸´ºÏÓï¾ä½á¹¹¡£
DELIMITER |
CREATE TRIGGER testref BEFORE INSERT ON test1
FOR EACH ROW BEGIN
INSERT INTO test2 SET a2 = NEW.a1;
DELETE from test3 WHERE a3 = NEW.a1;
UPDATE test4 SET b4 = b4 + 1 WHERE a4 = NEW.a1;
END
|
DELIMITER ;
×¢£º
1¡¢ ʹÓñðÃûOLDºÍNEW£¬Äܹ»ÒýÓÃÓë´¥·¢³ÌÐòÏà¹ØµÄ±íÖеÄÁС£OLD.col_nameÔÚ¸üлòɾ³ýËü֮ǰ£¬ÒýÓÃÒÑÓÐÐÐÖеÄ1ÁУ¬ÊÇ·ñ¿ÉÒÔÒý
ÓÃoldÓëtrigger_timeÎ޹أ¬NEW.col_nameÔÚ¸üÐÂËüÖ®ºóÒýÓý«Òª²åÈëµÄÐÂÐеÄ1ÁлòÒÑÓÐÐеÄ1ÁС£
INSERT£ºÖ»¿ÉÒýÓÃNEW¡¢DELETE£ºÖ»¿ÉÒýÓÃOLD¡¢ UPDATE£º¿ÉÒÔÒýÓÃNEW»òOLD¡£
2¡¢¼¤»î´¥·¢³ÌÐòʱ£¬¶ÔÓÚ´¥·¢³ÌÐòÒýÓõÄËùÓÐOLDºÍNEWÁУ¬ÐèÒª¾ßÓÐSELECTȨÏÞ£¬¶ÔÓÚ×÷ΪSET¸³ÖµÄ¿±êµÄËùÓÐNEWÁÐ, ÐèÒª¾ß
ÓÐUPDATEȨÏÞ¡£
3¡¢CREATE TRIGGERÓï
Ïà¹ØÎĵµ£º
MySQL·þÎñÆ÷Æô¶¯ÃüÁîÈçÏ£ºnet start mysql
MySQL·þÎñÆ÷Í£Ö¹ÃüÁîÈçÏ£ºnet stop mysql
MySQL·þÎñÆ÷µÄÁ¬½ÓÃüÁîÈçÏ£º
·½Ê½Ò»£ºmysql -h Ö÷»úÃû -u Óû§Ãû -p Óû§ÃÜÂë
·½Ê½¶þ£ºmysql --host=Ö÷»úÃû --user=Óû§Ãû --password=Óû§ÃÜÂë
ĬÈϵÄÖ÷»úÊÇ£ºlocalhost ......
Á¬½ÓMySQLÊý¾Ý¿âʱ³£»á³öÏÖÈçϵĴíÎóÌáʾ£º
#1251 - Client does not support authentication protocol requested by server; consider upgrading MySQL client
³öÏÖÕâÖÖÎÊÌâÊÇÔÚMySQL4.1ÒÔÉÏ°æ±¾£¬ÔÒòMySQL·þÎñÆ÷°æ±¾Ê¹ÓÃÁËеÄÃÜÂëÑéÖ¤»úÖÆ£¬Æä½â¾ö·½·¨ÈçÏ£º
mysql>;set password for 'Óû§Ãû' @ 'Ö÷»úÃû' = old ......
×î½üÏëÔÚ·þÎñÆ÷ÉÏÊÔÊÔMySQL5.1µÄ·ÖÇø£¬ÓÚÊǾÍÏë×Å×°¸öMySQL5.1£¬¾¹ýÍ´¿àµÄÒ»¶Î¾Àúºó£¬ÖÕÓÚÕÒ¸öÏÂÃæ×°¸öºÜºÃµÄ½Ì³Ì£¬ÌûÉÏÀ´±¸Íü¡£
1¡¢MySQLÃâ°²×°°æ/¶þ½øÖÆ°æÈí¼þ£¬²»ÓñàÒ룬ÏÂÔصØÖ·£º
http://dev.mysql.com/downloads/
Îļþ¸ñʽ£ºMYSQL-VERSION-OS.tar.gz
2¡ ......
°²×°mysql
sudo apt-get install mysql-server #Ö±½Ó×Ô¶¯»ñµÃ¿ÉÓð汾
Ò²¿ÉÒÔÕâÑùд
sudo apt-get install mysql-server-5.0 #°²×°mysql·þÎñÆ÷5.0°æ±¾
°²×°ºó
/etc/init.d/mysql start (stop) ΪÆô¶¯ºÍÍ£Ö¹·þÎñÆ÷
/etc/mysql/ Ö÷ÒªÅäÖÃÎļþËùÔÚλÖà my.cnf
/var/lib/mysql/ ·ÅÖõÄÊÇÊý¾Ý¿â±íÎļþ¼Ð£¬ÕâÀïµÄm ......
ÏëдÕâÑùһƪÎÄÕºܾÃÁË£¬µ«Ê¼ÖÕûÓÐÏÂÊÖ¡£×î½ü°ïͬÊ¿´Á˼¸¸ö²éѯ£¬¶øÇÒ×Ô¼ºÒ²ÔÚ¿¼ÂÇÒ»¸öË÷ÒýϵͳµÄÎÊÌ⣬ËùÒÔ½ñÌì¾Í°ÑÕâ¸öдÁË¡£½éÉÜÒ»ÏÂMySQLµÄË÷Òý»úÖÆ£¬»¹ÓÐһЩMySQL²éѯµÄÓÅ»¯²ßÂÔ¡£±ÉÈ˲ÅÊèѧdz£¬ºÜ¿ÉÄÜ˵µÄ²»¶Ô£¬Çë·¹ýµÄ¸÷λ´óÏÀÅúÆÀÖ¸Õý£¬Ï׳óÁË¡£
------------------- jiajun.org ¹Ù·½·Ö¸îÏß ---------------- ......