数据库系统概论(第五版复习)part1+mysql

前言

本文作用:重新学习一边数据库系统概论,对重要的概念着重记录,理解概念;另外要mysql为例,把其中的概念与之对应。如有错误,欢迎指正。

一、绪论

1数据库的四个基本概念

数据:描述事物的符号记录(被记录下的信息)
数据库:存在计算机中的数据的集合。(被存储在一起的数据)
数据库管理系统:数据库管理系统(应用程序,用户与操作系统之间。具有六个方面的主要功能:数据定义/数据组织、管理、存储/数据操作/数据库事务管理、运行管理/数据库的建立和维护功能/其他功能。mysql就是一个数据库软件)
数据库系统:存储、管理、处理和维护数据的系统。(人+软件+硬件)

2数据管理系统的发展

人工管理、文件系统(windows中的文件管理算一个)、数据库系统(开始有专门的软件)。

3数据模型

1数据模型:现实世界数据的抽象(一种映射,把现实中的事物映射为数据)
2两类数据模型:概念模型、逻辑模型和物理模型
3概念模型:实体,属性,码,实体型(一类实体),实体集(同类集合),联系(一对多,一对一,多对多等)。E-R图表示概念模型。
4数据模型:数据结构,数据操作,数据完整性约束组成。(这里指的就是逻辑数据模型
5常用逻辑模型:层次结构,网状模型,关系模型,面向对象模型,对象关系数据模型,半结构化数据模型。【逻辑模型也代表数据库中的存储结构,逻辑模型是基于概念模型设计出来的。也就是,e-r模型才是数据库库模型的基础】

4数据库系统结构

1通常是三级模式结构:
模式:与具体的程序,硬件无关,仅仅是逻辑上的描述(就是逻辑模型)
外模式:用户模式,数据库视图。
内模式:数据的物理存储方式:堆存储,簇存储,如何索引,b+索引或哈希索引,是否压缩加密。
2修改数据库中的表格或扩充字段,就是对逻辑模型进行修改。

二、关系数据库

关系数据库:以数学方法处理数据(就是有严格的定义和运算方法)。(支持关系模型的数据库系统。)【这一章全是重点,看书】
关系模型由关系数据结构,关系操作集合和关系完整性三部分组成。

1关系数据结构

关系数据数据结构,只有一种:关系。

关系数据结构涉及的基本定义有:

域:一组具有相同数据类型的值的集合(就是一些相同类型的数据),
笛卡尔积:域上的集合运算。(元组:一条基本的数据。分量:元组的某一个属性。)(笛卡尔积:通过域的组合,生成元组。就是排列组合生成数据,元组有多少种可能的取值,就是笛卡尔积的基数。域的基数,是域有多少种可能的取值。)
关系【看书吧】
(笛卡尔积的子集,是关系。关系中若有能唯一标识元组的属性组的值,称为候选码。若有多个候选码,选一个作为主码。作为候选码的属性称为主属性,其他非码属性、非主属性)
(域直接笛卡尔积是没有实际语义的,只有它(笛卡尔积)的真子集才有实际含义)
(基本关系、基本表,查询表,视图表)【看书吧】
关系数据库中,的关系要满足一定的规范条件,这些规范条件,称为范式。

关系模式

关系模式定义:关系的描述。R(U,D,DOM,F)
关系是元组的集合。
关系名,属性名集合,域,属性跟域之间的映像集合,属性间的数据依赖关系集合。
关系是关系模式某一个时刻的值。关系模式静态,关系动态。

2关系操作

常用关系操作:查询,插入,删除,修改
基本关系操作:选择,投影,并,差,笛卡尔积。
关系操作的对象和结果都是集合。
关系数据语言分类【关系代数,关系演算】
SQL语言(结构化查询语言)是介于关系代数和关系演算之间的一种语言。

3关系的完整性

三类关系的完整性约束。
实体完整性:主码作为唯一标识,不能为空,不能重复。
参照完整性:索引的外码,必须是存在的主码,主码更新了,也会导致外码失效。如果外码作为表格中的唯一标识符,那就不能为空。否则,就可以为空。
用户定义的完整性:用户定义的规则。

4关系代数

关系代数:对关系的运算进行表达。
传统集合运算:并,差,交,笛卡尔积。【二目运算,两个关系必须具有相同的n个属性,且属性取自相同的域】
专门的关系运算:选择,投影,连接,除运算。

5关系演算

【跳过】

三、关系数据库标准语言sql

sql标准:国际标准化(iso)组织定义的sql标准,即sql应当满足的概念和特性。
sql特点:。

支持sql的关系数据库管理系统即支持关系数据库的三级模式结构。
#使用的数据是书中学生表的数据。
#后续使用的语句参考在mysql 8.0中的使用,这里使用dbeaver连接mysql
#windows下mysql不区分大小写,包括数据库名/表格名/列名/变量值都不区分大小写。
#这里就记一下代码,不解释概念。

1数据定义

定义模式,表,视图,索引。

模式的定义

#这里参考了文章 :http://c.biancheng.net/view/2413.html
【mysql的模式,即database的增删】。
创建语法格式:

CREATE DATABASE [IF NOT EXISTS] <数据库名>
[[DEFAULT] CHARACTER SET <字符集名>] 
[[DEFAULT] COLLATE <校对规则名>];

创建:create database if not exsits test_db;
删除:drop database if exists test_db;
使用:use database test_db;

表的定义

定义:

例1:
create  table student(
	Sno CHAR(9) primary key,
	Sname char(10) unique,
	Ssex char(2),
	Sage smallint,
	Major char(20)
	);
alter table student change column major sdept char(20);改下列名
例2:
create table course(
	Cno char(4) primary key,
	Cname char(40) not null,
	Cpno char(4),
	Ccredit smallint,
	foreign key(Cpno) references Course(Cno)
	);
例3:
create table sc(
	Sno char(9),
	Cno char(4),
	Grade smallint,
	primary key(Sno,Cno),
	foreign key(Sno) references Student(Sno),
	foreign key(Cno) references Course(Cno)
	);

#mysql数据类型:https://www.runoob.com/mysql/mysql-data-types.html
删除:drop table student;
查看表字段:desc student;

修改表格:
alter table student add S_entrance Date;#增加列
alter table student modify column Sage int;#修改数据类型,与书有差异
alter table course add unique(Cname);#增加约束条件
索引的定义

创建索引:
create unique index stusno on student(Sno);
create unique index SCno on sc(Sno asc,Cno desc);
参考文章:https://blog.csdn.net/jmx_bigdata/article/details/88873268
重命名索引:
alter table sc rename index SCno to SCSno;
#删除索引:
alter table sc drop index SCSno;

2数据查询

单表查询
选择若干列

select sno,sname,2014-Sage
from student;#带个计算
select sname Name,“Year of birth:”,2014-Sage Birthday,lower(sdept) department
from student;#查询数据起别名,大小写修改

选择若干元组
select sno from sc;
select distinct sno from sc;#去重
#带查询条件
select sname,sage from student where sage<20;#比较大小
select sname,sage from student where sage between 20 and 23;#数值介于
select sname,sage from student where sdept in ("CS","MA","IS");#值在某个列表中
select sname,sage from student where sdept not in ("CS","MA","IS");
select * from student where sno like "201215121";#值相似于
select * from student where sname like "刘%";#值以刘开头
select * from student where sname like "_立";#值以立结尾,两个字
select * from course where Cname like "DB\_Design" ;#转义字符\
select * from sc where grade is null; #为空数值
select * from sc where grade is not null;
select Sname from student where sdept = 'CS' and sage<20;#与
select Sname from student where sdept = 'CS' or sdept = 'MA';#或
order by排序

例:
select sno,grade from sc where cno=‘3’ order by grade desc;
select * from student order by sdept, sage desc;

聚集函数(统计量)

统计量有count,sum,avg,max,min
统计量只能在select子句和group by的having子句中使用,不能用在where中做查询条件。
例:select count(*) from student;

group by

数据分组
select cno,count(sno) from sc group by cno;
select sno from sc group by sno having count(*) >=3;#组内计数大于等于3
select sno from sc group by sno having avg(grade)>=80;#组内均值大于等于80

连接查询
等值与非等值连接查询

#通过比较运算符连接表格
select student.,sc.
from student,sc
where student.sno = sc.sno;#等值
select student.sno,sname
from student,sc
where student.sno =sc.sno and
sc.cno=‘2’ and sc.grade>70;#选择+等值

自身连接

#要对表格起别名
select first.cno,second.cpno
from course first,course second
where first.cpno = second.cno;
#还有外连接和多表连接

嵌套查询

#被嵌套在里头语句称为子查询,不能在子查询里嵌套orderby,只能在最外面排序

带in的子查询
select sname
from student 
where sno in 
	(select sno
	from sc 
	where cno = '2');
带比较运算符的子查询
select sno,cno
from sc x
where grade>=(
	select avg(Grade)
	from sc y
	where y.sno = x.sno);#这里要这么理解,先是求了两张不同的表格,然后做表连接,然后做判断
带有any或all的子查询
select sname,sage
from student 
where sage<any(
	select sage
	from student 
	where sdept='CS'
	)
and sdept <> "CS";
带有exists的子查询
select sname
from student 
where exists 
	(select * 
	from sc 
	where sno=student.sno and cno = '1');#这里的exist可以理解为,做表连接之后,然后按照连接后的数据,做条件判断,exist就是判断存在与否
#上下两句结果相同
select sname
from student,sc 
where sc.sno=student.sno and sc.cno = '1';

#书中复杂一点的嵌套

select sname
from student 
where not exists 
	(select * 
	from course 
	where not exists 
		(select * 
		from sc 
		where sno =student.sno and cno = course.cno
		)
	);#查选了所有课的学生,理解起来,三个表格连接,然后是选了所有课的学生。
#上下文等价,先创一个多表连接的视图,然后从视图里分组查数
create view IS_SOME
as
select sname,sc.sno,sc.cno
from student,sc,course
where student.sno = sc.sno and sc.cno = course.cno
with check option;#创建视图,创建视图有连接表,有相同的列名,要选其中一个,不然报错
select sname
from is_some 
group by sno
having count(*)=8;#从视图里查询数据
集合查询

并,交,差.
对select结果进行集合

select * from student where sdept = 'CS'
union 
select * from student where sage <=19;
#mysql不支持交与差,支持Union

#这两句在mysql例不被支持
select * from student where sdept = ‘CS’
intersect
select * from student where sage <=19;
select * from student where sdept = ‘CS’
except
select * from student where sage <=19;

基于派生表的查询
select sname 
from student,(select sno from sc where cno = '1') as sc1
where student.sno = sc1.sno;
select语句的一般格式

书中114页

3数据更新

插入数据
例:
insert into course(Cno,Cname,Cpno,Ccredit) values(1,"数据库",null,4);
insert into sc values("201215121",1,92);
插入子查询结果例:
create table dept_age
	(sdept char(15),
	avg_age smallint);

insert into dept_age
select sdept,avg(sage)
from student 
group by sdept;
修改数据

update course set Cpno = 5 where Cno = 1;
#还有多元组修改,带子查询的修改语句

删除数据
delete from sc where cno = "8";#删除元组
delete from sc ;#删除多个元组
delete from sc where sno in 
	( select sno 
	from student 
	where sdept='CS'
	);#带子查询的删除条件

4空值的处理

1null
2判断,is null ,is not null
3约束,unique约束,码,not null 这三个约束不能为空
4空值的运算
空值比较运算的结果为unknown,有了unknown后,逻辑判断的结果有三种:True,False,unkown
查询语句中,只有TRUE的结果才会被查询到

5视图

视图的定义

视图是一张虚表。
create view IS_SOME
as
select sname,sc.sno,sc.cno
from student,sc,course
where student.sno = sc.sno and sc.cno = course.cno
with check option;

定义删除视图

drop view is_some ;

查询视图

和正常查询表格一样。

更新视图

和更新表格一样,有些不一样的点。

视图的作用

1简化用户操作
2多角度看待同数据
3为重构数据库提供了逻辑独立性
4视图能够对机密数据提供安全保护
5适当利用视图可以更清晰的表达查询

##2020年6月12号

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值