innodb === ###### tags: `mysql` `innodb` --- ```sql= 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 ```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 ```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. 範例 https://hk.saowen.com/a/08efbf37f380bad0f5888a79bd08b5b52325313e89a099aee5d98e49e5cd0c43
×
Sign in
Email
Password
Forgot password
or
Sign in via Google
Sign in via Facebook
Sign in via X(Twitter)
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
Continue with a different method
New to HackMD?
Sign up
By signing in, you agree to our
terms of service
.