在线精品99_中国九九盗摄偷拍偷看_91免费版在线观看_91.app_91高清视频在线_99热最新网站

Mysql中外键使用注意事项有哪些

105次阅读
没有评论

共计 3343 个字符,预计需要花费 9 分钟才能阅读完成。

这篇文章主要介绍了 Mysql 中外键使用注意事项有哪些,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让丸趣 TV 小编带着大家一起了解一下。

外键,FOREIGN KEY, 这个东东,作为 DBA,在 Oracle 我们都不建议在数据库级别去实现约束,因为他的维护成本很高,
  比如你要保证索引,导入数据时你得保证先后顺序等,所以我们更推荐由应用去控制逻辑。

在 MYSQL 中是更不推荐使用,不过在这里主要是说说使用过程中要注意的问题。[@more@]## 建立约束,注意命名规范 FK1,FK2,FK3 … 如果不指定约束名,系统会自动创建一个。
create table … …
constraint `FK1` foreign key (`user_id`) REFERENCES  `user`(`id`)
ON DELETE CASCADE ON UPDATE CASCADE

## 相应的字段(foreign key and the referenced key),
  Corresponding columns in the foreign key and the referenced key
    必须具有相同的内部数据类型;
       must have similar internal data types inside InnoDB so that they can be compared without a type conversion.
    整型字段的数据长度必须一样;
       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
    非二进制字符字段,the character set and collation 也必须一样;
       For non-binary (character) string columns, the character set and collation must be the same.

## 如果一个 INNODB 表有外键,那么他将不能直接转变存储引擎,除非把外键给删除了。
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

 =========================================================================
  [email protected] : test 12:21:05 alter table audit engine=myisam;
  ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails
  [email protected] : test 12:21:06
 
 
  [email protected] : 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
 
  [email protected] : 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.
  可以让表不按依赖关系导入;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);
   
   =========================================================================
   [email protected] : 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)
   
   ###### 这里为什么会报错呢??
   
   [email protected] : 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.
   
   ###### 系统提示说:你指定了一个错误的 CONSTRAINT_NAME
   
   [email protected] : 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)
   
   ##### 我们看到系统自动产生的外键名字不是简单的字段名。
   
   [email protected] : 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
   

感谢你能够认真阅读完这篇文章,希望丸趣 TV 小编分享的“Mysql 中外键使用注意事项有哪些”这篇文章对大家有帮助,同时也希望大家多多支持丸趣 TV,关注丸趣 TV 行业资讯频道,更多相关知识等着你来学习!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-07-26发表,共计3343字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)
主站蜘蛛池模板: 国产精品无码aⅴ嫩草 | 久久久久久久久影院 | 欧美一区二区三区四区五区六区 | 亚洲一级电影 | 91精品国产人成网站 | 无码福利日韩神码福利片 | 中文综合在线观 | 国产一精品一av一免费 | 亚洲肥老太bbw中国熟女 | 亚洲男人的天堂久久精品 | 国产成人亚洲综合无码 | 亚洲欧美中文v日韩v在线 | 国产精品久久久久久小说 | 国产精品无码av天天爽播放器 | 亚洲av无码乱码在线观看富二代 | 无码精品黑人一区二区三区 | 亚洲午夜理论片在线观看 | 最近2019中文字幕一页二页 | 中文亚洲av片在线观看 | 放荡的少妇2欧美版 | 国产精品午夜性视频 | 亚洲高清国产一区二区三区 | 亚洲在线久久 | 亚洲综合激情五月色播 | 黄漫在线免费观看 | 色在线观看视频 | 国产成人精品日本亚洲网址 | 亚洲夜色噜噜av在线观看 | 国产在线综合一区二区三区 | a免费在线观看视频 | 久久精品国产自在一线 | 24小时日本在线www免费的 | 久久黄色免费视频 | 日本三级在线电影 | 青青在线成人免费视频 | 四虎影视在线永久免费观看 | 久久久久久综合对白国产 | 91国内精品久久久久免费影院 | 午夜免费啪视频 | 亚洲真人无码永久在线观看 | 成人黄色一级片 |