[MySQL Self-study Road] Day 2 - Relational Algebra Calculation [Theoretical Knowledge]

Table of contents

foreword

base noun

relation

candidate code

Relational operations

Traditional set calculation (binary operation)

Create a sample table [SQL]

specialized relational operations

postscript

Destroy the created table


foreword

In the previous section, we mentioned the relationship between relational databases and non-relational databases. We mainly focus on the MySQL relational database. The relational model is based on set algebra, and we give a formal definition of relational data structure from the perspective of set theory.

Note: The blue background of the blog is the original text of the textbook, the yellow background is the blogger's own understanding, and the green background is the quotation.


base noun

relation

definition:

A subset of D1xD2xD3x...xDn is called a relation on the domain D1, D2,..., Dn, denoted as R(D1, D2,...,Dn) 

Here R is the name of the relation and n is the purpose or degree of the relation . 

An n-item relationship must have n attributes.

Here D is not an attribute, but an element in R, and an attribute is a subdivision of D.

For example: R(AB, B, BC), the attributes are A, B, C

candidate code

If the value of a certain attribute group can uniquely identify a tuple, but its subset cannot, then the attribute group is called a candidate key .

The above sentence means that every attribute or element in the relation R can be deduced by the candidate code.

The attributes of candidate codes are called primary attributes.

Attributes not included in the candidate key are called non-primary attributes.


Relational operations

Note: Each relational operation can be implemented with SQL statements.

Traditional set calculation (binary operation)

(1) and

R ∪ S = {t | t ∈ R ∨ t ∈ S}

(2) Poor

R - S = {t | t ∈ R ∧ t  S} 

(3) pay

R ∩ S = {t | t ∈ R ∧ t ∈ S}

R ∩ S = R -(R - S)

(4) Cartesian product

R x S = {tr ts | tr ∈ R ∧ ts ∈ S}

Example: R(a,b) S(c,d)

R x S = {(a, c),(a, d),(b, c),(b, d)}

Among them, (a, c) and so on also become tuples, and a and c in the tuple are respectively called components of the tuple.

Create a sample table [SQL]

(1) Create database sqlstudy

(2) Create a table students under the database sqlstudy

(3) There are three fields in the students table: sno, sname, and sage

(4) Now save the information of two students: ( '123456789','aaa',12 ), ('111111111', 'bbb', 13)

create database sqlstudy;

use sqlstudy;

create table students(
sno char(9) primary key,
sname char(20),
sage int
);

insert
into students
values('123456789','aaa',12);

insert
into students
values('111111111', 'bbb', 13);

select * from students;

Note: The SQL here does not need to be memorized, it is just for the convenience of demonstrating the effect, and the SQL statements will be explained one by one later.

specialized relational operations

(1) choose

Selection is also known as restriction. It is to select tuples satisfying given conditions in relation R.

δF(R) = {t | t ∈ R ∧ F(t)='真'}

Among them, F represents the selection condition, which is a logical expression, and takes the logical value "true" or "false".

For example: Query all students whose age is greater than 10 years old.

 δ sage > 10 (students)

SQL statements:

select *
from students
where sage > 10;

Summary:
The selection is to find out all the information of all the students who meet the condition. is a row query operation.

(2) projection

The projection on the relation R is to select several attribute columns from R to form a new relation. The projection operation is an operation performed from the perspective of columns.

ПA(R) = {t[A] | t ∈ R} 

For example: Query who are the people (names) in the student relationship students.

 Пsname(students)

SQL statements:

select sname
from students;

 Summarize:

The projection operation is an operation performed from the perspective of columns.

(3) connection

Connections are also known as theta connections. It selects tuples that satisfy certain conditions between attributes from the Cartesian product of two relations.

The join operation where θ is "=" is called an equivalent join.

Natural connection:

The components to be compared in the two relations must be attribute groups with the same name, and duplicate attribute columns will be removed in the result.

A natural join is a special kind of equijoin.

Summarize:

Connections are based on multiple database tables. By joining, the data of multiple tables can be spliced ​​through common fields, and the spliced ​​tables remove the repeated fields, which is called natural join.

SQL statements:

create table homes
(
sno char(9) primary key,
shome char(20)
);

insert
into homes
values('123456789','hahaha');

insert
into homes
values('111111111', 'xixixi');

select *
from homes, students
where homes.sno=students.sno;

  

The above situation is that the data of the two tables are in one-to-one correspondence. If I add another data to the table at this time:

insert
into homes
values('222222222', 'lalala');

select * 
from homes;

At this point in the connection:

select *
from homes, students
where homes.sno=students.sno;

Note: You can see that the newly added row of data does not appear in the connection, because there is no information about this student in the students table. We call this discarded tuple (a row of data) a suspended tuple.

Suspension tuple:

When two relations R and S are doing a natural connection, select tuples with equal values ​​on the common attributes of the two relations to form a new relation. At this time, some tuples in the relation R may not have tuples with equal values ​​in common attributes in S, so these tuples in R are discarded during the operation. These discarded tuples are called floating tuples.

Outer join:

If the floating tuple is also stored in the result relationship, and the NULL value is filled in other attributes, then this connection is called an outer connection. 

Left outer join:

Only keep the floating tuple of relation R on the left, and write null values ​​in other places.

Right outer join:

Only keep the floating tuple of the relation S on the right, and write null values ​​in other places.

Note: SQL can use keywords such as outer join on to implement outer joins to retain floating tuples, and the details will be explained in later chapters. Here is only a general description.

(4) Division operation

Let the result of dividing the relationship R by the relationship S be the relationship T, then T contains all attributes and values ​​​​that are in R but not in S, and all combinations of tuples of T and tuples of S are in R.

R ÷ S = {tr[X] | tr ∈ R ∧ Пy(S) Yx} 

where Yx is the image set of x in R

Note:

(1) Division is performed from the perspective of rows and columns at the same time. 

(2) Intersection, connection, and division can be expressed by five basic operations: union, difference, Cartesian product, selection, and projection. Usually, we convert them into basic operations when calculating division.

Example from set theory:

R
A B C
a1 b1 c2
a2 b3 c7
a3 b4 c6
a1 b2 c3
a4 b6 c6
a2 b2 c3
a1 b2 c1
S
B C D
b1 c2 d1
b2 c1 d1
b2 c3 d2
R ÷ S
A
a1

calculation steps:

(1) Find the public attribute columns (B, C)

(2) Calculate all values ​​(a1, a2, a3, a4) in the relationship R that are not public attribute columns (A).

(3) Calculate the image set of the result obtained in step (2) respectively:

        a1 image set: {(b1, c2), (b2, c3), (b2, c1)}

        a2 image set: {(b3, c7), (b2, c3)}

        a3 image set: {(b4, c6)}

        a4 image set: {(b6, c6)}

(4) Calculate the projection of S on (B, C): {(b1, c2), (b2, c1), (b2, c3)}

(5) Because of   the projection of the image set S of a1 on (B, C), the result is a1.

Take an example from the SQL statement: (The realization of the division operation through the SQL statement will involve some new knowledge points later, you can read the following chapters first, and then come back to learn the division operation of SQL)

R(X,Y)

S(Y,Z)

select distinct R.X from R R1
where not exists
( 
    select S.Y from S
    where not exists
    ( 
        select * from R R2
        where R2.X=R1.X and R2.Y=S.Y 
    ) 
);

postscript

Destroy the created table

drop table homes;

drop table students;

Note: The purpose of destroying the created table here is to make the following chapters independent of the previous chapters, so that the friends can not find the table in the example.

Guess you like

Origin blog.csdn.net/m0_61139217/article/details/128132554