MySQLå¤é®çº¦æï¼Foreign Key Constraintï¼æ¯å ³ç³»åæ°æ®åºä¸çä¸ç§éè¦çº¦æãæ¬æå°ä»MySQLå¤é®çº¦æçå®ä¹ãç¨éãè¯æ³ã示ä¾çå¤ä¸ªæ¹é¢è¿è¡è¯¦ç»éè¿°ï¼ä½¿è¯»è è½å¤ææ¡è¯¥çº¦æçåºæ¬æä½åå®é åºç¨ã
- ä¸ãMySQLå¤é®çº¦æçå®ä¹åç¨é
1.å®ä¹
å¤é®æ¯ä¸ç§å¯¹æ°æ®å®æ´æ§èµ·ä¿æ¤ä½ç¨ç约æãéè¿å¼å ¥å¤é®çº¦æï¼æ们å¯ä»¥ä¿è¯å ³ç³»æ°æ®åºä¸çä¸¤å¼ è¡¨ä¹é´çæ°æ®ä¸è´æ§ï¼é²æ¢äºä¸å¼ 表ä¸çæ°æ®éæä¿®æ¹å¯¼è´çå ¶ä»è¡¨ä¸æ°æ®çä¸ä¸è´ã
å ·ä½èè¨ï¼å¤é®çº¦æè§å®äºä¸ä¸ªè¡¨ä¸çæ°æ®é¡¹å¿ é¡»æ¯å¦ä¸ä¸ªè¡¨ä¸æ个ç¹å®æ°æ®é¡¹çå¼ç¨ãä¹å°±æ¯è¯´ï¼å¤é®å¼ºå¶çº¦æäºä¸¤å¼ 表ä¹é´çæ°æ®å ³ç³»ï¼ç¡®ä¿äºè¿äºå ³ç³»ä¸ä¼è¢«éæå°ç ´åã
2.ç¨é
éè¿å¤é®çå¼å ¥ï¼æ们å¯ä»¥å®æ以ä¸ä»»æä¸ç§æå¤ç§éæ±ï¼
1ï¼å¯ä»¥ä¸ºä»»æä¸å¼ 表ä¸çæ°æ®é¡¹å»ºç«ä¸ä¸ªå ³èçâç¶é¡¹-å项âå ³ç³»ï¼ä»¥ç¡®ä¿æ°æ®çå®æ´æ§ã
2ï¼å¯ä»¥ä¸ºä»»æä¸å¼ 表ä¸çæ°æ®é¡¹å»ºç«å¤ä¸ªå ³èçâç¶é¡¹-å项âå ³ç³»ï¼ä»¥å»ºç«å¤å¯¹å¤çå ³ç³»ã
3ï¼å¦ææ个表ä¸çä¸ä¸ªæ°æ®é¡¹ç»å®ï¼å¤é®çº¦æå¯ä»¥ç¡®ä¿è¿æ¥å°åç §è¡¨ä¸å¹¶æ£æ¥è¯¥é¡¹æ¯å¦åå¨ï¼ä»èé²æ¢æ æçåç §å ³ç³»ã
4ï¼å¯ä»¥å¸®å©è¯ä¼°è¡¨ä¹é´çä¾èµå ³ç³»ï¼å æ¬è·¨è¡¨çæ´æ°åå é¤æä½çã
- äºãMySQLå¤é®çº¦æçè¯æ³
å¨MySQLä¸ï¼æ们å¯ä»¥éè¿ä»¥ä¸è¯æ³æ¥å建ä¸ä¸ªå¤é®çº¦æï¼
```
ALTER TABLE <table_name>
ADD CONSTRAINT <constraint_name>
FOREIGN KEY (<column_name>) REFERENCES <related_table_name> (<related_column_name>)
```
å ¶ä¸ï¼å个åæ°çå«ä¹å¦ä¸ï¼
1ï¼ALTER TABLE <table_name>ï¼æå®è¦è¢«ä¿®æ¹ç表åã
2ï¼ADD CONSTRAINT <constraint_name>ï¼å£°æ约ææ¡ä»¶çå称ï¼å¯ä»¥æ ¹æ®å®é éè¦å½åï¼ã
3ï¼FOREIGN KEY (<column_name>)ï¼æå®è¯¥çº¦æçåã
4ï¼REFERENCES <related_table_name> (<related_column_name>)ï¼æå®è¯¥çº¦ææ¯ååªå¼ 表çåªä¸ªå段建ç«å ³ç³»çã
- ä¸ãMySQLå¤é®çº¦æç详ç»ç¤ºä¾
ç°å¨ï¼è®©æ们éè¿ä¸ä¸ªæ¡ä¾æ¥å 深对MySQLå¤é®çº¦æçç解ã
å设æ们æä¸¤å¼ è¡¨ï¼å¦ç表ï¼studentï¼å课ç¨è¡¨ï¼courseï¼ï¼åå«åå¨çå¦çå课ç¨çä¿¡æ¯ï¼ä¸è¿ä¸¤å¼ 表ä¹é´å ·æä¸ä¸ª1对å¤çå ³ç³»ï¼å³ä¸ä¸ªå¦çå¯ä»¥æ¥ä¿®å¤ä¸ªè¯¾ç¨ï¼èä¸é¨è¯¾ç¨åªè½è¢«ä¸ä¸ªå¦çéæ©ã
为äºå®ç°ä»¥ä¸éæ±ï¼å¨å建表çæ¶åï¼æ们å¯ä»¥ä½¿ç¨ä»¥ä¸å½ä»¤å建å¦ç表ï¼
```
CREATE TABLE student (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
age INT NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
```
å¨å建课ç¨è¡¨æ¶ï¼æ们å¯ä»¥ä½¿ç¨ä»¥ä¸å½ä»¤å建课ç¨è¡¨ï¼
```
CREATE TABLE course (
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
course_date DATETIME,
student_id INT NOT NULL,
CONSTRAINT fk_student
FOREIGN KEY (student_id) REFERENCES student (id)
ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
```
åå«è§£éä¸ä¸è¿ä¸¤ä¸ªå½ä»¤ï¼
1ï¼å¨å¦ç表çå½ä»¤ä¸ï¼âidâæ¯å¯ä¸çï¼ä¸è½ä¸ºNULLï¼ä¸èªå¨éå¢ï¼ânameâåâageâåæ ·ä¸è½ä¸ºNULLã
2ï¼å¨è¯¾ç¨è¡¨çå½ä»¤ä¸ï¼âidâæ¯å¯ä¸çï¼ä¸è½ä¸ºNULLï¼ä¸èªå¨éå¢ï¼ânameâåâcourse_dateâä¸è½ä¸ºNULLï¼"student_id"åå°ç¨æ¥å¼ç¨"å¦ç"表ä¸ç"id"åã"fk_student"约ææå®äºå¤é®ã
éè¿ä»¥ä¸å½ä»¤ï¼æä»¬å¯¹ä¸¤å¼ è¡¨ä¹é´ç1对å¤å ³ç³»è¿è¡äºå»ºç«åéå¶ï¼å®ç°äºä¸ç°å®ä¸ççæ°æ®å ³èãå¨å ·ä½ä½¿ç¨ä¸ï¼åªéè¦å¨å¦ç表å课ç¨è¡¨çIDåä¸ä½¿ç¨åæ ·çæ°å¼ï¼å³å¯å®ç°æ£ç¡®çå ³èå使ç¨ã
- åãMySQLå¤é®çº¦æçç¸å ³æ³¨æç¹
1.å¤é®çº¦æåªè½å¨InnoDB表ä¸ä½¿ç¨ãå¦æ使ç¨MyISAMå¼æï¼æ¯æ¬¡å建å¤é®çº¦æé½ä¼è¢«å¿½ç¥ã
2.å¦ææ个表ä¸çå¤é®å¨å ¶ä»è¡¨ä¸å·²ç»è¢«ä½¿ç¨ï¼åæ æ³å é¤æ¤è¡¨ä¸çæ°æ®ã
3.建ç«å¤é®çº¦æä¹åï¼å¿ 须确ä¿ä¸¤å¼ 表é½å·²å»ºç«ãå¦æå ¶ä¸ä¸å¼ 表è¿ä¸åå¨ï¼åå¿ é¡»å å建该表ï¼ç¶åæè½å¨å¦ä¸å¼ 表ä¸å»ºç«å¤é®çº¦æã
4.å¤é®çº¦æä¸çåï¼å¤é®ï¼è¡¨å¿ é¡»æ¯é»è®¤ä¸ºInnoDBç±»åç表ã
- äºãæ»ç»
MySQLå¤é®çº¦æå¨å ³ç³»åæ°æ®åºä¸å ·æéè¦çåºç¨ï¼å¯ä»¥å¯¹è¡¨ä¹é´çå ³ç³»èµ·å°æ©å»ºåä¿æ¤ä½ç¨ãæ¬æä»å®ä¹ãç¨éãè¯æ³ã示ä¾å注æç¹çæ¹é¢è¿è¡äºè¯¦ç»è®²è§£ï¼å¸æ读è 们è½æ·±å ¥äºè§£ãææ¡å¹¶å¹¿æ³åºç¨è¯¥æ°æ®åºçº¦æã