day02mysql多表联查

过滤连表查询

select student.stuname,score.score,score.courseid  from student, score 
where student.stuname="jack"    and student.stuid=score.stuid;

select student.stuname,score.score,score.courseid
from student join score  
on student.stuid=score.stuid 
where score.score >80;

一、数据的完整性

1.实体的完整性

实体:表中的一行或者一条记录代表一个实体
实体完整性的作用:标识每一行数据不重复
约束类型:
​ 主键约束【primary key】
​ 唯一约束【unique】
​ 自动增长列【auto_increment】

1.1 主键约束【primary key】

特点:数据唯一,且不能为null
主键可以是表中的一个字段或者多个字段,它的值用来唯一标识表中的某一条记录
场景:在多个表的关联关系中

#方式一
mysql> create table stu1(
	-> 		id int primary key,
	-> 		name varchar(50)
	-> );

#方式二
mysql> create table stu2(
	-> 		id int,
	-> 		name varchar(50),
	-> 		primary key(id,name)
	-> );

#方式三
mysql> create table stu3(
	-> 		id int,
	-> 		name varchar(50)
	-> );
mysql> alter table stu3 add constraint stu3_id primary key(id);

1.2 唯一约束

作用:在非主键列中不能输入重复的值

mysql> create table stu4(
 	-> 		id int primary key,
 	-> 		name varchar(50) unique
 	-> );


#primary key和unique之间的区别
a.二者都强调的是唯一性
b.在同一个表中,一般只出现一个primary key,可以出现多个unique
c.primary key不允许为null,但是unique是允许的

1.3 自动增长列

给主键添加自动增长性,列只能是整数类型
场景:一般添加给主键

mysql> create table stu5(
 -> id int primary key auto_increment,
 -> name varchar(50) unique
 -> );

2.域完整性

作用:限制单元格数据的正确性,
​ 域代表当前单元格
约束类型:
​ 数据类型
​ 非空约束【not null】
​ 默认值约束【default】

2.1数据类型

数字类型:int float double
日期类型:date datetime
字符串类型:varchar(20)

2.2 非空约束【not null】

mysql> create table stu6( id int primary key auto_increment, name varchar(50) unique not null);

#注意:name被约束为not null,插入数据的时候,name坚决不能为null,如果为null,数据库立马报错

2.3 默认值约束

mysql> create table stu7(
 -> id int primary key auto_increment,
 -> name varchar(50) unique not null,
 -> address varchar(50) default "beijing"
 -> );

3.外键约束 foreign key

二、多表查询

1.合并结果集

作用:将两个select语句的查询结果合并到一起
两种方式:
​ union:去除重复记录【并集】
​ union all;获取所有的结果

#合并结果集
mysql> select * from A
-> union
-> select * from B;
+------+-------+
| name | score |
+------+-------+
| a    |    10 |
| b    |    20 |
| c    |    30 |
| d    |    40 |
+------+-------+

mysql> select * from A
-> union all
-> select * from B;
+------+-------+
| name | score |
+------+-------+
| a    |    10 |
| b    |    20 |
| c    |    30 |
| a    |    10 |
| d    |    40 |
| c    |    30 |
+------+-------+

2.连接结果查询

作用:求出多个表的乘积,例如t1和t2,如果采用了连接查询,得到的结果是t1*t2(笛卡尔)

#解决办法:在实际应用中,需要去除重复记录,则需要通过条件进行过滤
mysql> select s.stuid,s.stuname,c.score,c.courseid from student s,score c where s.stuid=c.stuid;
+-------+----------+-------+----------+
| stuid | stuname  | score | courseid |
+-------+----------+-------+----------+
| 1001  | zhangsan |    98 |        1 |
| 1002  | lisi     |    80 |        2 |
| 1003  | jack     |    70 |        1 |
| 1004  | tom      |    60 |        2 |
| 1002  | lisi     |    75 |        3 |
+-------+----------+-------+----------+

2.1 内连接-inner join on

#内连接
mysql> select s.stuid,s.stuname,c.score,c.courseid  from student s join score c on s.stuid=c.stuid;
+-------+----------+-------+----------+
| stuid | stuname  | score | courseid |
+-------+----------+-------+----------+
| 1001  | zhangsan |    98 |        1 |
| 1002  | lisi     |    80 |        2 |
| 1003  | jack     |    70 |        1 |
| 1004  | tom      |    60 |        2 |
| 1002  | lisi     |    75 |        3 |
+-------+----------+-------+----------+

#等价写法
mysql> select  s.stuid,s.stuname,c.score,c.courseid from student s,score c where s.stuid=c.stuid;
+-------+----------+-------+----------+
| stuid | stuname  | score | courseid |
+-------+----------+-------+----------+
| 1001  | zhangsan |    98 |        1 |
| 1002  | lisi     |    80 |        2 |
| 1003  | jack     |    70 |        1 |
| 1004  | tom      |    60 |        2 |
| 1002  | lisi     |    75 |        3 |
+-------+----------+-------+----------+

2.2 外连接-outer join on

特点:以其中一个表作为参照连接另外一个表
分类:
​ 左外连接:left join on
​ 右外连接:right join on

#左外连接
mysql> select s.stuid,s.stuname,c.score,c.courseid  from student s left join score c on s.stuid=c.stuid;
+-------+----------+-------+----------+
| stuid | stuname  | score | courseid |
+-------+----------+-------+----------+
| 1001  | zhangsan |    98 |        1 |
| 1002  | lisi     |    80 |        2 |
| 1003  | jack     |    70 |        1 |
| 1004  | tom      |    60 |        2 |
| 1002  | lisi     |    75 |        3 |
+-------+----------+-------+----------+

#右外连接
#参照为c
mysql> select s.stuid,s.stuname,c.score,c.courseid  from student s right join score c on s.stuid=c.stuid;
+-------+----------+-------+----------+
| stuid | stuname  | score | courseid |
+-------+----------+-------+----------+
| 1001  | zhangsan |    98 |        1 |
| 1002  | lisi     |    80 |        2 |
| 1002  | lisi     |    75 |        3 |
| 1003  | jack     |    70 |        1 |
| 1004  | tom      |    60 |        2 |
+-------+----------+-------+----------+

3.子查询(嵌套查询)

#1.查询和scott在同一个部门的员工
mysql> select * from emp where deptno=(select deptno from emp where enname='scott');

#2.查询工资高于joens的员工信息
mysql> select * from emp where sal>(select sal from emp where enname='jones');

4.自关联

三、数据库的备份和恢复

1.备份

命令:mysqldump -u root -p  数据库名>生成sql脚本的路径

3.恢复

#前提:必须先创建数据库【空的】
mysql> source /home/rock/Desktop/mydb1.sql;

多对多关系

猜你喜欢

转载自blog.csdn.net/qq_37783111/article/details/107697016
今日推荐