久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

Mysql中外鍵使用注意事項(xiàng)有哪些

146次閱讀
沒有評論

共計(jì) 3343 個(gè)字符,預(yù)計(jì)需要花費(fèi) 9 分鐘才能閱讀完成。

這篇文章主要介紹了 Mysql 中外鍵使用注意事項(xiàng)有哪些,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓丸趣 TV 小編帶著大家一起了解一下。

外鍵,F(xiàn)OREIGN KEY, 這個(gè)東東,作為 DBA,在 Oracle 我們都不建議在數(shù)據(jù)庫級別去實(shí)現(xiàn)約束,因?yàn)樗木S護(hù)成本很高,
  比如你要保證索引,導(dǎo)入數(shù)據(jù)時(shí)你得保證先后順序等,所以我們更推薦由應(yīng)用去控制邏輯。

在 MYSQL 中是更不推薦使用,不過在這里主要是說說使用過程中要注意的問題。[@more@]## 建立約束,注意命名規(guī)范 FK1,F(xiàn)K2,F(xiàn)K3 … 如果不指定約束名,系統(tǒng)會(huì)自動(dòng)創(chuàng)建一個(gè)。
create table … …
constraint `FK1` foreign key (`user_id`) REFERENCES  `user`(`id`)
ON DELETE CASCADE ON UPDATE CASCADE

## 相應(yīng)的字段(foreign key and the referenced key),
  Corresponding columns in the foreign key and the referenced key
    必須具有相同的內(nèi)部數(shù)據(jù)類型;
       must have similar internal data types inside InnoDB so that they can be compared without a type conversion.
    整型字段的數(shù)據(jù)長度必須一樣;
       The size and sign of integer types must be the same.
    字符的長度可以不一樣;
       The length of string types need not be the same. For non-binary (character) string columns
    非二進(jìn)制字符字段,the character set and collation 也必須一樣;
       For non-binary (character) string columns, the character set and collation must be the same.

## 如果一個(gè) INNODB 表有外鍵,那么他將不能直接轉(zhuǎn)變存儲(chǔ)引擎,除非把外鍵給刪除了。
if an InnoDB table has foreign key constraints, ALTER TABLE cannot be used to change the table to use another storage engine. To alter the storage engine, you must drop any foreign key constraints first

 =========================================================================
  root@127.0.0.1 : test 12:21:05 alter table audit engine=myisam;
  ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails
  root@127.0.0.1 : test 12:21:06
 
 
  root@127.0.0.1 : test 12:25:40 alter table audit drop foreign key FK1;
  Query OK, 0 rows affected (0.18 sec)
  Records: 0  Duplicates: 0  Warnings: 0
 
  root@127.0.0.1 : test 12:25:46 alter table audit engine=myisam;
  Query OK, 0 rows affected (0.10 sec)
  Records: 0  Duplicates: 0  Warnings: 0
 =========================================================================

## set FOREIGN_KEY_CHECKS = 0.
  可以讓表不按依賴關(guān)系導(dǎo)入;mysqldump 就是這么做的。
  This avoids problems with tables having to be reloaded in a particular order when the dump is reloaded
   
## 刪除約束,請指定正確的約束名
 
   create table user (id int ,username varchar(20) , primary key (id) ) engine=innodb ;
   
   create table audit (id int ,user_id int ,  primary key (id) ,
   constraint  foreign key (`user_id`) REFERENCES  `user`(`id`) ON DELETE CASCADE ON UPDATE CASCADE
   ) engine=innodb ;
   
   insert into user values (1, heyf     insert into audit values (1,1);
   
   =========================================================================
   root@127.0.0.1 : test 11:00:19 alter table audit drop FOREIGN KEY user_id ;
   ERROR 1025 (HY000): Error on rename of ./test/audit to ./test/#sql2-4847-c (errno: 152)
   
   ###### 這里為什么會(huì)報(bào)錯(cuò)呢??
   
   root@127.0.0.1 : test 11:00:19  show innodb status G
   
   LATEST FOREIGN KEY ERROR
   ————————
   100202 11:00:30 Error in dropping of a foreign key constraint of table test/audit,
   in SQL command
   alter table audit drop FOREIGN KEY user_id
   Cannot find a constraint with the given id user_id.
   
   ###### 系統(tǒng)提示說:你指定了一個(gè)錯(cuò)誤的 CONSTRAINT_NAME
   
   root@127.0.0.1 : test 11:57:02 show create table audit G
   *************************** 1. row ***************************
          Table: audit
   Create Table: CREATE TABLE `audit` (
     `id` int(11) NOT NULL default 0 ,
     `user_id` int(11) default NULL,
     PRIMARY KEY  (`id`),
     KEY `user_id` (`user_id`),
     CONSTRAINT `audit_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
   ) ENGINE=InnoDB DEFAULT CHARSET=utf8
   1 row in set (0.00 sec)
   
   ##### 我們看到系統(tǒng)自動(dòng)產(chǎn)生的外鍵名字不是簡單的字段名。
   
   root@127.0.0.1 : test 11:54:26 alter table audit drop FOREIGN KEY `audit_ibfk_1`;
   Query OK, 1 row affected (0.21 sec)
   Records: 1  Duplicates: 0  Warnings: 0
   

感謝你能夠認(rèn)真閱讀完這篇文章,希望丸趣 TV 小編分享的“Mysql 中外鍵使用注意事項(xiàng)有哪些”這篇文章對大家有幫助,同時(shí)也希望大家多多支持丸趣 TV,關(guān)注丸趣 TV 行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!

正文完
 
丸趣
版權(quán)聲明:本站原創(chuàng)文章,由 丸趣 2023-07-26發(fā)表,共計(jì)3343字。
轉(zhuǎn)載說明:除特殊說明外本站除技術(shù)相關(guān)以外文章皆由網(wǎng)絡(luò)搜集發(fā)布,轉(zhuǎn)載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 休宁县| 潜江市| 濮阳市| 五家渠市| 潮州市| 合水县| 岳西县| 上栗县| 乌苏市| 岱山县| 叶城县| 大埔县| 红河县| 万源市| 文成县| 东兰县| 海门市| 自贡市| 中卫市| 泰顺县| 东海县| 邓州市| 潞西市| 泽普县| 晴隆县| 绥阳县| 肃北| 油尖旺区| 博野县| 清原| 且末县| 万山特区| 新宁县| 桐柏县| 天津市| 浦东新区| 花莲市| 连州市| 镇赉县| 兴义市| 穆棱市|