外键,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

=========================================================================

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.

可以让表不按依赖关系导入;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)

###### 这里为什么会报错呢??

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.

###### 系统提示说:你指定了一个错误的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)

##### 我们看到系统自动产生的外键名字不是简单的字段名。

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

=========================================================================

## 删除约束后,索引不会被自动删除,如果有必要,请手动删除

alter table Table_name drop index Index_name;

mysql set foreign key_Mysql 外键(FOREIGN KEY)使用注意事项相关推荐

  1. mysql foreign key_MYSQL外键(Foreign Key)的使用

    在MySQL 3.23.44版本后,InnoDB引擎类型的表支持了外键约束. 外键的使用条件: 1.两个表必须是InnoDB表,MyISAM表暂时不支持外键(据说以后的版本有可能支持,但至少目前不支持 ...

  2. 【MySQL】解决MySQL删除外键(foreign key)时报错问题

    [MySQL]解决MySQL删除外键(foreign key)时报错问题 文章目录 [MySQL]解决MySQL删除外键(foreign key)时报错问题 一.现象 二.分析 三.问题解决 四.拓展 ...

  3. 数据库六大约束用法:主键(primary key)、外键(foreign key)、非空(not null)、默认(default)、检查(check)、唯一(unique)

    1. 数据库有六大约束 主键(primary key) 外键(foreign key):被参照的键必须有唯一约束或是主键 非空(not null) 默认(default) 检查(check):orac ...

  4. MySQL数据库(25):外键 foreing key

    外键 foreing key 外键表示了两个实体之间的联系 外键 foreing key: A表中的一个字段的值指向另B表的主键 B: 主表 A: 从表 主表:主键(主关键字) = 从表:外键(外关键 ...

  5. mysql 约束条件 外键 forigen key 介绍

    外键 forigen key作用 :建立表之间的关系 什么是外键 员工信息表有这些字段:id号  姓名 性别 员工所在部门名 部门描述信息 公司有3个部门,但是有1个亿的员工,那意味着 员工所对应的部 ...

  6. MySQL为表添加外键约束

    为表添加外键约束的语法 Alter table 表名 add constraint FK_ID foreign key(外键字段名) REFERENCES 外表表名(主键字段名): 为表student ...

  7. MySQL中如何定义外键[转]

    MySQL中如何定义外键 假如某个电脑生产商,它的数据库中保存着整机和配件的产品信息.用来保存整机产品信息的表叫做pc:用来保存配件供货信息的表叫做parts. 在pc表中有一个字段,用来描述这款电脑 ...

  8. MySQL中如何设置外键

    下面我将以举例的方式说明,作者能力有限,欢迎指正和补充.也欢迎和我交流探讨~ 一.环境 Windows操作系统,MySQL 5.7 二.使用工具 Navicat 三.前提知识 1.什么是主键 Prim ...

  9. default默认值 unique单列唯一和联合唯一 primary key主键 auto_increment自增 外键foregin key 表与表之间关系

    约束条件 之前讲过: zerofill (填充0) unsigned (约束条件值unsigned 无符号) not null 非空(不能插入空) 补充知识点 插入数据的时候可以指定字段 create ...

最新文章

  1. k8s 去除master节点污点NoSchedule,添加master节点 尽量不调度
  2. 马斯克又破纪录了:120米史上最高火箭已组装,计划今年完成首次轨道试飞
  3. 借助Ant工具,实现快速开发
  4. 福禄克2500V数字绝缘电阻测试仪开箱与上手实测
  5. springboot 热部署的两种方式
  6. python入门区块链技术_区块链入门教程
  7. linux指向域同步时间,linux在局域网下设置时间同步
  8. 职业生涯中最致命的十二中想法
  9. python利用win32com实现doc文档转为pdf文档的功能
  10. Linux系统管理-(12)-网络配置IP命令
  11. WMITesting
  12. 6月16日!蒋江伟深度解读基础云产品生态战略 | 凌云时刻
  13. 操作系统-信号量机制的一些习题
  14. 别了,腾讯!别了,深圳!我去老家当富豪了!
  15. AcWing 143. 最大异或对 —— 神奇的二进制
  16. Classic Shell 后继者之 Classic Start
  17. Linux0号进程,1号进程,2号进程
  18. AI火爆干货最全整理!五套深度学习和算法学习教程和三套Python学习视频!!!限时无套路免费领取!...
  19. 使用 Hugo 和阿里云ECS搭建个人站
  20. Coreldraw绘制珍珠项链送给你

热门文章

  1. netty中handler的理解
  2. MVC 模式 定义概念及使用
  3. 6种方式保护自己免受网络黑客攻击,你学会了吗?
  4. 东软JavaWeb实训记-DAY8-小组项目开发实践(文件上传下载等操作)
  5. 教你破除恐怖短视频的恐惧
  6. SparkStreaming在启动执行步鄹和DStream的理解
  7. SQL编程之生日问题
  8. 计算机的发展历程简述
  9. 影响我学习过程的两个小机遇
  10. 美军从APP中购买位置数据用于支持特种作战部队的海外任务