Skip to content

Relational Model 

Data Model

  • Relational
  • NoSQL
    • Key/Value
    • Graph
    • Document
    • Column-family
  • Machine Learning
    • Array/Matrix
  • Obsolete/Legacy/Rare
    • Hierarchical
    • Network
    • Multi-Value

Relational Model

  • Structure: 数据库关系和内容的定义
  • Integrity: 确保数据库内容满足其约束
  • Manipulation: 数据库内容增删改查的编程接口

Relation

An unordered set that contains the relationship of attributes that represent entries.

n\displaystyle{ n }-ary relation = Table with n\displaystyle{ n } columns

Tuple

A set of attribute values (also known as domain) in the relation.

  • Values are (normally) atomic/scalar (原子量/标量)
  • The special value NULL is a member of every domain

Primary key

Uniquely identifies a single tuple.

Some DBMSs automatically create an internal primary key if a table does not define one.

Auto-generation of unique integer primary keys

  • SEQUENCE
  • AUTO_INCREMENT

Foreign keys

Artist (id, name, year, country)

idnameyearcountry
123Wu-Tang1992USA
456Notorious BIG1992USA

Album (id, name, artist, year)

idnameartistsyear
11Enter the Wu-Tang1231993
22St.Ides Mix Tape???1994

Create a new Table: ArtistAlbum (artist_id, album_id), the ids will be linked to the previous tables.

artist_idalbum_id
12311
12322
45622

Data Manipulation Languages (DML)

  • Procedural
    • The query specifies the (high-level) strategy the DBMS should use to find the desired result.
  • Non-Procedural (Declarative)
    • Not how to find it.
    • What data is wanted.

Relational Algebra

Select

Choose a subset of the tuples that satisfies a selection predicate.

Syntax: σpredict(R)\displaystyle{ \sigma _{ \text{predict} } \left( R \right) }

select * from R
where a_id='a2' and b_id>102;

Projection

Generate a relation with tuples that contains only the specified attributes.

Syntax: ΠA1,A2,,AnR()\displaystyle{ \Pi _{ A _{ 1 } , A _{ 2 } , \ldots , A _{ n } } R \left( \right) }

select b_id-100, a_id
from R where a_id='a2';

Πbid100,aid(σaid=a2)(R)\displaystyle{ \Pi _{ \mathtt{ b _{ i d } - 100 , a _{ i d } } } \left( \sigma _{ \mathtt{ a _{ i d } = ^{\prime} a 2 ^{\prime} } } \right) \left( R \right) }

Union

Syntax: (RS)\displaystyle{ \left( R \cup S \right) }

(select * from R) union all (select * from S);

Intersection

Syntax: (RS)\displaystyle{ \left( R \cap S \right) }

(select * from R) intersection (select * from S);

Difference

Syntax: (RS)\displaystyle{ \left( R - S \right) }

(select * from R) except (select * from S);

Product

Generate a relation that contains all possible combinations of tuples from the input relations.

两个表相乘,组成所有可能的组合,Tuple ×\times Tuple

Syntax: (R×S)(R \times S)

select * from R cross join S;
select * from R, S;

Join

Generate a relation that contains all tuples that are a combination of two tuples (one from each input relation) with a common value(s) for one or more attributes.

Syntax: (RS)\displaystyle{ \left( R \bowtie S \right) }

R (a_id, b_id)

a_idb_id
a1101
a2102
a3103

S (a_id, b_id)

a_idb_id
a3103
a4104
a5105

RS\displaystyle{ R \bowtie S }

a_idb_id
a3103