Database Principle and Application Series_03 Relational Database and Relational Algebra

1. The data structure of the relational model and its formal definition

The data structure of the relational model is very simple, containing only a single data structure - the relationship.
In the relational model, whether it is an entity or a relationship between entities, it is represented by a single structural type, the relationship.

The relational model is based on the theory of set algebra.

1. Formal definition of relationship and related concepts

  1. Domain

A domain is a collection of values ​​with the same data type, also known as a value domain (D). For example: sets of integers, real numbers, and strings are all fields.

The number of values ​​contained in a field is called the cardinality (m) of the field.

In the relationship, the domain is used to represent the value range of the attribute.

For example: D1={Wang Wei, Zhang San, Li Si}, m1=3

D1 represents the set of name fields.

The domain names are not arranged in order, such as: D2={male, female}={female, male}

  1. Cartesian Product

Cartesian product is also a set
[1] Each value di in each element (d1, d2, d3..., dn) is called a component, and the component comes from the corresponding domain.
[2] Each element (d1, d2, d3..., dn) is called an n-element ancestor, referred to as an ancestor. But Yuanzu is in order.

Cartesian products can be represented using a two-dimensional table.
insert image description here
3. Relationship

Relations are subsets of Cartesian products.

Any subset of the Cartesian product is called an n-ary relation defined on the field, and the relation is denoted by R.

R: the name of the
relationship n: the purpose or degree of the relationship

[1] R=1: unit relationship, R=2: binary relationship.
[2] Each element in the relation is a tuple, denoted by t. The number of tuples in a relation is the cardinality of the relation.
insert image description here
[3]
Infinite relation: The number of tuples in a relation is infinite

Finite relation: The number of tuples in a relation is limited

[4] The relation is a subset of the Cartesian product, so the relation is also a two-dimensional table

1. The frame of the table is composed of fields, that is, each column of the table corresponds to a field.

2. Each row of the table corresponds to a tuple

3. Each column has a unique name, called an attribute .

An n-ary relation must have n attributes.

The value range of the attribute is called the value domain , which corresponds to the value range in the domain.

[4] Relations with the same relational framework are called homogeneous relations

[5] A relationship defined on a domain consists of a relationship header and a relationship body

The relationship header is a collection of attribute names (A1, A2, A3)

The relationship header is a description of the data structure of the relationship and is immutable

The relationship body refers to the content or data in the relationship structure, which changes with the insertion, deletion, and modification of tuples . [Test] [Just add, delete, and change, but no query, because the query is only read only]

2. The nature of the relationship

A relationship is a normalized collection of rows in a two-dimensional table.

The nature of the relationship:

[1] The columns are homogeneous, that is, the components in each column must come from the same domain and must be the same type of data

[2] Different attributes can come from the same domain

[3] The order of the columns can be exchanged arbitrarily. But when swapping, it should be swapped together with the attribute name, otherwise you will get a different relationship

[4] The order of the tuples in the relation can be arbitrary, and the order of two lines can be exchanged arbitrarily in a relation. Because a relation is a collection of tuples as elements, and the elements in the collection are unordered, the tuples that are elements of the collection are also unordered.
[The tuples in the Cartesian product are ordered]

[5] The same tuple is not allowed in the relationship (de-duplication)

Tuples that are elements of a collection are unique.

[6] Each component in the relation must be an inseparable data item

All attribute values ​​are a deterministic value.

A relationship that satisfies this relationship is called a normalized relationship, otherwise it is a denormalized relationship.
insert image description here

3. Relationship Mode

In relational databases, the relational schema is the type and the relation is the value (instantiation).

A relational schema is a quintuple.

  1. A description of a relationship is called a relationship schema

The relational schema can be visualized as: R(U, D, DOM, F)

[1] R: relationship name
[2] U: set of attribute names that make up the relationship
[3] D: domain from which attributes in attribute group U come from
[4] DOM: set of images from attributes to domains
[5] F: attributes A set of dependencies between data

The relational schema can usually be simplified as: R(U) or R(A1,A2,…An)

[1] R: Relationship name

[2] U: set of attribute names

【3】A1,A2,…An: Each attribute name

The relationship schema is the type, the relationship header

Relationships are values, the body of the relationship

In practice, relations and relational models are often collectively referred to as relations

[Writing of the relationship model in the exam:]
1. Student (student number, name, age)
2. Teacher (teacher number, name, gender, salary)

4. Relational databases and relational database schemas

  1. relational database schema

  2. The value of a relational database is called a relational database. is the set of corresponding relations of the relational schema at a certain moment

  3. The current value in the database corresponding to the relational database schema is the content of the relational database and is called an instance of the relational database

2. Relational code and relational integrity

1. Candidate key and main key

  1. candidate code

An attribute or attribute set that can uniquely identify a tuple in a relationship is called a candidate key, also known as a candidate key or candidate key.

For example: in course selection,
only the attribute combination "student number + course number" can uniquely distinguish each course selection record, then the attribute combination "student number + course number" is the candidate code of the course selection relationship.

K is called a candidate code only when the following two conditions are met:
[1] Uniqueness: For any two different tuples of the relation R, the values ​​of the attribute set K are different.
[2] Minimality: In the attribute set that composes the relation key, no attribute can be deleted from the attribute set K, otherwise the uniqueness will be destroyed.

For example, the "student number + course number" in the course selection relationship satisfies the minimum, and if any attribute is removed from it, the course selection record cannot be uniquely identified.

2. Master code (PK)

If there are multiple candidate keys in a relationship, one can be selected as the operation variable for querying, inserting or deleting tuples, and the selected candidate key is called the primary key, or the primary key, key, etc.

A primary key must be selected for each relationship, and it cannot be changed arbitrarily after selection.

Usually the least combination of attributes is selected as the primary key.

3. Primary and non-primary properties

Main attribute [Prime Attribute]: Each attribute contained in the main code is called the main attribute.

Non-Prime Attribute [None-Prime Attribute]: Attributes that are not included in any candidate code are called non-prime attributes.

[Exam Judgment Question]
Each attribute contained in the candidate code is called the main attribute. (wrong, not the candidate code, but the main code)

Full code: The combination of all attributes is the candidate code for the relation, which is called the full code.

4. Foreign keys (FK)

If one or a set of attributes X of relation R2 is not the primary key of R2, but the primary key of another relation R1, then this attribute or set of attributes X is called the foreign key or foreign relation key of relation R2.

Also, the relationship R2 is referred to as a reference relationship, and the relationship R1 is referred to as a referenced relationship.

5. Relationship integrity

Insert, delete, and modify operations on relational databases must have certain constraints, which are actually real-world requirements.

Any relationship must satisfy these semantic constraints at all times.

In the relational model, there are three types of integrity constraints: entity integrity, referential integrity, and user-defined integrity.

[1] Entity integrity and referential integrity are the integrity constraints that must be satisfied in the relational model, which are called the two invariants of the relation.

[2] User-defined integrity: Different relational database systems require some special constraints due to different application environments. Semantic constraints embodied in specific domains.

  1. Entity Integrity [Exam: both fill in the blanks and judgments]

Entity integrity means that the value of the primary key cannot be null or partially null.

  1. referential integrity

If the outer code X of the relation R2 matches the primary code of the relation R1, then each value of X is either equal to a certain value of the primary code in R1, or a null value is removed.

E.g:insert image description here

  1. User Defined Integrity

The same relational database system requires some special constraints due to different application environments. Semantic constraints embodied in specific domains.

3. Relational algebra

The relational model consists of three parts: relational data results, relational operations, and relational integrity constraints.

insert image description here

1. Classification of relational algebra and its operators

insert image description here
insert image description here

[1] Traditional set operation: This type of operation regards the relationship as a set of tuples, and uses tuples as the elements of the set to perform operations, and the operations are performed from the horizontal direction of the relationship (axis=0). Traditional set operations include: union, intersection, difference, and Cartesian product.

[2] Special relational operation: This type of operation involves not only the row direction, but also the column direction. Includes selection, projection, concatenation and division operations.

[Exam Judgment Questions]
It is wrong for specialized relational operations to only include row operations. (wrong)

Except for the Cartesian product operation, other set operations require that the relations participating in the operation must satisfy the following definition of compatibility:

[1] Two relations R, S have the same number of columns
[2] The ith attribute of R and the ith attribute of S must be from the same domain

2. Traditional set operations

【1】Union

The union result of relation R and relation S consists of tuples belonging to R or S. That is, all tuples of R or S are merged, and the duplicate tuples are deleted to form a new relationship, and the result is still an n-ary relationship.
insert image description here
insert image description here

[2] Difference

The difference operation result of relation R and relation S consists of tuples that belong to R but not S. That is, delete the same tuple as in S from R to form a new relationship, and the result is still an n-ary relationship.

insert image description here
insert image description here

【3】Intersection

The result of the intersection of relation R and relation S consists of tuples that belong to both R and S.

insert image description here
insert image description here

[4] Generalized Cartesian product

insert image description here

3. Specialized relational operations

insert image description here
insert image description here
Course selection table
insert image description here

【1】Select

insert image description here
insert image description here

So the selection is to pick out the eligible rows from the row perspective.

insert image description here
insert image description here

【2】Projection

insert image description here
insert image description here
insert image description here
insert image description here

[3] Join (join)

insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here

example:
insert image description here
insert image description here
insert image description here

[4] Division (Division)

insert image description here

[1] The attributes of R ÷ S are attributes in R but not in S.
[2] The tuple of R ÷ S is combined with the tuple of S, and the result of the combination must be in R

write picture description here
Step 1: Find the same attribute in relation R and relation S, i.e. attribute Y. Project Y in relation S (that is, take out column Y); the result is as follows

insert image description here

Step 2: The attribute column in the excluded relation R that is different from that in S is X, and the projection of the relation R to cancel the duplicate value on the attribute (X) is {X1, X2};
Step 3: Find the X in the relation R The image set Y corresponding to the attribute
can obtain the records related to the value of X1 according to the records of the relationship R, as shown in Figure 3; the records related to X2, as shown in Figure 4
write picture description here

Step 4: Judging the inclusion relationship
R÷S is actually judging whether the image set Y of each value of X in the relationship R contains all the values ​​of the attribute Y in the relationship S. By comparison, it can be found that
the image set of X1 has only Y1 and cannot contain all the values ​​of the attribute Y in the relationship S, so X1 is excluded;
and the image set of X2 contains all the values ​​of the attribute Y in the relationship S, so the final value of R÷S The result is X2
write picture description here

Guess you like

Origin blog.csdn.net/wxfighting/article/details/124385168