标准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. http 三次握手

    1.三次握手 目的:主要规避由于网络阻塞,传输不畅等原因导致服务器端端口一直被占用,无法释放端口资源.(想想假设只要一次就建立连接,服务器响应后浏览器若是没收到数据,等待一段时间后,网络超时,浏览器重 ...

  2. linux C++ 莫名奇异的段错误(segmentation fault),无法调用其他函数

    进来在linux下开发C++项目,遇到了非常奇怪的bug. 项目须要多线程实现,在写好代码后,每当执行到线程函数内部,当内部调用其他函数如printf.fopen等时就会提示段错误(segmentat ...

  3. 【DB2】数据库的事务日志已满。SQLSTATE=57011

    问题描述 在使用数据库的时候报错如上图,我们先使用db2 get db cfg for sample查看相关配置参数,其中sample为数据库名称 C:\Users\Thinkpad>db2 g ...

  4. mingw 构建 Geos

    简述 在做某个小程序时候用到了QT,而用的Qt是mingw版本的,所以使用mingw构建了一下geos库. 1.准备工作 首先需要先安装好mingw,这里直接使用http://www.mingw-w6 ...

  5. LVM逻辑卷管理测试——逻辑卷扩展、收缩、快照及删除

    一.逻辑卷扩展 [root@lxjtest /]# umount /testLVM/ [root@lxjtest /]# df -h Filesystem Size Used Avail Use% M ...

  6. (原)CosFace/AM-Softmax及其mxnet代码

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/8525241.html 论文: CosFace: Large Margin Cosine Loss fo ...

  7. 如何找到Firefox的收藏夹,就像IE一样,出现在网页的侧面

    firefox有这个插件,安装后就可以 http://addons.mozine.cn/firefox/19/   All-In-OneSidebar ? 概述   All-In-OneSidebar ...

  8. 转 java实现截图

    转自 http://www.zhenhua.org/article.asp?id=382 可以直接运行! import java.awt.Dimension; import java.awt.Rect ...

  9. Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 2611816 bytes)

    一段PHP程序执行报错: Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 261181 ...

  10. TCP连接建立与释放

    tcp建立连接 tcp连接的建立需要经历”三次握手“的过程.过程如下 client发送SYN包(值为j)以及SEQ包到server端,此时client进入SYN_SEND状态.此为第一次握手. ser ...