(mysql)Foreign keyµÄʹÓÃ
(1)
Íâ¼üµÄʹÓÃ:
Íâ¼üµÄ×÷ÓÃ,Ö÷ÒªÓÐÁ½¸ö:
Ò»¸öÊÇÈÃÊý¾Ý¿â×Ô¼ºÍ¨¹ýÍâ¼üÀ´±£Ö¤Êý¾ÝµÄÍêÕûÐÔºÍÒ»ÖÂÐÔ.
Èç:
ÓбíA,±íBÖÐÒ»×Ö¶ÎÊDZíAijһ×ֶεÄÍâ¼ü.µ±É¾³ý±íA¼Ç¼ʱ,±íBÏàÓ¦¼Ç¼»á×÷ÏàÓ¦´¦Àí(ɾ³ý/»òÉèÖÃÍâ¼üÁÐΪNULL).µ«Èôɾ³ý±íB¼Ç¼,±íAµÄ¼Ç¼,Ôò²»»áÊÜÓ°Ïì!
Ò»¸ö¾ÍÊÇÄܹ»Ôö¼ÓERͼµÄ¿É¶ÁÐÔ
ÓÐЩÈËÈÏΪÍâ¼üµÄ½¨Á¢»á¸ø¿ª·¢Ê±²Ù×÷Êý¾Ý¿â´øÀ´ºÜ´óµÄÂé·³.ÒòΪÊý¾Ý¿âÓÐʱºò»áÓÉÓÚûÓÐͨ¹ýÍâ¼üµÄ¼ì²â¶øʹµÃ¿ª·¢ÈËԱɾ³ý,²åÈë²Ù×÷ʧ°Ü.ËûÃǾõµÃÕâÑùºÜÂé·³
ÆäʵÕâÕýʽÍâ¼üÔÚÇ¿ÖÆÄã±£Ö¤Êý¾ÝµÄÍêÕûÐÔºÍÒ»ÖÂÐÔ.ÕâÊǺÃʶù.
ÀýÈ磺
ÓÐÒ»¸ö»ù´¡Êý¾Ý±í£¬ÓÃÀ´¼Ç¼ÉÌÆ·µÄËùÓÐÐÅÏ¢¡£ÆäËû±í¶¼±£´æÉÌÆ·ID¡£²éѯʱÐèÒªÁ¬±íÀ´²éѯÉÌÆ·µÄÃû³Æ¡£µ¥¾Ý1µÄÉÌÆ·±íÖÐÓÐÉÌÆ·ID×ֶΣ¬µ¥¾Ý2µÄÉÌÆ·±íÖÐÒ²ÓÐÉÌÆ·ID×ֶΡ£Èç¹û²»Ê¹ÓÃÍâ¼üµÄ»°£¬µ±µ¥¾Ý1,2¶¼Ê¹ÓÃÁËÉÌÆ·ID=3µÄÉÌƷʱ£¬Èç¹ûɾ³ýÉÌÆ·±íÖÐID=3µÄ¶ÔÓ¦¼Ç¼ºó£¬Ôٲ鿴µ¥¾Ý1,2µÄʱºò¾Í»á²é²»µ½ÉÌÆ·µÄÃû³Æ¡£
µ±±íºÜÉÙµÄʱºò£¬ÓÐÈËÈÏΪ¿ÉÒÔÔÚ³ÌÐòʵÏÖµÄʱºòÀ´Í¨¹ýд½Å±¾À´±£Ö¤Êý¾ÝµÄÍêÕûÐÔºÍÒ»ÖÂÐÔ¡£Ò²¾ÍÊÇÔÚɾ³ýÉÌÆ·µÄ²Ù×÷µÄʱºòÈ¥¼ì²âµ¥¾Ý1,2ÖÐÊÇ·ñÒѾʹÓÃÁËÉÌÆ·IDΪ3µÄÉÌÆ·¡£µ«Êǵ±ÄãдÍê½Å±¾Ö®ºóϵͳÓÐÔö¼ÓÁËÒ»¸öµ¥¾Ý3 £¬ËûÒ²±£´æÉÌÆ·IDÕÒ¸ö×ֶΡ£Èç¹û²»ÓÃÍâ¼ü£¬Ä㻹ÊÇ»á³öÏֲ鲻µ½ÉÌÆ·Ãû³ÆµÄÇé¿ö¡£Äã×ܲ»ÄÜÿÔö¼ÓÒ»¸öʹÓÃÉÌÆ·IDµÄ×ֶεĵ¥¾Ýʱ¾Í»ØÈ¥ÐÞ¸ÄÄã¼ì²âÉÌÆ·ÊÇ·ñ±»Ê¹ÓõĽű¾°É
ͬʱ£¬ÒýÈëÍâ¼ü»áʹËٶȺÍÐÔÄÜϽµ¡£
(2)
Ìí¼ÓÍâ¼üµÄ¸ñʽ:
ALTER TABLE yourtablename
ADD [CONSTRAINT Íâ¼üÃû] FOREIGN KEY [id] (index_col_name, ...)
REFERENCES tbl_name (index_col_name, ...)
[ON DELETE {CASCADE | SET NULL | NO ACTION | RESTRICT}]
[ON UPDATE {CASCADE | SET NULL | NO ACTION | RESTRICT}]
˵Ã÷:
on delete/on update,ÓÃÓÚ¶¨Òådelete,update²Ù×÷.ÒÔÏÂÊÇupdate,delete²Ù×÷µÄ¸÷ÖÖÔ¼ÊøÀàÐÍ:
CASCADE:
Íâ¼ü±íÖÐÍâ¼ü×Ö¶ÎÖµ»á±»¸üÐÂ,»òËùÔÚµÄÁлᱻɾ³ý.
RESTRICT:
RESTRICTÒ²Ï൱ÓÚno action,¼´²»½øÐÐÈκβÙ×÷.¼´,¾Ü¾ø¸¸±íupdateÍâ¼ü¹ØÁªÁÐ,delete¼Ç¼.
set null:
±»¸¸ÃæµÄÍâ¼ü¹ØÁª×ֶα»update ,deleteʱ,×Ó±íµÄÍâ¼üÁб»ÉèÖÃΪnull.
¶ø¶ÔÓÚinsert,×Ó±íµÄÍâ¼üÁÐÊäÈëµÄÖµ,Ö»ÄÜÊǸ¸±íÍâ¼ü¹ØÁªÁÐÒÑÓеÄÖµ.·
Ïà¹ØÎĵµ£º
Ò»°ãÔÚMYSQLʹÓÃÖÐÎIJéѯ
¶¼ÊÇÓà set NAMES character
character_set_client £¬ÕâÊÇÓû§¸æËßMySQL²éѯÊÇÓõÄʲô×Ö·û¼¯¡£
character_set_connection £¬MySQL½ÓÊܵ½Óû§²éѯºó£¬°´ÕÕcharacter_set_client½«Æäת»¯Îªcharacter_set_connectionÉ趨µÄ×Ö·û¼¯¡£
character_set_results £¬ MySQL½«´æ´¢µÄÊý¾Ýת»»³Écharact ......
×î½üÓÃmysqlдÁ˼¸¸ö¸´ÔÓµÄÓï¾ä£¬¸Ð¾õÔÚ¹ØϵºÜ¸´ÔÓµÄʱºòÏÈÀíÇå¹ØϵÔÙÑ¡ÔñÓõIJéѯ·½·¨ÊǺÜÖØÒªµÄ£¬ÒòΪͬһ¸öÐèÇó¿ÉÄÜÓò»Í¬µÄ·½·¨À´²é³öÀ´£¬µ«ÊǸ´ÔӶȣ¬ÐÔÄܶ¼²»Ò»Ñù¡£
ÏÂÃæ˵һÏÂÄÜʵÏÖ¸´ÔÓ²éѯµÄһЩһ°ã·½·¨£º
Ò».withÓï·¨£¨Ê×ÏÈ˵Ã÷£¬ºÃÏñMysql²»Ö§³ÖÕâ¸öÓï ......
Windows:
1. ÓÃϵͳ¹ÜÀíÔ±µÇ½ϵͳ¡£
2. Í£Ö¹MySQLµÄ·þÎñ¡£
3. ½øÈëÃüÁî´°¿Ú(cmd)£¬È»ºó½øÈëMySQLµÄ°²×°Ä¿Â¼£¬±ÈÈçÎҵݲװĿ¼ÊÇc:\mysql,½øÈëC:\mysql\bin
4. Ìø¹ýȨÏÞ¼ì²éÆô¶¯MySQL£¬
c:\mysql\bin>mysqld-nt --skip-grant-tables
5. ÖØдò¿ªÒ»¸öcmd´°¿Ú£¬½øÈëc:\mysql\b ......
×òÌ죬¹«Ë¾´Ó·þÎñÆ÷ÉϸãÏÂÀ´Ò»¸ö´óµÄÊý¾Ý¿â£¬Îļþ´óСΪ487MB£¬ÓÐͬÊÂ˵½«php.iniµÄÎļþÅäÖÃÖеÄpost_max_size¸ÄΪ±ÈÄãÒªµ¼ÈëµÄÊý¾Ý¿âÎļþ´óһЩµÄ£¬±ÈÈçÏÖÔÚÎÒÒªµÄµ¼ÈëµÄÊý¾Ý¿âÎļþÊÇ487MB£¬ÄÇô¿ÉÒÔ½«´ËÏî¸ü¸ÄΪ£º700MB£¬ÔÙʹÓÃphpMyAdmin¹¤¾ßµÄµ¼È빦ÄÜÀ´ÊµÏÖ£¬µ«ÊÇ£¬ÎÒÊÔÓÃÁË´ËÖÖ·½·¨²»ÄÜ×àЧ£¬È»ºóÓëÓоÑéµÄÅóÓѽ»Á÷£ ......
ÕâÀïÊÇʹÓÃODBCÀ´×ª»»Êý¾Ý¿âµÄ¡£
1.°²×°mysqlÊý¾Ý¿âµÄODBCÇý¶¯£¬mysql¹ÙÍøÓÐÏÂÔØhttp://dev.mysql.com/downloads/connector/odbc/
2.´ò¿ª¿ØÖÆÃæ°å\¹ÜÀí¹¤¾ß\Êý¾ÝÔ´ODBC£¬ÔÚÓû§DSNÖÐÌí¼ÓÒ»¸öMySQL ODBC Êý¾ÝÔ´¡£
3.½Ó×ÅÔÚÏÂÃæµÄ´°ÌåÖÐÊäÈëÊý¾ÝÔ´Ãû³Æ£¬Èçmysqlodbc£»È»ºóÊäÈë·þÎñÆ÷ServerµØÖ·£¨localhost»òÆäËû£©£¬Óû§Us ......