一、外键约束

创建外键

---  每一个班主任会对应多个学生 , 而每个学生只能对应一个班主任

----主表

CREATE TABLE ClassCharger(

       id TINYINT PRIMARY KEY auto_increment,
name VARCHAR (),
age INT ,
is_marriged boolean -- show create table ClassCharger: tinyint() ); INSERT INTO ClassCharger (name,age,is_marriged) VALUES ("冰冰",,),
("丹丹",,),
("歪歪",,),
("姗姗",,),
("小雨",,); ----子表 CREATE TABLE Student( id INT PRIMARY KEY auto_increment,
name VARCHAR (),
charger_id TINYINT, --切记:作为外键一定要和关联主键的数据类型保持一致
-- [ADD CONSTRAINT charger_fk_stu]FOREIGN KEY (charger_id) REFERENCES ClassCharger(id) ) ENGINE=INNODB; INSERT INTO Student(name,charger_id) VALUES ("alvin1",),
("alvin2",),
("alvin3",),
("alvin4",),
("alvin5",),
("alvin6",),
("alvin7",); DELETE FROM ClassCharger WHERE name="冰冰";
INSERT student (name,charger_id) VALUES ("yuan",);
-- 删除居然成功,可是 alvin3显示还是有班主任id=1的冰冰的; -----------增加外键和删除外键--------- ALTER TABLE student ADD CONSTRAINT abc
FOREIGN KEY(charger_id)
REFERENCES classcharger(id); ALTER TABLE student DROP FOREIGN KEY abc;

INNODB支持的ON语句

--外键约束对子表的含义:   如果在父表中找不到候选键,则不允许在子表上进行insert/update

--外键约束对父表的含义:    在父表上进行update/delete以更新或删除在子表中有一条或多条对
-- 应匹配行的候选键时,父表的行为取决于:在定义子表的外键时指定的
-- on update/on delete子句 -----------------innodb支持的四种方式--------------------------------------- -----cascade方式 在父表上update/delete记录时,同步update/delete掉子表的匹配记录
-----外键的级联删除:如果父表中的记录被删除,则子表中对应的记录自动被删除-------- FOREIGN KEY (charger_id) REFERENCES ClassCharger(id)
ON DELETE CASCADE ------set null方式 在父表上update/delete记录时,将子表上匹配记录的列设为null
-- 要注意子表的外键列不能为not null FOREIGN KEY (charger_id) REFERENCES ClassCharger(id)
ON DELETE SET NULL ------Restrict方式 :拒绝对父表进行删除更新操作(了解) ------No action方式 在mysql中同Restrict,如果子表中有匹配的记录,则不允许对父表对应候选键
-- 进行update/delete操作(了解)

多表查询(*****)

准备表

-- 准备两张表
-- company.employee
-- company.department create table employee(
emp_id int auto_increment primary key not null,
emp_name varchar(),
age int,
dept_id int
); insert into employee(emp_name,age,dept_id) values
('A',,),
('B',,),
('C',,),
('D',,),
('E',,),
('F',,); create table department(
dept_id int,
dept_name varchar()
); insert into department values
(,'人事部'),
(,'技术部'),
(,'销售部'),
(,'财政部'); mysql> select * from employee;
+--------+----------+------+---------+
| emp_id | emp_name | age | dept_id |
+--------+----------+------+---------+
| | A | | |
| | B | | |
| | C | | |
| | D | | |
| | E | | |
| | F | | |
+--------+----------+------+---------+
rows in set (0.00 sec) mysql> select * from department;
+---------+-----------+
| dept_id | dept_name |
+---------+-----------+
| | 人事部 |
| | 技术部 |
| | 销售部 |
| | 财政部 |
+---------+-----------+
rows in set (0.01 sec)

准备两张表

多表查询之连接查询

1.笛卡尔积查询

mysql> SELECT * FROM employee,department;

--        select employee.emp_id,employee.emp_name,employee.age,
-- department.dept_name from employee,department; +--------+----------+------+---------+---------+-----------+
| emp_id | emp_name | age | dept_id | dept_id | dept_name |
+--------+----------+------+---------+---------+-----------+
| | A | | | | 人事部 |
| | A | | | | 技术部 |
| | A | | | | 销售部 |
| | A | | | | 财政部 |
| | B | | | | 人事部 |
| | B | | | | 技术部 |
| | B | | | | 销售部 |
| | B | | | | 财政部 |
| | C | | | | 人事部 |
| | C | | | | 技术部 |
| | C | | | | 销售部 |
| | C | | | | 财政部 |
| | D | | | | 人事部 |
| | D | | | | 技术部 |
| | D | | | | 销售部 |
| | D | | | | 财政部 |
| | E | | | | 人事部 |
| | E | | | | 技术部 |
| | E | | | | 销售部 |
| | E | | | | 财政部 |
| | F | | | | 人事部 |
| | F | | | | 技术部 |
| | F | | | | 销售部 |
| | F | | | | 财政部 |
+--------+----------+------+---------+---------+-----------+

2.内连接

-- 查询两张表中都有的关联数据,相当于利用条件从笛卡尔积结果中筛选出了正确的结果。

  select * from employee,department where employee.dept_id = department.dept_id;
--select * from employee inner join department on employee.dept_id = department.dept_id; +--------+----------+------+---------+---------+-----------+
| emp_id | emp_name | age | dept_id | dept_id | dept_name |
+--------+----------+------+---------+---------+-----------+
| | A | | | | 人事部 |
| | B | | | | 技术部 |
| | C | | | | 技术部 |
| | D | | | | 销售部 |
| | E | | | | 人事部 |
+--------+----------+------+---------+---------+-----------+

3.外连接

--()左外连接:在内连接的基础上增加左边有右边没有的结果

 select * from employee left join department on employee.dept_id = department.dept_id;

     +--------+----------+------+---------+---------+-----------+
| emp_id | emp_name | age | dept_id | dept_id | dept_name |
+--------+----------+------+---------+---------+-----------+
| | A | | | | 人事部 |
| | E | | | | 人事部 |
| | B | | | | 技术部 |
| | C | | | | 技术部 |
| | D | | | | 销售部 |
| | F | | | NULL | NULL |
+--------+----------+------+---------+---------+-----------+ --()右外连接:在内连接的基础上增加右边有左边没有的结果 select * from employee RIGHT JOIN department on employee.dept_id = department.dept_id; +--------+----------+------+---------+---------+-----------+
| emp_id | emp_name | age | dept_id | dept_id | dept_name |
+--------+----------+------+---------+---------+-----------+
| | A | | | | 人事部 |
| | B | | | | 技术部 |
| | C | | | | 技术部 |
| | D | | | | 销售部 |
| | E | | | | 人事部 |
| NULL | NULL | NULL | NULL | | 财政部 |
+--------+----------+------+---------+---------+-----------+ --()全外连接:在内连接的基础上增加左边有右边没有的和右边有左边没有的结果 -- mysql不支持全外连接 full JOIN
-- mysql可以使用此种方式间接实现全外连接 select * from employee RIGHT JOIN department on employee.dept_id = department.dept_id
UNION
select * from employee LEFT JOIN department on employee.dept_id = department.dept_id; +--------+----------+------+---------+---------+-----------+
| emp_id | emp_name | age | dept_id | dept_id | dept_name |
+--------+----------+------+---------+---------+-----------+
| | A | | | | 人事部 |
| | B | | | | 技术部 |
| | C | | | | 技术部 |
| | D | | | | 销售部 |
| | E | | | | 人事部 |
| NULL | NULL | NULL | NULL | | 财政部 |
| | F | | | NULL | NULL |
+--------+----------+------+---------+---------+-----------+ -- 注意 union与union all的区别:union会去掉相同的纪录

多表查询之复合条件连接查询

-- 查询员工年龄大于等于25岁的部门

    SELECT DISTINCT department.dept_name
FROM employee,department
WHERE employee.dept_id = department.dept_id
AND age>; --以内连接的方式查询employee和department表,并且以age字段的升序方式显示 select employee.emp_id,employee.emp_name,employee.age,department.dept_name
from employee,department
where employee.dept_id = department.dept_id
order by age asc;

多表查询之子查询

-- 子查询是将一个查询语句嵌套在另一个查询语句中。
-- 内层查询语句的查询结果,可以为外层查询语句提供查询条件。
-- 子查询中可以包含:IN、NOT IN、ANY、ALL、EXISTS 和 NOT EXISTS等关键字
-- 还可以包含比较运算符:= 、 !=、> 、<等 -- . 带IN关键字的子查询 ---查询employee表,但dept_id必须在department表中出现过 select * from employee
where dept_id IN
(select dept_id from department); +--------+----------+------+---------+
| emp_id | emp_name | age | dept_id |
+--------+----------+------+---------+
| | A | | |
| | B | | |
| | C | | |
| | D | | |
| | E | | |
+--------+----------+------+---------+
rows in set (0.01 sec) -- . 带比较运算符的子查询
-- =、!=、>、>=、<、<=、<> -- 查询员工年龄大于等于25岁的部门
select dept_id,dept_name from department
where dept_id IN
(select DISTINCT dept_id from employee where age>=); -- . 带EXISTS关键字的子查询 -- EXISTS关字键字表示存在。在使用EXISTS关键字时,内层查询语句不返回查询的记录。
-- 而是返回一个真假值。Ture或False
-- 当返回Ture时,外层查询语句将进行查询;当返回值为False时,外层查询语句不进行查询 select * from employee
WHERE EXISTS
(SELECT dept_name from department where dept_id=); --department表中存在dept_id=,Ture select * from employee
WHERE EXISTS
(SELECT dept_name from department where dept_id=); -- Empty set (0.00 sec) ps: create table t1(select * from t2);

MySQL数据库(4)_MySQL数据库外键约束、表查询的更多相关文章

  1. 主外键多表查询demo

    https://www.cnblogs.com/DragonFire/p/6949767.html mySQL练习-主外键多表查询 MySQL练习-主外键多表查询 练习: 1.建立表关系: 请创建如下 ...

  2. 【Hibernate】无外键多表查询

    无外键多表查询时编写hql,直接使用逗号分隔表,where作为联合查询条件进行查询.查询出来的结果可为两种,List<List<Object>>或者List<Map< ...

  3. sql操作数据库(3)-->外键约束、数据库表之间的关系、三大范式、多表查询、事务

    外键约束 在新表中添加外键约束语法: constraint 外键约束名称 foreign key(外键的字段名称) references 主表表名(主键字段名) 在已有表中添加外键约束:alter t ...

  4. MySQL进阶13--常见六大约束: 非空/默认/主键/唯一约束/检查约束/外键约束--表级约束 / 列级约束

    /* MySQL进阶13 常见六大约束: 1.not null 非空 2.default :默认值,用于保证该字段的默认值 ; 比如年龄:1900-10-10 3.primary key : 主键,用 ...

  5. mysql外键与表查询

    目录 自增特性 外键 外键关系 外键创建 外键的约束效果 级联更新级联删除 多对多关系 一对一关系 表查询关键字 select与from where筛选 group by分组 练习 关系练习 查询练习 ...

  6. mysql truncate带有被引用外键的表时报错解决方法

    清空具有外键约束的表时报ERROR 1701(42000)的解决   mysql> truncate table t_users;ERROR 1701 (42000): Cannot trunc ...

  7. mysql使用truncate截断带有外键的表时报错--解决方案

    报错内容如:1701 - Cannot truncate a table referenced in a foreign key constraint 一.为什么要使用truncate 使用trunc ...

  8. mysql操作说明,插入时外键约束,快速删除

    快速删除: CMD命令 SET FOREIGN_KEY_CHECKS=0;去除外键约束 truncate table 表名;

  9. MySQL练习-主外键多表查询

    练习: 1.建立表关系: 请创建如下表,并创建相关约束 USE db1; CREATE TABLE class( cid INT AUTO_INCREMENT PRIMARY KEY, caption ...

  10. Flask-SQLAlchemy - 不使用外键连表查询。记得常回来看我

    前言 相比于 Django 的 ORM ,SQLAlchemy "不依靠外键进行跨表联查" 的解决方案就比较多. 没啥好说的,只能怪自己学艺不精..  _(:з」∠)_ 解决办法 ...

随机推荐

  1. java - day08 - PrimeNumLoop

    质数循环查找 package day07_addition; //范围查找质数 public class PrimeNumLoop { public static void main(String[] ...

  2. Eclipse maven 项目红叉 编译不报错问题处理

    项目右键-> Maven ->  Update Maven Project 选中 :Force update 复选框

  3. 快速上手UIView动画

    UIView动画有两种使用方法 UIView [begin commit]模式 //动画开始标记 [UIView beginAnimations:@"changeframe" co ...

  4. 分布式服务框架 Zookeeper(二)官方介绍

    ZooKeeper:为分布式应用而生的分布式协调服务 ZooKeeper是一个为分布式应用而设计的分布式的.开源的协调服务.它提供了一套简单的原语,分布式应用利用这套原语可以实现更高层的服务,比如一致 ...

  5. map--C++ STL 学习

    map–C++ STL 学习   Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力.   说下map内 ...

  6. Linux gdb调试器

    gdb的启动 --gdb 程序名 [corefile] --corefile是可选的,但能增强gdb的调试能力 --强调:启动gdb必须在编译命里加上"-g"参数,"-g ...

  7. thinkphp No input file specified的解决方法

    .htaccess RewriteRule ^(.*)$ index.php?s=$1 [QSA,PT,L]

  8. Cocoapod安装使用和常见问题

    1.cocoapod的按照,cocoapod是运行在ruby环境下的,在ruby环境的 ,像cocoapod这样的开源项目时放在放在rubygems服务器上面的,但国内访问https://rubyge ...

  9. 机器学习算法-Adaboost

    本章内容 组合类似的分类器来提高分类性能 应用AdaBoost算法 处理非均衡分类问题 主题:利用AdaBoost元算法提高分类性能 1.基于数据集多重抽样的分类器 - AdaBoost 长处 泛化错 ...

  10. [转]unity3d所要知道的基础知识体系大纲,可以对照着学习,不定期更新 ... ... ... ...

    本文献给,想踏入3d游戏客户端开发的初学者. 毕业2年,去年开始9月开始转作手机游戏开发,从那时开始到现在一共面的游戏公司12家,其中知名的包括搜狐畅游.掌趣科技.蓝港在线.玩蟹科技.天神互动.乐元素 ...