【CMU15-445 Part-1】Introduction And Relation Model

Part1-Course Intro & Relation Model

CMU15-445/645

Organized collection of inter-related data that models some aspect of the real-world.

例子

有如下实体集,artist和album

image-20220808213651081

在存储的时候

  1. 如何保证

    image-20220808214159353

  2. What if somebody overwrites the album year with an invalid string?

  3. How do we store that there are multiple artists on an album?

在查询数据的时候

  1. 如何找到特定记录?数据量很大的时候
  2. image-20220808215026028
  3. What if two threads try to write to the same file at the same time? 事务

DURABILITY,安全

  1. What if the machine crashes while our program is updating a record? 如何在不同的机子上备份 或者 分布式存储
  2. What if we want to replicate the database on multiple machines for high availability

A DBMS is software that allows applications to store and analyze information in a database.

DBMS是一个允许应用存储或者分析信息的软件

History

Early DBMSs

革命性的paper

《A Relation Model of Data for Large Shared Data Banks》

关系模型三要素

表和表之间建立关系

  1. 将关系转化为简单的数据结构然后存入数据库;store database in simple data structures
  2. Access data through high-level language 通过高级语言获取数据,或者说提供API to APP?
  3. Physical storage left up to implementation 物理存储,物理层和逻辑层独立 分开的?

Data Models

数据模型:本质是说如何组织数据

关系型数据库:PG、Oracle、DB2、SQL server,SQLite

image-20220810235559173

NoSQL: KV 图 docu 列簇

MultiValue数据模型:

  • hierachical:层次数据模型
  • Nerwork:网络数据模型

关系模型三部分

  1. 关系结构 Structure
  2. Integrity:数据的完整性约束
  3. Maipulation:操作,如何获取修改数据库的内容

例子

关系只是一组无序的元素或者记录,这些元素或者记录的属性用来表示关系中的实体的实例。

使用tuple来记录一条数据,所有值都得原子性:不能是数组或者嵌套对象。上面是刚提出RM的时候,现在可以在关系型数据库存JSON对象、数组。

特殊值NULL。

realtion = table

tuple is 记录

Primary Key

主键:如果某一个唯一的属性或者属性组能够唯一标识一条记录,称为主键。通过逻辑层暴露的一个代理键

Foreign Key

外键用于指定一张表里面的属性必须存在于另外一张表中

外键约束的例子:

image-20220811005326505

改进之后

image-20220811005534427

DML

Data Manipulation Languages

如何从DB中存取信息

DML是一种操作数据的方式通过它可以用来访问或者修改数据库来生成想要的result

  1. procedural 程序:指定高级策略让数据系统帮忙找结果
  2. 非程序性的、声明式的:

image-20220811005951990

一种是关系代数、一种是关系演算。

关系代数和关系演算的区别?

Answer:


2. 非程序性的、声明式的:

[外链图片转存中…(img-szoawlJy-1660151001038)]

一种是关系代数、一种是关系演算。

关系代数和关系演算的区别?

Answer:

关系代数

七种基本运算符

选择、投影、并、交、差、乘、连接

select,projection,union,intersection,difference,product,join

输入关系,输出新关系。

Select

image-20220811225156739

Projection

投影运算。

image-20220811230035560

Union

并运算,将两个关系组合生成一个新的关系。

简单的合并就行。级联输出。

必须俩关系相同的关系模式。

SQL:Union ALL

Intersection

交,得到都在两个关系里面出现的tuple

SQL: 有intersect 关键词

Difference

R-S:只取出来第一个关系出现的元素,而不是第二个关系里面的。就是找第一个关系中不在第二个关系的元组。

sql:except

Product

R x S 笛卡尔积 sql:cross join

image-20220811231046980

Join

特别指自然连接natural join

image-20220811231319189

查询优化

image-20220811232352303

先投影后连接效率更高

但是不能依靠写SQL的人去人工优化

猜你喜欢

转载自blog.csdn.net/qq_47865838/article/details/126277245