标准SQL的外键约束条件

  1): 子表引用父表的主键

drop table if exists child,parent;

create table if not exists  parent(
id int not null auto_increment primary key,
v int
); create table if not exists child(
id int not null auto_increment primary key,
parent_id int not null,
v int,
constraint fk__child__parent_id foreign key (parent_id) references parent(id)
); insert into parent(id,v) values(1,100);
insert into child(parent_id,v) values(1,1000);
insert into child(parent_id,v) values(1,1000); select * from parent;
+----+------+
| id | v |
+----+------+
| 1 | 100 |
+----+------+ select * from child;
+----+-----------+------+
| id | parent_id | v |
+----+-----------+------+
| 1 | 1 | 1000 |
| 2 | 1 | 1000 |
+----+-----------+------+

  2): 子表引用交表的唯一索引

create table if not exists  parent(
id int not null,
v int,
constraint unique index uix__parent_id (id)
); create table if not exists child(
id int not null auto_increment primary key,
parent_id int not null,
v int,
constraint fk__child__parent_id foreign key (parent_id) references parent(id)
); insert into parent(id,v) values(1,100);
insert into child(parent_id,v) values(1,1000);
insert into child(parent_id,v) values(1,1000); select * from parent;
+----+------+
| id | v |
+----+------+
| 1 | 100 |
+----+------+ select * from child;
+----+-----------+------+
| id | parent_id | v |
+----+-----------+------+
| 1 | 1 | 1000 |
| 2 | 1 | 1000 |
+----+-----------+------+

innodb在标准SQL上做的扩展

  1): 只要在父表上有在对应的列上建索引,那么这个列就能在子表中引用

create table if not exists  parent(
id int not null auto_increment primary key,
v int,
index uix__parent_v (v) -- 只要父表上有索引就行
); create table if not exists child(
id int not null auto_increment primary key,
parent_v int not null,
v int,
constraint fk__child__parent_v foreign key (parent_v) references parent(v) -- 在子表中引用
); insert into parent(id,v) values(1,100);
insert into parent(id,v) values(2,100); insert into child(parent_v,v) values(100,2000);
insert into child(parent_v,v) values(100,2000); select * from parent;
+----+------+
| id | v |
+----+------+
| 1 | 100 |
| 2 | 100 |
+----+------+ select * from child;
+----+----------+------+
| id | parent_v | v |
+----+----------+------+
| 1 | 100 | 2000 |
| 2 | 100 | 2000 |
+----+----------+------+

我的评介

  主外键约束在标准SQL下体现的是一种一对多的关系,但是经过MySQL的拓展之后可以表现出“多对多”的关系;虽然MySQL这样

  的设计有一定的灵活性,个人觉得最好还是使用标准SQL的方式。

学习交流

-----------------------------http://www.sqlpy.com-------------------------------------------------

-----------------------------http://www.sqlpy.com-------------------------------------------------

MySQL 主外键约束与标准SQL不同的地方的更多相关文章

  1. C# 如何物理删除有主外键约束的记录?存储过程实现

    十年河东,十年河西,莫欺少年穷 本篇主旨是如何物理删除有主外键约束的记录!那么,我们从主外键走起! 下面新建三张有主外键约束的表,分别为:系/学院表,专业班表,学生表,如下: CREATE TABLE ...

  2. Oracle开发 之 主-外键约束FK及约束的修改

    试验环境: 1)数据库版本:oracle 11.2.0.4 2)建表脚本:以scott的dept及emp表为基础. 父表:dept -- Create table create table DEPT ...

  3. 批量删除MSSQL 中主外键约束

    转自: http://www.maomao365.com/?p=813 在制作 MSSQL同步工具的时候,发现由于主外键的约束,导致数据同步异常,所有我们需要把 读数据库里面的主外键约束,进行批量删除 ...

  4. MySQL 建立外键约束

    http://www.jzxue.com/shujuku/mysql/201109/06-8742.html MySQL 建立外键约束的语法太晦涩难懂了, 不得不记下笔记. 1. 在建表时建立外键 C ...

  5. 两种获取MySql数据库中所有表的主键和外键约束信息的Sql语句

    最近在写Rafy底层的一些东西,在数据库方面把MySql数据库集成到里面去,里面有一个需求,需要获取非系统数据库,也就是我们自己建立的数据库中所有表的主键和外键元数据列表. 第一种方法:是网上的方法, ...

  6. SQL server 添加主外键约束

    ---添加主键约束   alter table 表名 add constraint 约束名 primary key (主键)          - --添加唯一约束   alter table 表名 ...

  7. 通过sql命令建表 和 主外键约束以及其他约束

    create table命令 create table dept ( dept_id int primary key, dept_name ) not null, dept_address ) ) c ...

  8. MySQL数据库--外键约束及外键使用

    什么是主键.外键关系型数据库中的一条记录中有若干个属性,若其中某一个属性组(注意是组)能唯一标识一条记录,该属性组就可以成为一个主键. 比如: 学生表(学号,姓名,性别,班级) 其中每个学生的学号是唯 ...

  9. mysql添加外键约束变为索引

    今天有位自己填上一坑:mysql储存引擎 原因就是数据库表引擎为:MyISAM,建立主外键关系需要是InnoDB: 解决方案:alter  table table_name1  engine=inno ...

随机推荐

  1. PASCAL知识

    API Index http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delph ...

  2. Visual Studio修改可执行程序的文件名和路径

  3. 理解CPU steal time

    http://melody-dc.com/2015/11/21/%E7%90%86%E8%A7%A3CPU-steal-time/ http://www.cnblogs.com/yjf512/p/33 ...

  4. E437: terminal capability "cm" required 错误出现的原因和解决方法

    E437: terminal capability "cm" required 错误: 出现这个问题原因是没有配置export TERM=xterm 执行:export TERM= ...

  5. Laravel返回不重复的某个字段信息列表

    ->groupBy('brand_id') ->pluck('brand_id');

  6. 4、线程范围内的数据共享之ThreadLocal

    /** * 线程范围类的数据共享 * 核心:ThreadLocal类 * 实际场景: * Hibernate的getCurrentSession方法,就是从线程范围内获取存在的session,如果不存 ...

  7. Qt5.9静态库编译VS2015-x64

    不多说. 编译配置参数如下 configure.bat -static -no-openssl -release 不支持OpenSSL,也没有安装各个数据库的Driver,所以数据库方面也只支持了SQ ...

  8. Inno Setup入门(九)——修改安装过程中的文字显示

    前面说到过可以使用不用的语言文件实现不同的显示方式,方便与国际接轨,事实上即使没有语言文件也可以实现修改.[Messages] 段用于定义安装程序和卸载程序中显示的消息.一般不需要创建 [Messag ...

  9. Centos6下编译LEDE/OpenWrt

    准备工作 1. 安装依赖软件 这是官方文档提供的依赖列表 yum install subversion binutils bzip2 gcc gcc-c++ gawk gettext flex ncu ...

  10. QQ登录整合/oauth2.0认证-02-跳转到QQ互联页

    ---------------------------目录---------------------------------- QQ登录整合/oauth2.0认证-01-申请appkey和appid ...