å¨MySQL 3.23.44çæ¬åï¼InnoDBå¼æç±»åç表æ¯æäºå¤é®çº¦æã
å¤é®ç使ç¨æ¡ä»¶ï¼
1.ä¸¤ä¸ªè¡¨å¿ é¡»æ¯InnoDB表ï¼MyISAM表ææ¶ä¸æ¯æå¤é®(æ®è¯´ä»¥åççæ¬æå¯è½æ¯æï¼ä½è³å°ç®åä¸æ¯æ)ï¼
2.å¤é®åå¿ é¡»å»ºç«äºç´¢å¼ï¼MySQL 4.1.2以åççæ¬å¨å»ºç«å¤é®æ¶ä¼èªå¨å建索å¼ï¼ä½å¦æå¨è¾æ©ççæ¬åéè¦æ¾ç¤ºå»ºç«ï¼
3.å¤é®å ³ç³»ç两个表çåå¿ é¡»æ¯æ°æ®ç±»åç¸ä¼¼ï¼ä¹å°±æ¯å¯ä»¥ç¸äºè½¬æ¢ç±»åçåï¼æ¯å¦intåtinyintå¯ä»¥ï¼èintåcharåä¸å¯ä»¥ï¼
å¤é®ç好å¤ï¼å¯ä»¥ä½¿å¾ä¸¤å¼ è¡¨å ³èï¼ä¿è¯æ°æ®çä¸è´æ§åå®ç°ä¸äºçº§èæä½ï¼
å¤é®çå®ä¹è¯æ³ï¼
[CONSTRAINT symbol] FOREIGN KEY [id] (index_col_name, ...)
REFERENCES tbl_name (index_col_name, ...)
[ON DELETE {RESTRICT | CASCADE | SET NULL | NO ACTION | SET DEFAULT}]
[ON UPDATE {RESTRICT | CASCADE | SET NULL | NO ACTION | SET DEFAULT}]
该è¯æ³å¯ä»¥å¨ CREATE TABLE å ALTER TABLE æ¶ä½¿ç¨ï¼å¦æä¸æå®CONSTRAINT symbolï¼MYSQLä¼èªå¨çæä¸ä¸ªååã
ON DELETEãON UPDATE表示äºä»¶è§¦åéå¶ï¼å¯è®¾åæ°ï¼
RESTRICT(éå¶å¤è¡¨ä¸çå¤é®æ¹å¨)
CASCADE(è·éå¤é®æ¹å¨)
SET NULL(设空å¼)
SET DEFAULT(设é»è®¤å¼)
NO ACTION(æ å¨ä½ï¼é»è®¤ç)
æ个ä¾åï¼ç®åæ¼ç¤ºä¸ä¸ä½¿ç¨ï¼ådageåxiaodi两个表ï¼å¤§å¥è¡¨æ¯ä¸»é®ï¼å°å¼è¡¨æ¯å¤é®ï¼
建表ï¼
1
CREATETABLE`dage`Â (2
  `id`int(11)NOTNULLauto_increment,3
  `name`varchar(32)default'',4
PRIMARYKEY(`id`)5
)Â ENGINE=InnoDBDEFAULTCHARSET=latin1ï¼6
7
CREATETABLE`xiaodi`Â (8
  `id`int(11)NOTNULLauto_increment,9
  `dage_id`int(11)defaultNULL,10
  `name`varchar(32)default'',11
PRIMARYKEY(`id`),12
KEY`dage_id`Â (`dage_id`),13
CONSTRAINT`xiaodi_ibfk_1`FOREIGNKEY(`dage_id`)REFERENCES`dage`Â (`id`)14
)Â ENGINE=InnoDBDEFAULTCHARSET=latin1ï¼
æå ¥ä¸ªå¤§å¥ï¼
1
mysql>insertintodage(name)values('éé£æ¹¾');2
Query OK,1row affected (0.01sec)3
mysql>select*fromdage;4
+----+--------+5
|id|name|6
+----+--------+7
|1|éé£æ¹¾|8
+----+--------+9
1rowinset(0.00sec)
æå ¥ä¸ªå°å¼ï¼
1
mysql>insertintoxiaodi(dage_id,name)values(1,'éé£æ¹¾_å°å¼A');2
Query OK,1row affected (0.02sec)3
4
mysql>select*fromxiaodi;5
+----+---------+--------------+6
|id|dage_id|name|7
+----+---------+--------------+8
|1|1|éé£æ¹¾_å°å¼A|9
+----+---------+--------------+
æ大å¥å é¤ï¼
1
mysql>deletefromdagewhereid=1;2
ERROR1451(23000): Cannotdeleteorupdatea parent row: aforeignkeyconstraintfails (`bstar/xiaodi`,CONSTRAINT`xiaodi_ibfk_1`FOREIGNKEY(`dage_id`)REFERENCES`dage` (`id`))
æ示ï¼ä¸è¡åï¼æ约æçï¼å¤§å¥ä¸é¢è¿æå°å¼ï¼å¯ä¸è½æä¸æ们ä¸ç®¡åï¼
æå ¥ä¸ä¸ªæ°çå°å¼ï¼
1
mysql>insertintoxiaodi(dage_id,name)values(2,'æºè§_å°å¼A');2
ERROR1452(23000): Cannotaddorupdatea child row: aforeignkeyconstraintfails (`bstar/xiaodi`,CONSTRAINT`xiaodi_ibfk_1`FOREIGNKEY(`dage_id`)REFERENCES`dage` (`id`))3
æ示ï¼å°åï¼æ³é ååï¼ä½ è¿æ²¡å¤§å¥å¢ï¼
æå¤é®çº¦æå¢å äºä»¶è§¦åéå¶ï¼
1
mysql>showcreatetablexiaodi;2
3
CONSTRAINT`xiaodi_ibfk_1`FOREIGNKEY(`dage_id`)REFERENCES`dage`Â (`id`)4
5
mysql>altertablexiaodidropforeignkeyxiaodi_ibfk_1;6
Query OK,1row affected (0.04sec)7
Records:1Duplicates:0Warnings:8
mysql>altertablexiaodiaddforeignkey(dage_id)referencesdage(id)ondeletecascadeonupdatecascade;9
Query OK,1row affected (0.04sec)10
Records:1Duplicates:0Warnings:0
å次è¯çæ大å¥å äºï¼
1
mysql>deletefromdagewhereid=1;2
Query OK,1row affected (0.01sec)3
4
mysql>select*fromdage;5
Emptyset(0.01sec)6
7
mysql>select*fromxiaodi;8
Emptyset(0.00sec)
å¾ï¼è¿å对åºçå°å¼ä¹æ²¡äºï¼æ²¡åæ³ï¼è°è®©ä½ è·æon delete cascadeäºå¢ï¼
ä¾å说æçåºè¯¥è®æ¸ æ¥äºå§ï¼å ¶ä»åè½å¯¹åºæåèªå·±å®è·µå§ï¼:-)