Chapter 2 Relational Database Notes

table of Contents 

2.1 Relational model
2.2 Relational data structure and its formal definition
2.3 Relational integrity
2.4 Relational algebra 
2.5 Relational calculation
2.6 Summary

 

 

2.0 relationship

A domain is a set of values ​​of the same type. The domain can be a finite set or an infinite set. If the domain D is a finite set, the number of elements in D is called the cardinality of D. Let it be IDI
. Collections, collections of real numbers, collections of character strings, collections of character strings not exceeding 20 and collections {0,1}, etc. are all fields

Cartesian Product

Given n domains D , D ..... Dn (they do not have to be different from each other). The Cartesian product on is written as: D1x D2x ... xDn, and Dyx D2x ... xDn, = {(d , d2, .... dn,)}

Each element (d, d, ... d,) is called an n-tuple (tuple for short), and di is the ith component of the tuple. 
For example, there are three fields: D1 = {0,1}, D2 = {a, b, c}, D3 = {D, E}, then
the Cartesian product of D, D2, D3 is:
Djx D2x D3 = { (0, a, D), (0, a, E), (0, b, D), (0, b, E), (0, c, D), (0, c, E), (1 , a, D), (1 , a, E), (1, b, D), (1, b, E), (1, c, D), (1, c, E)}
which base is | D1lxI D2 | x | D3 | = 12

 

Another representation of the
relationship is that relationships can use two-dimensional tables to indicate that each table has a unique name (relationship name),

The corresponding component of each column of the table is called an attribute.The first row of the table is the table header, which gives the attribute name of each column, and each remaining row corresponds to a tuple, and each tuple represents an entity or contact relationship. This kind of visual representation allows us to attach some semantics to the relationship.
For example, we can explain the semantics of the relationship tuple by the attribute name, we can define the code of the relationship through the semantics of the attribute , and define the integrity constraints of the relationship

 

 

 Nature of relationship

Relationship is a subset of the Cartesian product, i.e., set of tuples, therefore, the relation order of the rows is not important , and the relations (tables) can not contain two identical tuples (rows) shows the relationship between a two-dimensional table so that we can show the relationship in any column order, or the order of the columns is not important in a relational database, any meaningful relationship must meet certain constraints, these constraints are semantic constraints

 

In addition to semantic constraints, there are two main restrictions on relationships.
First, in relational databases, only finite relationships are considered.
Second, each attribute of the relationship must be atomic

 

Code Supercode Candidate Code

The main code considers employee relations, and its model is as follows:

EMPS (Eno, Ename, Esex, Eage, Eaddress, Dname, Esalary)
Eno is the code of the EMPS relationship, because each employee has a unique number-
any attribute set that contains Eno is a supercode , such as, Ename}, this attribute set can also uniquely determine each employee.
If two employees with the same home address are unlikely to have the same name and surname, then {Ename, Eaddress} is also the code of EMPS

 

Let K be the attribute set of relation R. If K can uniquely identify a tuple and its true subset cannot, then K is the code of R. Generally, when a relationship has multiple codes, one of them should be selected as the only identification relationship The code of the tuple is called the main code. The main code is used to represent the code selected from multiple codes as the unique identification relationship tuple, and all the codes are also called the candidate code. The attribute in the code is called the main attribute, but not in The attributes that appear in any code are called non-primary attributes. For example, Eno and {Ename, Eaddress} are candidate codes for EMPS. You can choose one as the main code of EMPS.For example, choose Eno as the main code.If FK is the attribute set of R and is not the code of R, but FK corresponds to the main code K1 of relationship R1, then FK is called R The outer code. Where R is the reference relationship, R1 is the referenced relationship (R and R1 are not necessarily different relationships), and it is said that FK refers to the main code K1 of R1

 

Entity integrity rules: all tuples of relation R must have unique values ​​on the main code and cannot take null values ​​on any attributes of the main code

 

Referential integrity rules
example 
Students (Sno, Sname, Sex, Birthday, Enrollyear, Speciality, Dno) 
Courses (Cno, Cname, Period, Credit) 
SC (Sno, Cno, Grade) 
There are also references between the three relationships "Sno" in SC must be an existing student in Students, and "Cno" in SC must be an existing course in Courses

 

User-defined integrity rules
User-defined integrity reflects the semantic constraints that the data involved in a particular database must satisfy. Since there are no general rules, these constraints must be specified by the user according to the semantics of the actual problem.For example, in the educational administration database, we can define the following constraints: the student's grade must be an integer between 0 and 100; the student's accumulation No more than 5 failed courses; the value of gender ~ can only be male, female and blank; the names of teachers and students can not be blank, etc.

2.1 Relational model

1. Entity integrity: stipulate that each row of the table is the only entity in the table.
2. Domain integrity: Refers to the column in the table must meet a specific data type constraints, which includes the range of values, precision and other regulations.
3. Referential integrity: refers to the data of the primary key and the foreign key of the two tables should be consistent, to ensure the consistency of the data between the tables, to prevent data loss or meaningless data from spreading in the database.
4. User-defined integrity: Different relational database systems often need some special ...

1. Data structure-relationship of relational model

The data structure of the relational model: Very simple. From the user's perspective, the logical structure of the data in the relational model is a two-dimensional table. But this simple data structure of the relational model can express rich semantics describe each between real-world entities and entity types link .

3. The three types of integrity constraint relationship model of the relationship provides a rich integrity control mechanism, allowing the definition of three types of integrity: entity integrity, referential integrity and user-defined integrity . Among them entity integrity and referential integrity It is the integrity constraint that the relational model must meet, and it should be automatically supported by the relational system. The user-defined integrity is a constraint condition that needs to be followed by the special requirements of the application domain, and reflects the semantic constraints in the specific domain. The following will proceed from the three elements of the data model, and gradually introduce the data structure of the relationship model (including the formal definition of the relationship and related concepts), the three types of relationship integrity constraints, relationship algebra and relationship calculus operations, etc.


2.2 Relational data structure and its formal definition
2.3 Relational integrity
2.4 Relational algebra 
2.5 Relational calculation
2.6 Summary

Guess you like

Origin www.cnblogs.com/SunChuangYu/p/12672520.html