[Database] Introduction to Relational Model + Formal Relational Query Language

Table of contents

Chapter 2 Introduction to the Relational Model

2.1 Structure of relational database

relation

2.2 Database Schema

2.3 yards

2.4 Mode Diagram

Schema diagram of the university database! ! !

University database relational model! ! !

2.5 Relational Query Language

2.6 Relational operations

2.7 Summary

Chapter 6 Formal Relational Query Language

1. Relational Algebra relational algebra

a. Select Operation

b. Projection (Project Operation)

combination

c. Union Operation

d. Set Difference Operation

e. Cartesian-Product Operation

 f. Rename Operation

2. Tuple Relational Calculus

3. Domain Relational Calculus


Chapter 2 Introduction to the Relational Model

2.1 Structure of relational database

relation

  • Attributes : the data for each column in the table.A1, A2, …, An
  • Tuples: data for each row in the table
  • relation : relation is unordered
    • relation instance: table
    • Relation schema:  R = (A1, A2, …, An ). For example: instructor = (ID, name, dept_name, salary)

2.2 Database Schema

2.3 yards

  • superkey : An attribute or set of attributes that allows us to uniquely identify a tuple within a relation.

                For example  {ID, name},{ID}

  • Candidate key (candidate key): the smallest (including the least number of attributes) superkey. For example {ID}

  • Primary key : one of the candidate keys is selected as the primary key, and there can only be one primary key for any relationship

    • The choice of master code must be careful. As we have noticed, the person's name is obviously not enough to be the primary code, because there may be many people with the same name.

    • The primary key should select attributes whose values ​​never or rarely change

  • Foreign key (foreign key): All values ​​of a column in one table must appear in a column of another table, and are the main

2.4 Mode Diagram

Schema diagram of the university database! ! !

        A database schema containing primary key and external key dependencies can be represented by a schema diagram. The following is the schema diagram of the university database:

University database relational model! ! !

department(dept_name,building,budget);
instructor(ID, name,dept_name,salary);
course(course_id,title,dept_name,credits);
section(course_id,sec_id,semester,year,building,room_number,time_slot_id);
teaches(ID,course_id,section_id,semester,year);
student(ID,name,dept_name,tot_cred);
prereq(course_id,prereq_id);
Advisor(s_id,i_id)
takes(ID,course_id,sec_id,semester,year,grade)
classroom(building,room_number,capacity)
time_slot(time_slot_id,day,start_time,end_time)

2.5 Relational Query Language

2.6 Relational operations

2.7 Summary

  • A relational data model is based on a collection of tables. Users of the database system can query these tables and can insert new tuples, delete tuples, and update (modify) tuples. There are several languages ​​for expressing these operations.
  • The schema of a relationship refers to its logical design, while the instance of a relationship refers to its content at a particular moment. Database schema and instance definitions are similar. The schema of a relationship includes its attributes, and may also include attribute types and constraints on the relationship, such as primary key and foreign key constraints.
  • The superkey of a relation is a collection of one or more attributes whose values ​​are guaranteed to uniquely identify the tuples in the relation. A candidate key is a minimal superkey, that is, it is a set of attributes that make up a superkey, but any subset of this set of attributes is not a superkey. A candidate key for the relation is chosen as the primary key 
  • The foreign key in the reference relation is such a set of attributes: for each tuple in the reference relation, its value on the foreign key attribute must be equal to the value of a tuple in the referenced relation in the main value on the code. 
  • Schema diagram (schema diagram) is a graphical representation of the schema in the database, which shows the relationship in the database, the attributes of the relationship, the primary key and the external key.
  • A relational query language defines a set of operations that can be performed on tables and output tables as results. These operations can be combined into expressions expressing the desired query. 
  • Relational algebra (relational algebra) provides a set of operations. They take one or more relations as input and return a relation as output. Actual query languages ​​such as SQL are based on relational algebra • but add some useful syntactic features.

Chapter 6 Formal Relational Query Language

1. Relational Algebra relational algebra

        Relational algebra is a procedural query language. It consists of a collection of operations that take one or two relations as input and produce a new relation as a result. The basic operations of relational algebra are: selection, projection, union, set difference, Cartesian product and renaming . In addition to the basic operations, there are some other operations, namely, set intersection, natural join, and assignment . We will define these operations in terms of basic operations.

        The selection, projection, and renaming operations are called unary operations because they operate on one relation, and the other three operations operate on two relations. They are thus called binary operations.

a. Select Operation

       The selection (selelct) operation selects tuples that satisfy a given predicate , denoted by the symbol σ:

b. Projection (Project Operation)

combination

c. Union Operation

     

        1. The relations r and s must be homogeneous, that is, they must have the same number of attributes .

        2. For all i, the domain of the i-th attribute of r must be the same as the domain of the i-th attribute of s .

Note that r and s can be database relations or temporary relations that are the result of relational algebra expressions.

d.  Set Difference Operation

        The set-difference operation, denoted by -, allows us to find those tuples that are in one relation but not in the other. The result of the expression rs is a relation containing all tuples in r that are not in s.

e.  Cartesian -Product Operation

 f.  Rename Operation

To be perfected

2. Tuple Relational Calculus

3. Domain Relational Calculus

Guess you like

Origin blog.csdn.net/m0_63834988/article/details/132596942