关于数据库系统概念的一些简单总结

版权声明:未经允许禁止转载 https://blog.csdn.net/weixin_38481963/article/details/87864937

数据库
前提概念:
关系:指代表
元组:指代行
属性:指代列
空(null):表示未知或者不存在

超码:是一个多属性的集合,这些属性的集合可以使我们唯一的标识一个元组。
候选码:最小的超码成为候选码。
主码:设计者选中的在一个关系中区分不同元组的候选码。
外码:一个关系模式(r1)在它的属性中包含另一个关系模式(r2)的主码,这个属性在r1上叫做参照r2的外码。

基本数据类型:
char(n);
varchar(n);
int;
smallint
numeric(p,d); 定点数,这个数有p位数字(加上符号位),其中d位数字在小数点右边。
real,double precision;
float(n);精度至少为n位的浮点数。

SQL语句:
1、建表语句

create table department
(
dept_name varchar(20),
building varchar(15),
budget numeric(12,2),
primary key(dept_name),
foreign key(budget) references (table_name)
);

2、插入语句(增)

insert into person (sex,name,age) values(1,’tom’,28);

3、删除语句(删)

delete from person where name=’tom’;
delete from person; 删除整张表中的数据
4、删除关系

drop table r;

5、为已有关系增加属性

alter table r add A D; A为属性名,D为属性域
alter table person add weight numeric(5,2);

6、删除一个属性

alter table r drop A; 删除属性A

7、查询语句(查)

select name,age,weight from person;
select distinct name,age,weight from person;

8、自然连接

自然连接作用于两个关系,并产生一个关系作为结果。
自然连接只考虑在两个关系上具有相同属性值相等的元组对。

select name,course_id
from instructor natural join teaches;

等价:

select name,course_id
from instructor, teaches
where instructor.ID = teaches.ID;

在指定属性上进行连接

select name,course_id
from instructor join teaches using(ID);

9、更名运算

select T.name,S.course_id
from instructors as T,teaches as S;

10、字符串运算

百分号(%):匹配任意字串
下划线(_):匹配任意一个字符串
反斜杠(\):转义字符

select dept_name
from department
where building like ‘%Waston%’;

11、排列元组的显示次序

select name
from instructors
where dept_name=’Physics’
order by name;

默认是升序排序:asc
降序排序(desc)

12、集合运算

union运算会自动去除重复

(select course_id
from section
where semester=’Spring’ and year=2009)
union
(select course_id
from section
where semester=’Fall’ and year=2008)

intersect运算会自动去除重复

(select course_id
from section
where semester=’Spring’ and year=2009)
intersect
(select course_id
from section
where semester=’Fall’ and year=2008)

except运算

(select course_id
from section
where semester=’Spring’ and year=2009)
except
(select course_id
from section
where semester=’Fall’ and year=2008)

13、空值(null)

算数表达式的任一输入为空,则该算数表达式的结果为空。(+,-,*,/)
unknown:涉及空值的任何比较运算的结果为unknown。(>,=,<)

14、聚集函数

avg:平均值
min:最小值
max:最大值
sum:总和
count:计数。

15、分组聚集

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

16、having子句

having子句用于对group by子句产生的分组进行限定

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

17、测试成员资格

select distinct course_id
from section
where semester=’Spring’ and year=2009 and
sourse_id in (select course_id
from section
where semester=’Fall’ and year=2008);
select distonct name
from instructor
where name not in (‘tom’,’peiqy’); 单属性测试

select count (distinct ID)
from takes
where (course_id,sec_id,semester,year) in (
select course_id,sec_id,semester,year
from teaches
where teaches.ID = 10101);

18、集合比较

>some :至少比某一个大
=some :和其中的一些值相等 相当于 in
<>some :和部分不相等
>all,<all,<>all…

19、空关系测试

用exists 和not exists测试某个元组是否存在

select course_id
from section
where semester=’Spring’ and year=2009 and
exists(select *
from section
semester=’Fall’ and year=2008
)

20、from子句中的子查询

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

21、with子句

with子句在这里创建了一个新的关系max_budget,该关系只有一个属性value。

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

22、标量子查询

只要改子查询只返回包含单个属性的元组。

select department,
(select count(*)
from instructor
where department.dept_name=instructor.dept_name )
as num_insturctor
from department;

23、更新元组

update insturctor
set salary=salary*1.2
where salary<700000;

24、创建视图

create view view_name as
select *
from table_name
where…

25、完整性约束

完整性约束保证授权用户对数据所做的修改不会破坏数据的一致性。防止对数据的意外破坏。

26、单个关系上的约束

not null
unique
check(<谓词>)

27、参照完整性

在一个关系中给定属性集上的取值也在另一关系的特定属性集的取值中出现,这种情况称为参照完整性。

在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_38481963/article/details/87864937