DataBaseSystem:Relational Data Structure

In this part from the "Introduction to Database Systems (fifth edition)" (Wang Shan Sa division Xuan), mainly basic concept of sorting the database for their own review inspection.

relational database

Relational database is a database system based on the relational model. Relational model consists of three parts: the relationship between the data structure , the operation set relationship , relational integrity constraints .

Relational data structures

Relational data structure contains only one data structure - the relationship, from the logical structure point of view, the relationship is a two-dimensional table . As mentioned earlier, the relational model is the data model formed based on rigorous mathematical concepts, and the relationship is the basis of set theory . It should be understood that the relationship between a data structure of the formal definition of the angle from the set theory.

Domain (domain)

Domain is a collection of values having the same data type. The common natural number field N, the real domain R. The number of different values of this field is referred to as a domain of the base .

Cartesian product (Cartesian product)

It is a Cartesian product set operations , defined as follows:
\ [\ the aligned the begin {} & For a given set of field D_1, D_2, ..., D_n, mutually different domain is not required, D_1, D_2, ... , D_n Cartesian product of \\ & D_1 \ times D_2 \ times ... \ times D_n = \ {(d_1, d_2, ..., d_n) | d_i \ in D_i, i = 1,2, ... , n \} \ end {aligned } \]

Cartesian product is a result of a computation domain of its base equal to the product base of each domain.

relationship

definition

\ [\ Begin {aligned} & D_1 \ times D_2 \ times ... \ times D_n subset of the domain is called D_1, D_2, ..., the relationship D_n, \\ & denoted as R (D_1, D_2 ,. .., D_n), where R is the relation name, n being the degree of relationship (mesh). \ End {aligned} \]

Tuple (tuple)

Each element in the relationship is the relational tuple, generally indicated by t. When n = 1 means the relationship, n = 2 for the binary relations.

Code candidate (candidate key)

If the value of a property of a set of relationships can be uniquely identifies a neuron progenitor, and the subset of the set may identify a no, then the candidate code is called shuffling.

Master Properties prime attribute

All individual attribute codes is called the primary candidate attributes, referred to as non-primary attributes or remaining attribute codes.

The master key (primary key)

If there is a relationship between a plurality of candidate code (such as the relationship between student and student number ID number can be used as a candidate code) is selected as a primary key.

Relationship

Three ways:

  • The basic relationship, that is the most basic form, the data is actually stored.
  • Look-up table, query results corresponding table.
  • View table is a virtual table, derived from the basic table tables or other views.

The basic nature of the relationship

  • Column homogeneous (homogeneous) , i.e., component column is the same type of data.
  • Different columns from the same domain. Such as family relations, all family members from the "people" in this field.
  • Exchangeable order of the columns, so that each time a new attribute can be added directly into to the last one.
  • Two yuan candidate code sets can not take any of the values ​​are identical.
  • Order of the rows is also exchangeable.
  • Component value must take atoms , which does not allow nested table. This is the most basic norms conditions.

Relational model (relational schema)

Called relationship model described relationship is generally expressed as R (U, D, DOM, F). R is the relation name, U is the set of attribute name of the relation, D is the set of all the domain attributes, DOM is a set of attributes to the image domain (Mapping), F is the dependencies among attribute data set.

Relational Operations

Relational model only interested in the relationship between the ability to operate, for a description of how not to make a specific limit, that is, different relational database systems can be defined to develop different language to operate relationship.

Types of

Common operations primarily relational model to query and modify. Changes include insertion (INSERT) , delete (delete) and modification (Update) . The ability to query a strong relationship, the relationship is the most important part of the operation, including the selection (SELECT) , a projection (Project) , the connection (the Join) , except (Divide) , and (Union) , the difference (the except) , cross (intersection) , Cartesian product and so on. Wherein the basic operation is selected, the projection, and the difference, Cartesian product, other operations can be derived from the basic operation.

Feature

The relationship between the object and the result is a collection operation.

Relational integrity

Three relational model and integrity constraints: entity integrity (Integrity Entity) , referential integrity (referential Integrity) , user-defined integrity (User-defined Integrity) . The first two relations are automatically supported, which is bound specific applications.

Entity integrity

If the property is a primary attribute A basic relation R, then A can not take a null value (null value) .

Referential integrity

Before defining referential integrity, we must first understand the outer code concept.

Outer code (foreign key)

Let F be a fundamental relationship R or a set of attributes, but not the code of R, K S is the basic relationship of the master key. If the F and K corresponding to the outer code is called F R, and R is a basic relationship and said reference relationship, S is the reference relation or target relationship.

A simple example: Suppose the student table in the "professional number" attribute, professional relationship table of the main code that is "professional number," which is the number two professional correspondence, where you can say "professional number" property is a student outer code relations. Where professional relationships are referring to the relationship between student relationship is a reference to the relationship.

definition

When the group F is an attribute or an outer code base relations, the master key K which substantially corresponds to the relationship between S, the R value for each tuple over F must be equal to a null value, or tuples in S main code value.

Back to the example above, it is easy to understand: If the number of professional property relations students take a null value, indicating that the student has not been assigned a professional; if not empty, but not the primary key value tuples professional relationship, that is to say, the student was assigned to a professional non-existent, which is obviously inappropriate.

User-defined integrity

User-defined data integrity reflects a specific application involved semantics must meet the requirements. Such as: student relationships, student's name can not be empty.

Relational algebra

Relational algebra is an abstract query language, with operations on relations represent the query. Operands and results of operations of relational algebra are relations, it contains two types of operators: the traditional set operations and special relational operators.

The traditional set operations

Traditional set operations are binary operator, and includes, difference, intersection, Cartesian product, as it is the relationship between the set of tuples, i.e. understood from the relationship between the angle of the line.

And the relationship established relation R S have the same mesh number n (i.e., n has attributes), the corresponding attribute from the same domain, t is a tuple variable. Set operations defined as follows:

and

Relation R, and the result S is still n mesh relationship, wherein the tuple or from R, or from S, and the operation denoted as:
\ [R \ bigcup S = \ {T | T \ in R \ bigvee T \ in S \} \]

difference

Relationship R, poor results of S is n mesh relationship in which the tuple was from R but not S, the differential operation referred to as:
\ [the RS = \ {T | T \ in R \ bigwedge T \ notin S \} \]

cross

Results post relationship R, S also n mesh relationship in which the tuple belongs R and S, intersection operation referred to as:
\ [R \ bigcap S = \ {T | T \ in R \ bigwedge T \ in S \ } \]
deposit operation and a differential operation can be converted into each other:
\ [R & lt \ bigcap the R- S = (the RS) \]

Cartesian product (generalized)

Relationship set R, S are the result of n, m mesh relationship, their Cartesian product is a set of (m + n) column of tuples. Column of the first n-tuple is a tuple of the relation R, the m column is a tuple of the relation S. If the relation R, the number of tuples S respectively r, s, the result of their Cartesian product are tuples r * s. Cartesian product operation referred to as:
\ [R & lt \ Times S = \ {\ overbrace t_rt_s {} | T_R \ in R & lt \ bigwedge T_S \ in S \} \]

Special relationship operations

Operation comprises selecting a special relationship, a projection, is connected, the division operation and the like.

mark

Components

\ [\ Begin {aligned} & relational schema is set R (A_1, A_2, ..., A_n), it is set a relation R. \\ & t \ in R represents t is a tuple of R. t [A_i] t represents the tuple corresponding to A_i of a component attribute. \ End {aligned} \]

Complement

\ [\ Begin {aligned} & If A = \ {A_ {i1}, A_ {i2}, ..., A_ {ik} \}, wherein, A_ {i1}, A_ {i2}, ..., A_ {ik} is A_1, A_2, ..., A_n part of, the & \\ a group called attributes. t [A] = (t [A_ {i1}], t [A_ {i2}], ..., t [A_ {ik}]) represents a set of individual components of the tuple t in the property set A, \\ & \ overline {a} represents \ {A_1, A_2, ..., A_n \} removed in \ {A_ {i1}, A_ {i2}, ..., A_ {ik} \} remaining after the attribute group. \ End {aligned} \]

Connection tuple

\ [\ Begin {aligned} & provided R, S respectively, n, m mesh relationship, t_r \ in R, t_s \ in S, \ overbrace {t_rt_s} called connection tuple. It \\ & tuple is a m + n columns, a first n components is n-tuple of R, the m components is an m-tuple of S. \ End {aligned} \]

Image Set (images set)

Given a relation R (X, Z), X , Z is a group property. When t [X] = x, x as set defined in R is:
\ [z_x = \ {T [the Z] | T \ in R, T [X-] = X \} \]
which indicates the attribute tuple corresponding to the value of X x, the value of the set of attributes corresponding Z.

select

Tuple selection operation calculates from the perspective of row select eligible from the relation R (a given logical expression is true), and referred to as:
\ [\ sigma_F (R) = \ {T | T \ in R \ bigwedge F (t) = true \ }, where F (t) is the logical expression \]
Example:
\ [\ sigma_ Sdept = { 'the iS'} (Student) \]
where, Sdept attribute represents professional, iS information professional , student student relationship is the corresponding table, the formula gives all students choose professional information professional.

projection

It calculates the angle of projection from the column. A projection on a relation R number of selected attributes from the group of R form a new relationship, referred to as:
\ [\ Prod {} _ A (R) = \ {T [A] | T \ in R \} \]
Example:
\ [\ prod {} _ {Sname
, Sdept} (student) \] where, Sname is the student's name, the formula gives students the names and professional queries are located.

It should be noted that, although the projection operation is for the column, but it is possible, after the elimination of certain property group, and the remaining tuples duplicate content may appear, then you need to repeat the group eliminated altogether. Such as: When students only query the table, it is clear where the students have a lot of repetition, then each professional can only hold a tuple.

connection

Connection is selected from the Cartesian product of two relations between attributes tuples satisfying certain conditions, referred to as:
\ [R & lt \ bowtie_ {A \ B} Theta S = \ {\ overbrace t_rt_s {} | T_R \ in R & lt \ bigwedge t_s \ in S \ bigwedge t_r
[a] \ theta t_s [B] \} \] where, a, B are the R, S the same as the number of the above group and comparable properties, θ is a comparison operator. The meaning of the formula: R value selected relationship between the value on the relationship between S A set of attributes satisfy attribute in the group B from the Cartesian product of R and S S R * comparative relationship of θ tuples.

Equijoins

When θ = referred to the equivalent connection.

Natural connection

NATURAL equivalent connection is a special connection, while it calculates the angles of rows and columns, the requirements for component comparing two relations must be the same name attribute group, and the repetition of the column is removed in results.

In addition to operations

T relationship is provided by dividing the result of the relationship R S relation, the T but does not include all attributes and their values R S in, and all combinations of tuples T to S are tuples in R. That is:
\ [R & lt \ div S = T, S \ Times T \ subseteq R & lt \]
relations division can as set definition: given relation R & lt (X, Y) and S (Y, Z), wherein X, Y , Z is a group property. R Y and S in the Y attribute names may be different, but must be from the same domain. In this case, in addition to the calculation result R and S X contains only one attribute, a result of the arithmetic relationship between P (X), P is R satisfies the following conditions tuple attribute group projected on the X: X component in the tuple the value of x as set Y_x contained S in the Y-projection set, denoted as:
\ [R & lt \ div S = \ {T_R [x-] | T_R \ in R & lt \ bigwedge \ Prod {} _ Y (S) \ subseteq Y_x \ } \]
division operation also calculates from the perspective of rows and columns.

Relational calculus

Relational calculus is the predicate calculus mathematical logic based. Press predicate variable relational calculus can be divided into tuple relational calculus and the domain relational calculus. The main understand tuple relational calculus.

Tuple relational calculus language ALPHA

Tuple relational calculus tuple - variable as the basic object predicate argument, ALPHA is typical of the tuple relational calculus language, there are GET, PUT, HOLD, UPDATE, DELETE, DROP , six operations statement. The basic format of the statement is:
\ [operating statement \ space \ space workspace name (expression): operating conditions \]

Retrieval operations

General inquiries can be achieved using GET; you can also combine RANGE statement to explain tuple variables, simplifying the relationship name. Meanwhile, when the operating conditions employed quantifier must use variables described ancestral; contains expression may also be used to retrieve information.

  1. Query data for all students.
    \ [GET \ space W (Student ) \]

  2. Student information query system younger than 20-year-old student number and age.
    \ [GET \ space W (Student.Sno , Student.Sage): Student.Sdept = 'IS' \ bigwedge Student.Sage <20 \]

  3. Query Information Department of the oldest of three students in school number and age by age results in descending order.
    \ [GET \ space W (3 ) (Student.Sno, Student.Sage): Student.Sdept = 'IS' \ space DOWN \ space Student.Sage \]

  4. Query No. 2 elective courses students name.
    \ [\ Begin {aligned} & RANGE \ space SC \ space X \\ & GET \ space W (Student.Sname): \ exists X (X.Sno = Student.Sno \ bigwedge X.Cno = '2') \ end { aligned} \]

  5. Queries do not choose the No. 1 course students name.
    \ [\ Begin {aligned} & RANGE \ space SC \ space SCX \\ & GET \ space W (Student.Sname): \ forall SCX (SCX.Sno \ ne Student.Sno \ bigvee SCX.Cno \ ne '1') \ end {aligned} \]

  6. Queries least enrolled student number of student number is 201 215 122 students chosen course.
    \ [\ Begin {aligned} RANGE \ space & Course \ space CX \\ & SC \ space SCX \\ & SC \ space SCY \\ GET \ space W & (Student.Sno): \ forall CX (\ exists SCX (SCX.Sno = '201215122' \ bigwedge SCX.Cno = CX.Cno ) \\ & \ Rightarrow \ exists SCY (SCY.Sno = Student.Sno \ bigwedge SCY.Cno = CX.Cno)) \ end {aligned} \]

Update

Modification operations

UPDATE modification operations implemented by:

  1. To be modified with HOLD tuple extracted from the database statements to the workspace;
  2. Modify the attribute values ​​of tuples in the workspace with host language;
  3. UPDATE statement with the modified tuple back to the database.

Which, HOLD is GET statement with concurrency control. And in the ALPHA language, not allowed to modify the relationship between the main code, if necessary, only delete and add.

Example: student number is 201 215 127 students to information from the Department of Computer Science.
\ [\ Begin {aligned} & HOLD \ space W (Student.Sno, Student.Sdept): Student.Sno = '201215127' \\ & MOVE \ space 'IS' \ space TO \ space W.Sdept \\ & UPDATE \ space W \ end {aligned} \]

Insert

PUT achieved by insertion:

  1. Establish new tuple in the workspace with the host language;
  2. With a PUT statement to the tuple into a designated relationship.

Example: The school has opened a new course "Computer Organization and Architecture" course 8, 2 credits, direct antecedence course is No. 6.
\ [\ Begin {aligned} & MOVE \ space '8' \ space TO \ space W.Cno \\ & MOVE \ space ' Computer Organization' \ space TO \ space W.Cname \\ & MOVE \ space '6' \ space TO \ space W.Cpno \\ & MOVE \ space '2' \ space TO \ space W.Ccredit \\ & PUT \ space W (Course) \ end {aligned} \]

Deletion

Deletion with DELETE achieve:

  1. Tuples to be deleted from the database to read with HOLD statement to the workspace;
  2. Delete the tuple with a DELETE statement.

Example: 201 215 230 students drop out for any reason.
\ [\ Begin {aligned} & HOLD \ space W (Student): Student.Sno = '201215230' \\ & DELETE \ space W \ end {aligned} \]

Guess you like

Origin www.cnblogs.com/Li-F/p/11854500.html