**
- - - - - -
```
CREATE TABLE parent(
id INT NOT NULL auto_increment,
`title` varchar(255) NOT NULL default '' comment '名稱',
PRIMARY KEY (id)
) ENGINE = INNODB;
CREATE TABLE child(
id INT NOT NULL auto_increment,
parent_id INT,
PRIMARY KEY (id),
FOREIGN KEY(parent_id) REFERENCES parent(id) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = INNODB;
上面有存在的關聯紀錄行時,會禁止父資料表的刪除或修改動作。
-- 參數:
[ON DELETE {CASCADE | SET NULL | NO ACTION | RESTRICT}]
[ON UPDATE {CASCADE | SET NULL | NO ACTION | RESTRICT}]
當關聯父資料表的主鍵紀錄行被刪除或修改時,InnoDB 對子資料表中紀錄行的處理方式:
CASCADE - 會將有所關聯的紀錄行也會進行刪除或修改。
SET NULL - 會將有所關聯的紀錄行設定成 NULL。
NO ACTION - 有存在的關聯紀錄行時,會禁止父資料表的刪除或修改動作。
RESTRICT - 與 NO ACTION 相同。
詳細的錯誤訊息可以在 MySQL 指令模式下輸入:
SHOW engine innodb status;
由於會列出很多資料,所以要找一下,在訊息中有一组【LATEST FOREIGN KEY ERROR】會有最近錯誤的詳細描述和解决辦法。
```
- - - - - -
XOOPS 與 FOREIGN KEY
-------------------
1. 無法在mysql.sql處理,因為不會自動加前置字元
2. include/onInstall.php ```
#增加客戶資料表外鍵
$sql="ALTER TABLE `" . $xoopsDB->prefix("ugm_stk_customer") . "`
ADD FOREIGN KEY (`staff`) REFERENCES `" . $xoopsDB->prefix("ugm_stk_staff") . "` (`sn`) ON DELETE RESTRICT ON UPDATE RESTRICT";
go_addForeignKey($sql);
#-----------------------
#增加廠商資料表外鍵
$sql="ALTER TABLE `" . $xoopsDB->prefix("ugm_stk_vendor") . "`
ADD FOREIGN KEY (`staff`) REFERENCES `" . $xoopsDB->prefix("ugm_stk_staff") . "` (`sn`) ON DELETE RESTRICT ON UPDATE RESTRICT";
go_addForeignKey($sql);
#-----------------------
```
3. 被當外鍵的資料表,須最後移除,否則無法刪除
xoops\_version.php ```
//---模組資料表架構---//
$modversion['sqlfile']['mysql'] = 'sql/mysql.sql';
$modversion['tables'][1] = 'ugm_stk_files_center';
$modversion['tables'][2] = 'ugm_stk_customer';
$modversion['tables'][3] = 'ugm_stk_vendor';
$modversion['tables'][4] = 'ugm_stk_prod';
$modversion['tables'][5] = 'ugm_stk_kind';
$modversion['tables'][6] = 'ugm_stk_system';
$modversion['tables'][7] = 'ugm_stk_news';
$modversion['tables'][8] = 'ugm_stk_contact';
$modversion['tables'][9] = 'ugm_stk_page';
$modversion['tables'][10] = 'ugm_stk_main_cart';
$modversion['tables'][11] = 'ugm_stk_cart';
$modversion['tables'][12] = 'ugm_stk_receipt';
$modversion['tables'][13] = 'ugm_stk_staff';
$modversion['tables'][14] = 'ugm_stk_main_purchase';
$modversion['tables'][15] = 'ugm_stk_purchase';
$modversion['tables'][16] = 'ugm_stk_main_sales';
$modversion['tables'][17] = 'ugm_stk_sales';
$modversion['tables'][18] = 'ugm_stk_pay';
$modversion['tables'][19] = 'ugm_stk_cancel';
```
4. 範例:
5. 多 對 多 範例
ugm\_cart\_kind ```
CREATE TABLE `ugm_cart_kind` (
`sn` smallint(5) unsigned NOT NULL AUTO_INCREMENT COMMENT 'sn',
`ofsn` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT '父類別',
`kind` varchar(255) NOT NULL DEFAULT 'kind_prod' COMMENT '分類',
`title` varchar(255) NOT NULL DEFAULT '' COMMENT '標題',
`sort` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT '排序',
`enable` enum('1','0') NOT NULL DEFAULT '1' COMMENT '狀態',
`url` varchar(255) NOT NULL DEFAULT '' COMMENT '網址',
`target` enum('1','0') NOT NULL DEFAULT '0' COMMENT '外連',
`col_sn` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'col_sn',
`content` text COMMENT '內容',
`ps` varchar(255) NOT NULL DEFAULT '' COMMENT '備註',
PRIMARY KEY (`sn`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
```
ugm\_cart\_prod ```
CREATE TABLE `ugm_cart_prod` (
`sn` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'prod_sn',
`no` varchar(255) NOT NULL DEFAULT '' COMMENT '編號',
`title` varchar(255) NOT NULL DEFAULT '' COMMENT '名稱',
`summary` text COMMENT '摘要',
`content` text COMMENT '內容',
`enable` enum('1','0') NOT NULL DEFAULT '1' COMMENT '狀態',
`create_date` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '建立日期',
`update_date` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '更新日期',
`sort` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '排序',
`counter` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '人氣',
`icon` varchar(255) NOT NULL DEFAULT '' COMMENT '圖示',
`youtube` varchar(255) NOT NULL DEFAULT '' COMMENT 'youtube',
`uid` mediumint(8) unsigned NOT NULL COMMENT 'uid',
PRIMARY KEY (`sn`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
```
kind2prod
多對多實例:一個類別可以多個商品,一個商品可以多個類別
必須有 key index,且欄位的結構須與關聯資料表欄位一樣 ```
CREATE TABLE `kind2prod` (
`sn` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'kind2prod_sn',
`kind_sn` smallint(5) unsigned NOT NULL COMMENT 'kind_sn',
`prod_sn` int(10) unsigned NOT NULL COMMENT 'prod_sn',
PRIMARY KEY (`sn`),
KEY `kind_sn` (`kind_sn`),
KEY `prod_sn` (`prod_sn`),
CONSTRAINT `kind2prod_ibfk_1` FOREIGN KEY (`kind_sn`) REFERENCES `ugm_cart_kind` (`sn`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `kind2prod_ibfk_2` FOREIGN KEY (`prod_sn`) REFERENCES `ugm_cart_prod` (`sn`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
```
- - - - - -