45fan.com - 路饭网

搜索: 您的位置主页 > 网络频道 > 阅读资讯:秋小辰的专业课复习整理(一)(数据库方面)

秋小辰的专业课复习整理(一)(数据库方面)

2019-03-29 05:54:31 来源:www.45fan.com 【
最后更新日期 更改内容
2019.3.23 简单梳理知识点
   

目录

一、数据库系统基本概念

1、文件处理系统(file-processing system)的弊端(数据库系统的主要目标):

2、数据库系统的数据抽象:

3、数据模型:

4、数据库操纵语言(Data-Manipulation Language,DML)

数据库定义语言(Data-Definition Language,DDL)

5、一致性约束

二、关系模型

三、SQL

数据定义

create

insert

delete/drop

alter

单关系查询

多关系查询

自然连接

附加基本运算

集合运算

1、并运算:union运算自动去除重复。如果想保留重复用 union all

2、交运算:intersect 运算自动去除重复。如果想保留重复用 intersect all

3、差运算:except 运算自动去除重复。如果想保留重复用 except all

聚集函数

avg min max sum count

group

having

嵌套子查询

in / not in

some/all

exists/not exists

unique/not unique

with

数据库的修改

delete

insert

update

四、数据库设计

五、事务管理


 

一、数据库系统基本概念

1、文件处理系统(file-processing system)的弊端(数据库系统的主要目标):

  1. 数据的冗余和不一致
  2. 数据访问困难
  3. 数据孤立:数据分散在不同文件中,检索数据困难
  4. 完整性问题:一致性约束处理困难。如余额必须大于0,年龄必须在某一区间
  5. 原子性问题
  6. 并发访问异常
  7. 安全性问题

2、数据库系统的数据抽象:

物理层、逻辑层、视图层

3、数据模型:

  1. 关系模型:关系模型用的集合来表示数据与数据间的联系
  2. 实体-联系模型(E-R模型):现实世界由一组称作实体的基本对象以及这些对象间的联系构成。
  3. 基于对象的数据模型:面向对象的数据模型可以看做是E-R模型增加了封装、方法(函数)和对象标识等概念的扩展。
  4. 半结构化的数据模型:半结构化的数据模型允许那些相同类型的数据项含有不同属性表的数据定义。

4、数据库操纵语言(Data-Manipulation Language,DML)

数据库定义语言(Data-Definition Language,DDL)

5、一致性约束

  1. 域约束:取值范围
  2. 参照完整性:一个关系中某属性的取值在另一个关系中的某一属性集中出现。如,学生信息(表)中每个学生的学院(属性)必须是学校机构(表)中学院(属性)中的某一个。
  3. 断言:一个断言就是数据库需要时刻满足的某一条件。如,每学期每个学院必须开设至少5门课
  4. 授权:增删改查权限

二、关系模型

  • 基本概念
关系(relation)
元组(tuple)
属性(attribute)

域:对于关系的每个属性,都存在一个允许取值的集合,成为该属性的域

超码(super key)

候选码(candidate key)

主码(primary key)

外码(foreign key)

三、SQL

SQL主要内容:

1、数据库定义语言(DDL )定义、删除、修改关系

2、数据库操纵语言(DML)查询信息、插入删除修改元组

3、完整性

4、视图定义

5、事务

6、嵌入式SQL动态SQL

7、授权

样例code:

数据定义

create

create table department
(  dept_name   varchar(20),
   building   varchar(15),
   budget    numeric(12,2),  --12位的数,小数点后保留2位
   primary key (dept_name)
);
create table course
(  course_id   varchar(7),
   title     varchar(50),
   dept_name   varchar(20),
   credits    numeric(2,0),
   primary key (course_id),
   foreign key (dept_name) references department
);
create table instructor
(    ID    varchar(5),
     name   varchar(20) not null,
     dept_name varchar(20),
     salary  numeric(8,2),
     primary key (ID),
     foreign key (dept_name) references department
);

insert

insert into instructor 
  values(10211,'Smith','Biology',66000);

delete/drop

delete from student;  --从student关系中删除所有元组
drop table student;   --删除student关系

alter

alter table r add A D;  --为关系r添加属性  A为待添加属性名字 D为待添加属性的域
alter table r drop A;   --从关系r中删除属性

单关系查询

select name
from instructor;

--强行删除重复
select distinct dept_name
from instructor;

--强行显示重复
select all dept_name
from instructor;

select ID,name,dept_name,salary*1.1
from instructor;

select name
from instructor
where dept_name = 'Comp.Sci' and salary >70000;
--where 语句中可使用 and or not

多关系查询

select name,instructor.dept_name,building
from instructor,department
where instructor.dept_name=department.dept_name
/*
这里的表是叉乘关系
相当于
for each 元组 t1 in 关系 r1
  for each 元组 t2 in 关系 r2
    ...
    for each 元组 tm in 关系 rm
    把t1,t2,...,rm连接成单个元组t
    把t加入结果集中
*/

自然连接

自然连接只考虑那些在两个关系模式中都出现的属性上取值相同的元组对

select name,course_id
from instructor,teaches
where instructor.ID = teacher.ID
--等价于
select name,course_id
from instructor natural join teaches;

select name,title
from (instructor natural join teaches) join course using (course_id );

附加基本运算

select name as T.name,id as S.course_id
from instructor as T,teaches as S
where T.ID=S.ID

upper(s) --将字符串转换为大写
lower(s) --将字符串转换为小写
trim(s) --去掉字符串后面的空格

like 模式匹配
% --匹配任意字符串
_ --匹配一个字符

select dept_name
from department
where building like '%Watson%';

like 'ab\%cd%' escape '\' --匹配所有以“ab%cd开头的字符串”

select instructor.*
from instructor,teaches
where instructor.ID=teaches.ID


select *
from instructor
order by salary desc,name asc;  --按薪水降序、按名字升序

select name 
from instructor
where salary between 90000 and 100000

集合运算

1、并运算:union运算自动去除重复。如果想保留重复用 union all

(
select course_id
from section
where semester = 'Fall' and year = 2009
)
union
(
select course_id
from section
where semester = 'Spring' and year = 2010
);

2、交运算:intersect 运算自动去除重复。如果想保留重复用 intersect all

3、差运算:except 运算自动去除重复。如果想保留重复用 except all

聚集函数

avg min max sum count

select avg(salary)
from instructor
where dept_name = 'Comp.Sci';

select count(distinct ID)
from teachers
where semester = 'Spring' and year = 2010;

group

select dept_name,avg(salary) as avg_salary
from instructor
group by dept_name;

having

用来过滤指定条件的分组

select dept_name,avg(salary) as avg_salary
from instructor
group by dept_name
having avg(salary>42000)

嵌套子查询

in / not in

select distinct course_id
from section
where semester = 'Fall' and year = 2009 and
  course_id not in (
  select course
  from section
  where semester = 'Spring' and year = 2010 
  );

some/all

>some至少比某一个大

>all 比所有的都大

select name
from instructor
where salary >some(
  select salary
  from instructor
  where dept_name = 'Biology'
  );

exists/not exists

unique/not unique

with

with子句提供定义临时关系(表)的方法

with max_budget(value) as
  (
  select max(budget)
  from department
  )
select budget
from department, max_budget
where department.budget=max_budget.value

数据库的修改

delete

insert

update

update instructor
set salary = salary *1.03
where salary>100000


update instructor
set salary = case
        when salary <=100000 then salary*1.05
        else salary * 1.03
       end

四、数据库设计

五、事务管理

 

 
 

本文地址:http://www.45fan.com/a/question/99827.html
Tags: 复习 秋小 专业课
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部