Database review

basic concepts

1. DBMS concept

  • Database (DB) A
    database is a collection of related data that is stored in the computer for a long time, is organized, uniformly managed, and can be shared
    (in fact, it is the data storage)
  • A database management system (DBMS) is
    a software for managing databases, and provides users with methods to access DB, including DB creation, query, update and various data control. Located between the user and the OS.
    (In fact, it is to operate the database)
  • Database System (DBS)
    DBS is a system composed of computer hardware, software and data resources that realizes the organized and dynamic storage of large amounts of associated data and facilitates multi-user access.
    (The broadest concept, generally including DB, DBMS, DBA, etc.)

2. The difference between table and view

Table: It is the data table, which is the real relationship in the database.
View: It is a view, which is a table derived from one or several basic tables. Although it is also a relational form, it does not exist in the database itself. There is only a definition without data, which is a virtual relation.
Commonality: It's all a form of relationship

Difference: The data table is actually stored in the DB, which is a real relationship.
The view is just a virtual relationship, no data is stored.

3. ER

ER diagram is used to describe the relationship between things.
Insert picture description here
Insert picture description here

Insert picture description here

4. The three-level structure model of the database


  • The database under the external mode (sub mode, user mode) user concept is the interface between the user and the database.
    A database system can have multiple external modes

  • Conceptual mode (mode, logic mode)
    describes the global DB, a data view shared by all users.
    A database system can only have one conceptual model.

  • The internal mode (storage mode)
    describes the physical structure and storage mode of the data in the DB, and is the representation of the data inside the DB.
    A database system can only have one internal mode.

5. Relational model data constraints

  • Entity integrity constraints
    Entity integrity is used to ensure that each tuple in the relational database is distinguishable and unique.
    (Actually the primary key)
  • Referential integrity constraints
    Referential integrity requirements do not allow references to non-existent entities in relationships.
    (Actually it is a foreign key)
  • User-defined integrity rules
    User-defined integrity is a constraint for a specific relational database, which reflects the semantic requirements that the data involved in a specific application must meet.
    (In fact, it is to set the value range, not null and other regulations)

6. Several basic operations of relational algebra

Selection, projection, union, difference, Cartesian product

  • Select
    select legal line
  • Projection
    select legal columns
  • And
    the same properties combined two tables all rows, and removing duplicate rows
  • Difference
    Two tables with the same attributes R, S
    RS calculates a table composed of rows that belong to R but do not belong to S
  • Cartesian product
    If R has m tuples and S has n tuples, then R×S has m×n tuples
    Insert picture description here
    Insert picture description here

7. The life cycle of the database

  • Planning stage
  • Demand analysis stage
  • Concept design stage
  • Logic design stage
  • Physical design stage
  • Realization phase
  • Operation and maintenance phase

8. Five levels of security measures to protect the database

  • Environmental level
  • Staff level
  • OS level
  • Network level
  • DBS level

9. Several corresponding ways between entities

1 : 1
1 : n
n : m

10. The concept of super key, primary key, candidate key

Super key: a set of attributes that can uniquely represent a tuple in the relationship
(a super key is a collection of attributes, as long as the set contains attributes that can uniquely identify a tuple)
Candidate key: a super key that does not contain redundant attributes

Primary key: Select the key that is the tuple identifier from the candidate keys

Command design

1. Create a database

create database datebaseName;

2. Create a table

for example

create table SC(
Sno char(10) not null,
Cno char(5) not null,
Degree decimal(5,1),
foreign key (Sno) 
references Student (Sno),
foreign key (Cno)
references Course (Cno) on delete restrict,
check (Degree >= 0 and Degree <= 100 ));

3. Modify the table structure

#添加属性
alter table <表名> add <属性名><类型>;
#删除已有属性
alter table <表名> drop<属性名>;
#属性重命名
alter table <表名> change <原属性名> <新属性名> <新的类型>;
#修改属性的数据类型
alter table <表名> modify <属性名> <新的类型>;
#删除表
drop table <表名>;
#补充主键定义
alter table <表名> add primary key(<属性名表>);
#撤销主键定义
alter table <表名> drop primary key;
#补充定义外键
alter table <表名> 
add foreign key <属性> references <表名>(<属性>);
#撤消外键定义
alter table <表名> drop foreign key <外键名>;

4. Table record update

# 插入元组
insert into <表名> [(<属性列1>[, <属性列2 >)]
values (<常量1> [, <常量>]  ......   );
# 插入子查询的结果
insert into <表名>  [(<属性列1> [, <属性列2>......])]
子查询;
# 删除数据
delete from <表名>
[where <条件>];
#修改数据
update <表名>
set <列名>=<表达式>[,<列名>=<表达式>]......
    [where <条件>];

5. Query creation

#查找格式
select [distinct] <属性列表>
from <表名/视图名>
[where <条件>]
[group by <分组列表名> [having <组合条件>]]
[order by <排序列名> [asc | desc]]

#集函数
count([distinct|all] *) #计算元组个数
count([distinct|all] <列名>) #计算某一列值个数,空值也计算
sum([distinct|all] <列名>) #计算某一列总和
avg([distinct|all] <列名>) #计算某一列平均值
max([distinct|all] <列名>) #计算某一列最大值
min([distinct|all] <列名>) #计算某一列最小值
#having中可以使用集函数作为条件,having不能单独出现,只能和group by一起出现

For more query examples, please refer to hands-on exercises 3 and 4

## 下面是几个例子

# 查询指定列
select Sname, Ssex, Sbirthday from student
where Speciality='电子商务';
# 查询全部列
select * from student
where(Ssex = '女' and 2020 - year(Sbirthday)>20 );
# 使用别名改变列标题
select sdept as '系', count(Sno) as '人数'
from student
group by sdept;

6. View creation

# 视图创建语句
create view <视图名>  [(<列名>  [,<列名>]......)]
as <子查询>
[with check option];
# 删除视图
drop view if exists <视图名>;

7. The creation of a simple trigger

#只有表才有触发器,视图不支持触发器
create
    [definer = { user | current_user }]
    trigger trigger_name
    trigger_time trigger_event
    on tbl_name for each row
    trigger_body
    #举个例子
create trigger trigger_student_count_insert
after insert on student_info for each row
update student_count #表名
set student_count=student_count+1;

Guess you like

Origin blog.csdn.net/mwl000000/article/details/108541502