[Database] Introduction to Database Systems (2) - Relationships

relational database

A relational database is a database system that supports a relational model.

Data structure of relational model: relationship; logical structure: flat two-dimensional table

area

Is a collection of values ​​of the same data type.

For example: {a,b,c}, {0,1,2,3}, etc.

Cartesian Product

definition

The Cartesian product is a set operation over a field.

Calculation formula

Given a set of domains: D1, D2, D3,…,Dn [Duplicate domains are allowed]

The formula for calculating the Cartesian product is:

D 1 × D 2 × D 3 × … × D n = { ( d 1 , d 2 , d 3 , … , d n ) ∣ d i ∈ D i , i = 1 , 2 , … , n } D1×D2×D3 ×…×Dn = {\{(d1,d2,d3,…,dn) | di ∈Di, i = 1,2,…,n\}}D1×D2×D3××Dn={(d1,d2,d3,,dn)diDi,i=1,2,,n}

Name translation

  1. (d1, d2, d3,…,dn) is called an n-tuple (referred to as a tuple)

  2. di is called component.

Cardinality

The number of different values ​​allowed for a domain is called the cardinality of the domain.

For a finite set Di, the base is mi, then the base M of the Cartesian product D1×D2×D3×…×Dn is:

M = Π n i = 1 m i \Pi{n \atop i=1}m_i Pii=1nmi

Insert image description here
Example 1

relation

definition

The finite subset of D1×D2×…×Dn is called the relationship on the domain D1, D2,…,Dn, expressed as R(D1,D2,D3,…,Dn)

R represents the name of the relationship, n is the order or degree of the relationship

A relationship is a two-dimensional table, each row in the table corresponds to a tuple, and each column in the table corresponds to a field.

Since the fields can be the same, each column must have a name, which is called attribute.

n-order relations must have n attributes

Glossary:

  1. Each element in the relation is a tuple in the relation, usually represented by t
    • When n = 1, the relationship is a unit relationship
    • When n = 2, the relationship is a binary relationship
  2. Candidate code: The value of a certain attribute group in the relationship can uniquely identify a tuple, but its subset cannot, then the attribute group is called a candidate code.
  3. Primary code: There are multiple candidate codes for a relationship, and one of them must be selected as the primary code.
  4. Main attributes: The attributes of the candidate code are called main attributes
  5. Non-primary attributes: attributes that are not included in any candidate code are called non-primary attributes (or non-code attributes)
  6. Full code: All attributes of the relational pattern are candidate codes for this relational pattern. This candidate code is called full code.

Three types of relationships

  1. Basic relationship (basic table/base table)

    It is an actual table and a logical display of the actual stored data.

  2. lookup table

    Is the table corresponding to the query result

  3. view table

    It is a table derived from a basic table or other view table. It is a virtual table and does not correspond to the actual stored data.

The nature of the basic relationship

  1. Columns are homogeneous (the components in each column are the same type of data and come from the same domain)
  2. Different columns can come from the same domain, and each column is called an attribute. Different attributes should be given different attribute names.
    1. For exampleExample 1, we can also divide it into two domains, person{张清玫、刘逸、李勇、刘晨、王敏} and specialty{计算机专业、信息专业}. The person domain is divided into two attributes, 研究生和导师.
  3. The order of the columns does not matter, i.e. the order of the columns can be interchanged at will
  4. The order of the rows does not matter, i.e. the order of the rows can be interchanged at will
  5. The candidate codes of any two tuples cannot have the same value.
  6. ⭐Components must take atomic values, that is, each component is an indivisible data item

This standardized relationship is referred to as a paradigm.

relationship model

The description of relationships is called a relationship schema.

R ( U , D , D O M , F ) R(U,D,DOM,F) R(U,D,DOM,F)

  • R: relationship name
  • U: The set of attribute names that make up the relationship
  • D: The domain from which all attributes in U come
  • DOM: a collection of images of attribute-like domains
  • F: The collection of data dependencies between attributes

can also usually be abbreviated as R ( U ) R(U) R(U)

relational operations

Basic relational operations

Including: query and insertion, modification and deletion.

Query operation

  1. SELECT select
  2. PROJECT projection
  3. JOIN connection
  4. DIVIDE except
  5. UNION and
  6. EXCEPT difference
  7. INTERSECTION
  8. Cartesian Product

relational language

Insert image description here

SQL language is a highly non-procedural language. That is to say, if you want to query a certain indicator, the relational database will choose the optimal query path for it to improve query efficiency.

integrity

Entity integrity

rule:

If attribute (one or a group) A is the main attribute of basic relation R, then A cannot take a null value.

referential integrity

Foreign code concept

Suppose F is an attribute or a set of attributes of the basic relation R, but not the key of the relation R, and Ks is the primary key of the basic relation S. If F corresponds to Ks, then F is said to be the foreign code of R. The basic relationship R is also called the reference relationship, and S is the referenced relationship (target relationship).

Insert image description here

rule

If the attribute F is the foreign key of the basic relationship R, which corresponds to the primary key Ks of the basic relationship S (the basic relationships R and S are not necessarily different relationships), then for each tuple in R, the value of F for:

  1. Null value (each attribute value in F is null)
  2. The primary key value of a tuple in S

User defined integrity

The data involved in a specific application must meet semantic requirements

relational algebra

operator meaning
set operator
intersection
union
- difference set
× Cartesian Product
Relational operators
p choose
Pi projection
connect
÷ remove

set operator

Let's take the set R and set S as an example.

And ∪

R ∪ S = { t ∣ t ∈ R ∪ t ∈ S } R∪S=\{t|t∈R∪t∈S\} RS={ ttRtS}

Difference -

R − S = { t ∣ t ∈ R ∩ t ∉ S } R - S = \{t|t∈R∩t∉S\} RS={ ttRt/S}

cross ∩

R ∩ S = { t ∣ t ∈ R ∩ t ∈ S } R∩S=\{t|t∈R∩t∈S\} RS={ ttRtS}

Cartesian Product

R × S = { t r t s ∣ t r ∈ R ∩ t s ∈ S } R×S = \{t_rt_s|t_r∈R∩t_s∈S\} R×S={ trtstrRtsS}

Illustration of traditional set operations

Insert image description here

Guess you like

Origin blog.csdn.net/qq_41675812/article/details/132859075