sqlÖÐÍâ¼üÔ¼ÊøÊÇʲôÒâ˼
Íâ¼üÔ¼ÊøÊÇ sql Êý¾Ý¿âÍêÕûÐÔ¹æÔò£¬ÓÃÓÚÈ·±£±íÖеÄÁÐÓëÁíÒ»ÕűíÖеÄÖ÷¼üÁйØÁª¡£Íâ¼üÔ¼Êøͨ¹ýÇ¿ÖÆÒýÓÃÍêÕûÐÔÀ´È·±£Êý¾Ý׼ȷÐÔºÍÒ»ÖÂÐÔ£¬¸ÄÉÆÊý¾Ý½á¹¹£¬²¢ÓÅ»¯ÅÌÎÊÐÔÄÜ¡£¿ÉÒÔʹÓà foreign key ×Ó¾äÔÚ sql Öн¨ÉèÍâ¼üÔ¼Êø£¬Í¨¹ýÖ¸¶¨ÁÐÖµ±ØÐè±£´æÓÚÒýÓñíÖеÄÖ¸¶¨Ö÷¼üÁÐÖÐÀ´ÊÂÇé¡£
SQL ÖеÄÍâ¼üÔ¼Êø
Íâ¼üÔ¼ÊøÊÇʲô£¿
Íâ¼üÔ¼ÊøÊÇÒ»ÖÖÊý¾Ý¿âÍêÕûÐÔ¹æÔò£¬ËüÇ¿ÖÆÔ¼Êø±íÖеÄÁÐÓëÁíÒ»ÕűíÖеÄÖ÷¼üÁÐÏà¹ØÁª¡£Í¨¹ýÇ¿ÖÆÒýÓÃÍêÕûÐÔ£¬Íâ¼üÔ¼ÊøÓÐÖúÓÚÈ·±£Êý¾ÝµÄ׼ȷÐÔºÍÒ»ÖÂÐÔ¡£
Íâ¼üÔ¼ÊøÔõÑùÊÂÇ飿
µ±ÔÚ±íÖнç˵Íâ¼üÔ¼Êøʱ£¬Ëü»áÖ¸¶¨¸ÃÁеÄÖµ±ØÐè±£´æÓÚÁíÒ»ÕűíµÄÖ¸¶¨Ö÷¼üÁÐÖС£ÈôÊÇʵÑéÏò±íÖвåÈë»ò¸üоßÓÐÎÞЧÍâ¼üÖµµÄÐУ¬ÔòÊý¾Ý¿â»áÒý·¢¹ýʧ¡£
Íâ¼üÔ¼ÊøµÄÀûÒ棺
Êý¾ÝÍêÕûÐÔ£ºÍâ¼üÔ¼ÊøÈ·±£Êý¾ÝÔÚ²î±ð±íÖ®¼äµÄÒ»ÖÂÐÔ£¬±ÜÃâÎÞЧÊý¾Ý²åÈë¡£
Êý¾Ý½á¹¹£ºÍâ¼üÔ¼ÊøÃ÷È·½ç˵Á˱íÖ®¼äµÄ¹Øϵ£¬´Ó¶ø¸ÄÉÆÁËÊý¾Ý¿âµÄ½á¹¹¡£
ÐÔÄÜÓÅ»¯£ºÍ¨¹ýʹÓÃÍâ¼üÔ¼Êø£¬Êý¾Ý¿â¿ÉÒÔÓÅ»¯¶ÔÏà¹Ø±íµÄÅÌÎÊ£¬Ìá¸ßÅÌÎÊÐÔÄÜ¡£
ÔõÑù½¨ÉèÍâ¼üÔ¼Êø£¿
ÔÚ SQL ÖУ¬¿ÉÒÔʹÓà FOREIGN KEY ×Ӿ佨ÉèÍâ¼üÔ¼Êø¡£Óï·¨ÈçÏ£º
ALTER TABLE ADD FOREIGN KEY () REFERENCES ()
µÇ¼ºó¸´ÖÆ
ÀýÈ磬Ҫ½«±í ¶©µ¥ ÖÐµÄ Ö÷¹Ë ID ÁÐÓë±í Ö÷¹Ë ÖÐµÄ CustomerID ÁйØÁª£¬¿ÉÒÔʹÓÃÒÔÏÂÍâ¼üÔ¼Êø£º
ALTER TABLE ¶©µ¥ ADD FOREIGN KEY (Ö÷¹Ë ID) REFERENCES Ö÷¹Ë (CustomerID)
µÇ¼ºó¸´ÖÆ
½áÂÛ£º
Íâ¼üÔ¼ÊøÊÇ SQL ÖеÄÖ÷Òª¹¤¾ß£¬ÓÃÓÚÈ·±£Êý¾ÝÍêÕûÐÔ¡¢¸ÄÉÆÊý¾Ý½á¹¹ºÍÓÅ»¯ÅÌÎÊÐÔÄÜ¡£Í¨¹ýÇ¿ÖƱíÖ®¼äµÄÒýÓÃÍêÕûÐÔ£¬Íâ¼üÔ¼ÊøÓÐÖúÓÚά»¤¿É¿¿ÇÒÒ»ÖµÄÊý¾Ý¿âÇéÐΡ£
ÒÔÉϾÍÊÇsqlÖÐÍâ¼üÔ¼ÊøÊÇʲôÒâ˼µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡