Relational database (1) --- relational data model and relationship


foreword

This article mainly explains the relevant knowledge of relational database, involving relational data model and relational knowledge


1. Data model

1. What is a data model

A data model is an integrated set of concepts forDescribe and manipulate data within an organization, the relationships between data, and constraints on data.

The data model is the core and foundation of the database system . The DBMS software implemented on various machines is based on a certain data model

2. The basic elements of the data model

The data structure is the description of the static characteristics of the system, and the data operation is the description of the dynamic characteristics of the system.

(1) Data structure

The data structure is:
1. Objects related to data type, content, and nature, such as data items and records in the network model, domains, attributes, relationships, etc. in the relational model

2. Objects related to the relationship between data, such as the set type in the network model

(2) Data manipulation

Data manipulation isRefers to the collection of operations allowed to be performed on instances of various objects in the database, including operations and related operation rules

There are two main types of operations in the database : retrieval and update .

(3) Data constraints

The constraints on the data areA collection of integrity rules

The data model should reflect and specify the basic general integrity constraints that the data model must comply with . In addition, the data model should also provide a mechanism for defining integrity constraints to reflect the specific semantic constraints that the data involved in a specific application must abide by

2. Relational data model

1. Basic concepts of relational data model

(1) Relationship instance

A relational instance is a table of named columns and rows.

It is important to emphasize that:
In general, relationships refer to instances.

(2) Relationship model

A relational schema is a description of relationships. The relationship schema can usually be abbreviated as:
R ( U ) or R ( A 1 , A 2 , … , A n ) R (U) or R (A1, A2, …, An)R ( U ) or R ( A 1 , A 2 ,An

A relational schema can be formalized as:
R( U , D , dom , F ) R(U, D, dom, F)RUDdomF

R     关系名
U     组成该关系的属性名集合
D     属性组U中属性所来自的域
dom   属性向域的映象集合,常常直接说明为属性的类型、长度
F     属性间的数据依赖关系集合

(3) Relational database

In a given application field, the collection of all entities and the relationships between entities constitutes a relational database.

Because a relation is composed of two parts, a relational database is also composed of two parts, namely, a collection of relational schemas and a collection of corresponding relational instances .

A collection of relational schemas is called a database schema, and a collection of corresponding relational instances is called a database instance.

2. Data structure of relational data model

(1) tuple

A row in a table represents an entity , and relations are composed of tuples.

(2) attributes

Each column in the table is called an attribute in the relationship , each attribute has an attribute name, and the attribute value is the value of each tuple attribute.

(3) domain

The range of values ​​for an attribute is called a domain. The same attribute can only take values ​​in the same domain.

(4) Weight

An attribute value in the tuple .

Example 1:

insert image description here

(5) keys

Energy in relationshipAn attribute or combination of attributes that uniquely distinguishes different tuples, is called a key of the relationship, or is called a keyword or code. The attribute value of the keyword cannot take "null value".

(6) Candidate key

The attributes or combination of attributes in a relation that can be a key may not be unique.Any attribute or attribute combination that can uniquely distinguish and determine different tuples in the relationship is called a candidate key.

in:

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

(7) Primary key

When there are multiple candidate keys in a relationship, select one of them as the primary key of the relationship

A primary key is unique within a relationship. There is one and only one primary key in each relation.

(8) foreign key

An attribute or attribute combination in a relationship is mergedis not a key of this relation, but is a primary key of another relation, this attribute or combination of attributes is called the foreign key of this relationship .

Example 2

insert image description here

3. Data manipulation of relational data model

There are two types of relational operations commonly used in relational models:

1.检索(查询)
2.更新(插入、删除和修改)

Users can complete various operations on data through relational language.It can be understood as a library function by speaking a relational language, or a command line instruction

4. Data constraints of the relational data model

Divided into three categories:

1.数据模型中固有的约束
2.可以在数据模型的模式中直接表述的约束 
3.不能在数据模型的模式中直接表述的约束 

5. Advantages and disadvantages of relational data model

(1) Advantages

1. The relational model is different from the non-relational model in that it is based on strict mathematical concepts.

2. The data structure is simple and clear.

3. Higher data independence, better security and confidentiality.

4. Rich integrity.

(2) Disadvantages

1. Weak ability to express "real world" entities.

2. Since the access path is transparent to the user (that is, the user cannot directly obtain the access path), the query efficiency is often not as good as that of the non-relational data model.

3. The relational model has only some fixed set of operations.

4. Business rules cannot be well supported.

3. Relationship

1. Domains, Cartesian products and relations

(1) domain

domain is a set ofvalues ​​of the same data typecollection.

For example:

1. 自然数、整数、实数
2. 长度小于25字节的字符串
3. 指定长度的字符串的集合
4. {‘男’,‘女’}

(2) Cartesian product

Cardinality

If D i ( i = 1 , 2 , … , n ) is a finite set whose base is mi , i = 1 , 2 , … , n ), then the base M of D 1 × D 2 × … × D n is: If D_i (i=1,2,…,n) is a finite set, and its base is m_i,i=1,2,…,n), then the base M of D_1\times D_2\times …\times D_n is:If Di(i1,2,,n ) is a finite set whose base is mi,i1,2,,n),Then D1×D2××DnThe base M of is:
M = ∏ i = 1 nmi M=\prod_{i=1}^{n}m_iM=i=1nmi

Cartesian product

Given a set of domains D 1 , D 2 , ... , D n , some of the domains are allowed to be the same. Given a set of domains D_1, D_2, ..., D_n, some of them are allowed to be the same.Given a set of domains D1D2Dn, allowing some of these fields to be the same.

Then: the Cartesian product of D 1 , D 2 , … , D n is D_1, D_2, …, the Cartesian product of D_n isD1D2Dn的笛卡尔积为
D 1 × D 2 × … × D n = { ( d 1 , d 2 , … , d n ) ∣ d i ∈ D i , i = 1 , 2 , … , n } D_1\times D_2\times …\times D_n =\{(d_1,d_2,…,d_n)|d_i\in D_i,i=1,2,…,n\} D1×D2××Dn{(d1,d2,,dn)diDi,i1,2,,n }
where each element( d 1 , d 2 , … , dn ) (d_1,d_2,…,d_n )(d1,d2,,dn) is called ann-tuple, each valuedi d_idicalled a component .

The Cartesian product is a set of all values ​​​​of all domains, and the components cannot be repeated

The Cartesian product can be represented as a two-dimensional table, each row in the table corresponds to a tuple, and each column in the table corresponds to a field

example

For example, three domains are given:
D 1 = tutor set SUPERVISOR = {Zhang Qingmei, Liu Yi} D 2 = professional set SPECIALITY = {computer major, information major} D 3 = graduate student set POSTGRADUATE = {Li Yong, Liu Chen, Wang Min} D_1=supervisor set SUPERVISOR=\{Zhang Qingmei, Liu Yi\}\\ D_2=professional set SPECIALITY=\{computer major, information major\}\\ D_3=postgraduate set POSTGRADUATE=\{Li Yong, Liu Chen, Wang Sensitive\}D1=Mentor Collection S U PER V I SOR={ Zhang Qingmei, Liu Yi }D2=Professional collection SPEC I A L I T Y={ Computer major, information major }D3=Graduate Collection POSTGR A D U A TE={ Li Yong, Liu Chen, Wang Min }
D 1 , D 2 , D 3 D_1,D_2,D_3D1,D2,D3ofThe Cartesian product is:
D = D 1 × D 2 × D 3 = { (Zhang Qingmei, major in computer, Li Yong) , (Zhang Qingmei, major in computer, Liu Chen) , (Zhang Qingmei, major in computer, Wang Min) , (Zhang Qingmei, major in information, Li Yong) , (Zhang Qingmei, information major, Liu Chen) , (Zhang Qingmei, information major, Wang Min) , (Liu Yi, computer major, Li Yong) , (Liu Yi, computer major, Liu Chen) , (Liu Yi, computer Major, Wang Min) , ( ( Liu Yi, information major, Li Yong) , ( ( Liu Yi, information major, Liu Chen) , ( ( Liu Yi, information major, Wang Min) } D=D_1\times D_2\times D_3=\\ \{ (Zhang Qingmei, major in computer, Li Yong),\\(Zhang Qingmei, major in computer, Liu Chen),\\ (Zhang Qingmei, major in computer, Wang Min),\\(Zhang Qingmei, major in information, Li Yong ),\\ (Zhang Qingmei, information major, Liu Chen),\\(Zhang Qingmei, information major, Wang Min),\\ (Liu Yi, computer major, Li Yong),\\(Liu Yi, computer major, Liu Chen ),\\ (Liu Yi, computer major, Wang Min),\\((Liu Yi, information major, Li Yong),\\( (Liu Yi, information major, Liu Chen),\\((Liu Yi, Information major, Wang Min) \}D=D1×D2×D3{( Zhang Qingmei, computer major, Li Yong ) ,( Zhang Qingmei, major in computer science, Liu Chen ) ,( Zhang Qingmei, major in computer science, Wang Min ) ,( Zhang Qingmei, information major, Li Yong ) ,( Zhang Qingmei, information major, Liu Chen ) ,( Zhang Qingmei, information major, Wang Min ) ,( Liu Yi, major in computer science, Li Yong ) ,( Liu Yi, major in computer science, Liu Chen ) ,( Liu Yi, major in computer science, Wang Min ) ,(( Liu Yi, information major, Li Yong ) ,(( Liu Yi, information major, Liu Chen ) ,(( Liu Yi, information major, Wang Min )}
Base is
| D 1 | × | D 2 | × | D 3 | = 2 × 2 × 3 = 12 |D_1|\times|D_2|\times|D_3|= 2\times2\times3=12 D1×D2×D3=2×2×312
Converted to a table as:
insert image description here

(3) Relationship

The subset of D 1 × D 2 × … × D n is called the relation D_1×D_2×…×D_n on the domain D 1 , D 2 , … , D n The subset of D_1×D_2×…×D_n is called the domain D_1,D_2,…, Relationships on D_nD1×D2××DnA subset of is called in the domain D1,D2,,DnThe relationship on , expressed as:
R ( D 1 , D 2 , … , D n ) R (D_1,D_2,…,D_n)RD1,D2,,Dn)
R: relationship name
n: purpose or degree of the relationship (Degree)
Each element in the relationship is a tuple in the relationship, usually represented by t.

Unit Relationships and Binary Relationships

When n=1,
the relationship is called a unit relationship (Unary relation), or a one-way relationship

When n=2,
the relationship is called a binary relationship (Binary relation)

representation of relationship

The relationship is also a two-dimensional table, each row of the table corresponds to a tuple, and each column of the table corresponds to a field

2. The nature of the relationship

A relationship usually has the following properties:

1.有一个关系名,并且跟关系模式中所有其他关系不重名
2.每一个单元格都包含且仅包含一个原子值(1NF)
3.每个属性都有一个不同的名字
4.同一属性中的各个值都取自相同的域
5.各个元组互不相同,不存在重复元组
6.属性的顺序并不重要
7.理论上讲,元组的顺序并不重要 

3. The difference between relational schema and relation

Relationship schema:

是型
对关系的描述
静态的、稳定的

relation:

是值
关系模式在某一时刻的状态或内容
动态的、随时间不断变化的

4. Relational database schema

A relational database schema S contains a set of relational schemas S = { R 1 , R 2 , … , R m } S=\{ R_1,R_2,…,R_m\}S={ R1,R2,,Rm} and the set IC of integrity constraints.

The following table shows a relational database schema, denoted as HIS={Dept,Doctor,Patient,Diagnosis}.
insert image description here

5. Relational database related

(1) Type and value of relational database

Types of relational databases: relational database schemas are descriptions of relational databases

The value of a relational database: a collection of relations corresponding to a relational schema at a certain moment, usually called a relational database

(2) Physical organization of relational database

In some relational database management systems, a table corresponds to an operating system file, and the physical data organization is handed over to the operating system to complete

Some relational database management systems apply for several large files from the operating system, divide the file space by themselves, organize storage structures such as tables and indexes, and perform storage management

6. Relational Integrity

(1) empty

1.Represents an attribute value that is not currently known or available for this tuple

2. Empty is a way to deal with incomplete or abnormal data.

3. Empty is not equal to a string composed of zero values ​​or spaces.

(2) Entity integrity rules

The rules are as follows:

1. 实体完整性规则是针对基本关系而言的。一个基本表通常对应现实世界的一个实体集。
2. 现实世界中的实体是可区分的,即它们具有某种唯一性标识。
3. 关系模型中以主码作为唯一性标识。
4. 主码中的属性即主属性不能取空值

(3) References between relationships

In the relational model, entities and the links between entities are described by relations, so there may be references between relations.

For example:
student ( student number , name, gender, major number, age)
course ( course number , course name, credits)
elective (student IDcourse number,score)

(4) foreign key

Let F be an attribute or set of properties of the underlying relation R, but not the key of the relation R. If F corresponds to the primary key Ks of the basic relation S, then F is said to be an outer key of R

The basic relation R is called the referencing relation (Referencing Relation)
The basic relationship S is called the referenced relationship (Referenced Relation) or the target relationship (Target Relation)

(5) Referential integrity rules

If the attribute (or attribute group) F is the outer key of the basic relation R , which corresponds to the primary key Ks of the basic relation S (the basic relation R and S are not necessarily different relations), then for each tuple in R in F The value on must be:
or take a null value (each attribute value of F is a null value)
or equal to the primary key value of a tuple in S.

(6) User-defined integrity

User-defined integrity is a constraint for a specific relational database, reflecting the semantic requirements that the data involved in a specific application must meet.

The relational model should provide mechanisms for defining and verifying such integrity so that they are handled in a uniform and systematic way, rather than leaving this function to the application


Summarize

Please forgive and correct the inappropriateness of the article

Guess you like

Origin blog.csdn.net/weixin_52042488/article/details/126922465
Recommended