MySQL 主外键约束与标准SQL不同的地方
【标准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不同的地方的更多相关文章
- C# 如何物理删除有主外键约束的记录?存储过程实现
十年河东,十年河西,莫欺少年穷 本篇主旨是如何物理删除有主外键约束的记录!那么,我们从主外键走起! 下面新建三张有主外键约束的表,分别为:系/学院表,专业班表,学生表,如下: CREATE TABLE ...
- Oracle开发 之 主-外键约束FK及约束的修改
试验环境: 1)数据库版本:oracle 11.2.0.4 2)建表脚本:以scott的dept及emp表为基础. 父表:dept -- Create table create table DEPT ...
- 批量删除MSSQL 中主外键约束
转自: http://www.maomao365.com/?p=813 在制作 MSSQL同步工具的时候,发现由于主外键的约束,导致数据同步异常,所有我们需要把 读数据库里面的主外键约束,进行批量删除 ...
- MySQL 建立外键约束
http://www.jzxue.com/shujuku/mysql/201109/06-8742.html MySQL 建立外键约束的语法太晦涩难懂了, 不得不记下笔记. 1. 在建表时建立外键 C ...
- 两种获取MySql数据库中所有表的主键和外键约束信息的Sql语句
最近在写Rafy底层的一些东西,在数据库方面把MySql数据库集成到里面去,里面有一个需求,需要获取非系统数据库,也就是我们自己建立的数据库中所有表的主键和外键元数据列表. 第一种方法:是网上的方法, ...
- SQL server 添加主外键约束
---添加主键约束 alter table 表名 add constraint 约束名 primary key (主键) - --添加唯一约束 alter table 表名 ...
- 通过sql命令建表 和 主外键约束以及其他约束
create table命令 create table dept ( dept_id int primary key, dept_name ) not null, dept_address ) ) c ...
- MySQL数据库--外键约束及外键使用
什么是主键.外键关系型数据库中的一条记录中有若干个属性,若其中某一个属性组(注意是组)能唯一标识一条记录,该属性组就可以成为一个主键. 比如: 学生表(学号,姓名,性别,班级) 其中每个学生的学号是唯 ...
- mysql添加外键约束变为索引
今天有位自己填上一坑:mysql储存引擎 原因就是数据库表引擎为:MyISAM,建立主外键关系需要是InnoDB: 解决方案:alter table table_name1 engine=inno ...
随机推荐
- mac pro 显示隐藏文件
经常希望在IOS操作系统现实隐藏文件,下面两条语句可以使用: 显示:defaults write com.apple.finder AppleShowAllFiles -bool true 隐藏:de ...
- JERSEY中文翻译(第三章、模块和依赖)
Chapter 2 Modules and Dependencencies 2.1 Java SE 兼容 所有的Jersey组建都是基于Java6开发的,所以你的Java必须是Java6以上的版本才能 ...
- webbrowser载入地图网页出现脚本错误解决
对于这个问题.我整整花了一上午的时间来解决,网上关于此问题的解决差点儿找不到,于是我就尽能够能的从网上相关问题的答案中获取些灵感.功夫不负有心人.最终通过这些灵感的积累我最终攻克了此问题. 首先让我们 ...
- Spring面试题一
目录一.Spring工作原理 二.为什么要用Spring三.请你谈谈SSH整合四.介绍一下Spring的事务管理五.什么是依赖注入,依赖注入的作用是什么? 六.什么是AOP,AOP的作用是什么? 七. ...
- Android——开机自启动app
android在开机完成后会发送一个android.intent.action.BOOT_COMPLETED的广播,告诉系统内app们已经开机. 我们可以在需要开机自启动的app中定义一个广播接收器, ...
- J2EE框架知识清单
1:Struts MVC.JVC 2:struts action 3:struts 1.0和2.0区别 4:Spring 核心机制:依赖注入 5:使用Spring容器 6:AOP的概念与应用 7:IO ...
- Xamarin.Android之ListView和Adapter
一.前言 如今不管任何应用都能够看到列表的存在,而本章我们将学习如何使用Xamarin去实现它,以及如何使用适配器和自定义适配器(本文中的适配器的主要内容就是将原始的数据转换成了能够供列表控件显示的项 ...
- 微信扫码支付springboot版本
发布时间:2018-11-06 技术:springboot+freemarker 概述 该项目是一个采用springboot构建的web项目,主要实现了微信扫码支付功能.包含最基本的创建订单, ...
- 微信小程序支付源码,后台服务端代码
作者:尹华南,来自原文地址 微信小程序支付绕坑指南 步骤 A:小程序向服务端发送商品详情.金额.openid B:服务端向微信统一下单 C:服务器收到返回信息二次签名发回给小程序 D:小程序发起支付 ...
- ios 中UIViewController的分类
#import <UIKit/UIKit.h> #define TOPVIEWTAG 0x10000 // 导航栏的图片 @interface UIViewController (Chnb ...