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)∣di∈Di,i=1,2,…,n}
Name translation:
-
(d1, d2, d3,…,dn) is called an n-tuple (referred to as a tuple)
-
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
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:
- 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
- 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.
- Primary code: There are multiple candidate codes for a relationship, and one of them must be selected as the primary code.
- Main attributes: The attributes of the candidate code are called main attributes
- Non-primary attributes: attributes that are not included in any candidate code are called non-primary attributes (or non-code attributes)
- 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
-
Basic relationship (basic table/base table)
It is an actual table and a logical display of the actual stored data.
-
lookup table
Is the table corresponding to the query result
-
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
- Columns are homogeneous (the components in each column are the same type of data and come from the same domain)
- Different columns can come from the same domain, and each column is called an attribute. Different attributes should be given different attribute names.
- For exampleExample 1, we can also divide it into two domains,
person{张清玫、刘逸、李勇、刘晨、王敏}
andspecialty{计算机专业、信息专业}
. The person domain is divided into two attributes,研究生和导师
.
- For exampleExample 1, we can also divide it into two domains,
- The order of the columns does not matter, i.e. the order of the columns can be interchanged at will
- The order of the rows does not matter, i.e. the order of the rows can be interchanged at will
- The candidate codes of any two tuples cannot have the same value.
- ⭐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
- SELECT select
- PROJECT projection
- JOIN connection
- DIVIDE except
- UNION and
- EXCEPT difference
- INTERSECTION
- Cartesian Product
relational language
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).
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:
- Null value (each attribute value in F is null)
- 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\} R∪S={ t∣t∈R∪t∈S}
Difference -
R − S = { t ∣ t ∈ R ∩ t ∉ S } R - S = \{t|t∈R∩t∉S\} R−S={ t∣t∈R∩t∈/S}
cross ∩
R ∩ S = { t ∣ t ∈ R ∩ t ∈ S } R∩S=\{t|t∈R∩t∈S\} R∩S={ t∣t∈R∩t∈S}
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={ trts∣tr∈R∩ts∈S}