Relational Databases - Relational Operations and Relational Integrity

1. Relational operations

1. Basic relational operations

Commonly used relational operations :
Query (Query) operation and Insert (Insert), Delete (Delete), and Modify (Update) operations.

Relational queries are very expressive and are the most important part of relational operations.

Query operations can be divided into:
Select, Project, Join, Divide, Union, Except, Intersection, Cartesian product, etc.
Among them , selection, projection, union, difference, and Cartesian product are five basic operations.
Other operations can be defined and derived using primitive operations. Just as multiplication can be defined and derived in terms of addition.

The characteristic of relational operation is the set operation mode, that is, the objects and results of the operation are both sets. This method of operation is also called a set-at-a-time method. Correspondingly, the data operation method of the non-relational data model is a record-at-a-time method.

2. Classification of relational data languages

relational algebra language

Use operations on relations to express queries
Representation : ISBL

Relational Calculus Language

Use predicates to express query requirements.

  1. Tuple relational calculus language: the basic object of the predicate variable is a tuple variable
    representative : APLHA, QUEL
  2. Domain relational calculus language: the basic object of the predicate variable is the domain variable
    representative : QBE

A language with dual features of relational algebra and relational calculus

代表:SQL (Structured Query Language)


The integrity of the relationship

There are three types of integrity in relational models: entity integrity, referential integrity, and user-defined integrity.
Among them, entity integrity and referential integrity are the integrity constraints that must be satisfied in the relational model, and are called the two invariants of the relation .

Explanation of terms :

  1. Candidate code : attribute group (a single attribute can also be a group), the value of this attribute group can uniquely identify a tuple, but its subset cannot.
  2. Main attribute : the attributes in the candidate key.
  3. Non-code attributes : attributes that are not in any candidate codes.
  4. Primary code : If there are multiple candidate codes, select one of them as the primary code (that is, the primary code is one of the candidate codes).
  5. Outer key : an attribute or a set of attributes. It is not the key of this relationship, but is the primary key of another relationship. This relationship is a referencing relationship, and the other relationship is a referenced relationship.

1. Entity Integrity

Entity Integrity Rules

If attribute A is the main attribute of the basic relation R , then A cannot take a null value
. If the main key is composed of several attributes, all these main attributes cannot take a null value.

Null value :
"don't know", "does not exist", "meaningless"... not 0

Description :

  1. The rules for entity integrity are for the underlying relationships . A base table usually corresponds to an entity set of the display world
  2. Entities in the real world are distinguishable, i.e. they have some sort of unique identity
  3. Correspondingly, the primary key is used as the unique identifier in the relational model
  4. The attribute in the primary code, that is, the primary attribute cannot take a null value

2. Referential Integrity

referential integrity rules

If an attribute or attribute group F is a foreign 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 on F The value of must be:
(1) Null value, each attribute value of F is a null value.
(2) The primary key value (primary key value) in a tuple in S.
That is, the attribute value in the referenced relationship must be found in the referenced relationship or take a null value , otherwise it does not conform to the semantics of the database.
In the actual operation, such as updating, deleting, and inserting data in a table, check whether the data operation on the table is correct by referring to the data in another table that is related to each other, and reject the operation if it is incorrect.

insert image description here
Reference relationship—→referenced relationship (target relationship)

Obviously, the primary key Ks of the target relation S and the foreign key F of the reference relation R must be defined on the same (or the same group) field.

Reference relationship

Same relation
In referential integrity rules, R and S can be the same relation.
In the student (student number, name, gender, age, major number, class leader) relationship, the "student number" attribute is the primary key, and the "student number" attribute represents the student number of the class leader of the student's class, so it applies to this relationship" student number" attribute. According to the rules of referential integrity, the attribute of "Squad Leader" can take the following two types of values:
(1) Null value, indicating that the student's class has not yet elected a monitor.
(2) A non-null value, which must be the student ID value of a tuple in this relationship, that is, the student ID value of a student who is elected monitor.

Two relationships
such as student entity and professional entity can be represented by the following relationship schema, where the student number is the primary key of the student, and the professional number is the primary key of the major:
student (student number, name, gender, major number, age)
major (professional
There is an attribute reference between the two relationships (contains the same attribute "professional number"), the student relationship references the primary key "professional number" of the professional relationship, and the major number is the foreign key of the student relationship . And according to the rules of referential integrity, the "professional number" attribute of each tuple in the student relationship (not the professional relationship) can only take two values:
(1) Null value, indicating that the major has not been assigned to the student.
(2) A non-null value. At this time, the value must be the "professional number" value of a tuple in the professional relationship, indicating that the student cannot be assigned to a non-existing major. That is to say, the value of an attribute in the student relationship needs to refer to the value of the attribute in the professional relationship.

More than two relationships More
than two relationships can also have a reference relationship. The connection between students, courses, and courses (students and courses) can be expressed by the following three relationships:
students (student number, name, gender, major number , age)
major (professional number, major name)
course selection (student number, course number, grade)
here, the "student number" and "course number" attributes are foreign keys for the course selection relationship, but only "student number" and "course number" Number" can be combined to determine the relationship of course selection, so there is a composite primary key relationship here, (student number, course number) is the primary key of the course selection relationship. Although two types of values ​​can be taken according to referential integrity, due to entity integrity, each attribute in a composite primary key cannot take a null value. Therefore, the "student number" and "course number" attributes in the course selection relationship can only take the existing primary key values ​​​​in the referenced relationship (student relationship and professional relationship). That is, the "student number" value in the course selection relationship must be the student number of a student that actually exists, and there is a record of the student in the student relationship; the "course number" value in the course selection relationship must also be the course number of the course that exists, that is There is a record for the course in the course relationship.

3. User-defined integrity (User-defined Integrity)

User-defined integrity refers to the constraints on a specific relational database , which reflects the semantic requirements that the data involved in a specific application must meet .

A user-defined integrity that must take a unique value for an attribute and cannot take a null value for a non-primary attribute
can cover integrity types such as entity integrity, domain integrity, and referential integrity.

For example, a certain attribute must take a unique value, a non-primary attribute cannot take a null value, and a certain attribute has a value range between 0-100, etc.

Guess you like

Origin blog.csdn.net/z135733/article/details/128797839