(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,×Ó±íµÄÍâ¼üÁÐÊäÈëµÄÖµ,Ö»ÄÜÊǸ¸±íÍâ¼ü¹ØÁªÁÐÒÑÓеÄÖµ.·
Ïà¹ØÎĵµ£º
¾ÍÎÒÖªµÀµÄÁ½¸öÇø±ð£º
myISAM ²»Ö§³ÖÊÂÎñ
Innodb Ö§³ÖÊÂÎñ
Èç¹ûÐÞ¸ÄmyISAM ΪInnodb£¬ÐèÒª°ÑÔÀ´µÄÊý¾ÝÏÈdump³öÀ´£¬Çå¿Õ±í£¬ÔÚÖØвåÈë
innodbÔÚ/etc/my.cfgµÄÅäÖÃΪ
innodb_data_home_dir = /var/lib/mysql/
innodb_data_file_path = ibdata1:2000M;ibdata2:10M:autoextend
innodb_log_group_hom ......
¸÷λ¸ßÊֺá£Ð¡µÜѧϰJAVA²»¾Ã£¬È»ºóÔÙÓÃMYSQLÊý¾Ý¿â¡£
ÓÉÓÚÊÇÓñðÈ˵ĵķ¶Àýѧϰ£¬ËùÒÔ³öÏֺܶàÆæ¹ÖµÄÎÊÌâ
ÎÒÓÃ×Ô¼ºµÄMYSQL ·þÎñÆ÷£¬È»ºó°Ñ±ðÈ˵ÄÊý¾Ý¿âµ¼½øÀ´¡£È»ºó³öÏÖ°æ±¾ÎÊÌâ¡£
ÎÊÌâÈçÏÂ
Table './db_librarysys/tb_manager' was created with a different version of MySQL and cannot be read
2010-4-28 22:44 ......
×òÌ죬¹«Ë¾´Ó·þÎñÆ÷ÉϸãÏÂÀ´Ò»¸ö´óµÄÊý¾Ý¿â£¬Îļþ´óСΪ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 ......
Can't open the mysql.plugin table. Please run mysql_upgrade to create it
µ±Åöµ½ÕâÑùµÄ´íÎóÊÇûÓгõʼ»¯mysqlÊý¾Ý¿â,¿ÉÒÔÔËÐÐÈçϽű¾¾Í¿ÉÒÔ½â¾ö
cd /opt/mysql-5.1.46
cd scripts/
./mysql_install_db --user=mysql --datadir=/usr/local/mysql/var/
ÔËÐÐÈçϵĴúÂë¾Í¿ÉÒÔ½â¾öÉÏÊöÎÊÌâ ......