Chapter 1 Introduction
A database management system is composed of a collection of interrelated data and a set of programs used to access this data. This collection of data is often called a database
The collection of information stored in the database at a specific moment is called an instance of the database , and the overall design of the database is called the database schema
The basis of the database structure is the data model . The data model is a collection of conceptual tools that describe data, data relationships, data semantics, and consistency constraints. The data model provides a way to design the physical layer, logical layer and view layer database.
Data values stored in a database must satisfy certain consistency constraints
Entities in the database are described through attribute collections, such as name and salary, which can describe the instructor entity.
A link is an association between several entities. For example, a member link links a teacher to her department. The set of all entities of the same type is called an entity set, and the set of all relationships of the same type is called a relationship set.
Part One--Relational Database
Chapter 2--Introduction to the relational model
A tuple is just a sequence (or list) of values. A relationship between n values can be mathematically represented by an n-tuple of these values. In other words, an n-tuple is a n-tuple of n values. A tuple of , which corresponds to a row in the table
In relational model terminology, relationships are used to refer to representatives, tuples are used to refer to rows, and attributes are used to refer to columns in a table.
For each attribute of a relationship, there is a set of allowed values, called the domain of the attribute.
Domains are atomic and cannot be subdivided
A null value is a special value that means the value is unknown or does not exist
Database schema
A database schema is the logical design of a database, and a database instance is a snapshot of the data in the database at a given moment.
code
The attribute values of a tuple must be able to uniquely distinguish the tuples. In other words, no two tuples in a relationship have the same values for all attributes.
A superkey is a set of one or more attributes. The combination of these attributes can uniquely identify a tuple in a relationship.
The main key is mainly used to distinguish the candidate keys of different tuples in a relationship.
Foreign code :
Pattern diagram
A database schema that contains main code and foreign code dependencies can use a schema diagram
relational query language
A query language is the language that users use to request information from a database
In a procedural language , the user directs the system to perform a series of operations on the database to calculate the desired result.
In a non-procedural language , users only need to describe the required information without giving a specific process to obtain the information.
relational operations
Chapter 3--SQL
SQL data definition
Basic properties
Basic schema definition
create table
create table r
( A1,D1,
A2,D2,
....,
The, Dn,
<Integrity Constraint 1>,
...,
<Integrity constraint k> );
Integrity constraints:
not null: The not null constraint on an attribute indicates that null values are not allowed on the attribute. This constraint excludes null values from the attribute domain.
SQl prohibits any database updates that violate integrity constraints
insert
insert into instructor
values (10211,'Smith' , 'Biology' , 66000);
delete delete tuple
delete from student;
drop remove the relationship
drop table r;
This command deletes all information about the removed relationship from the database. delete retains the relationship r but deletes all tuples in r. drop not only deletes all tuples of r, but also deletes the pattern of r. Once r is removed, no tuples can be inserted into r unless r is rebuilt using the create table command.
alter table add attributes
alter table r add A,D;
The values of all tuples in the relationship on the new attribute are set to null, r is the name of the existing relationship, A is the name of the attribute to be added, and D is the domain of the attribute to be added.
alter table r drop A;
Remove an attribute from a relation, where r is the name of the existing relation and A is the name of an attribute of the relation
Basic structure of SQL query
The basic structure of SQL query consists of three clauses: select, from, where
Single relation query
To forcefully delete duplicates, you can add the keyword distinct after select .
select distinct dept_name
from instructor;
SQL allows us to use the keyword all to explicitly indicate not to remove duplicates
The select clause can also contain arithmetic expressions containing +, -, *, / operators, and the operands can be constants or attributes of tuples.
where:
Multiple relationship query
A Cartesian product of the relations listed in the from clause is defined. It can be defined formally using set theory, but is best understood through the following iterative process :
This resulting relation has all attributes from all relations in the form clause
natural connection
The natural selection operation operates on two relations and produces a relation as a result. Natural join only considers pairs of tuples with the same value for attributes that appear in both relational patterns.
Additional basic operations
rename operation
as clause
old-name as new-name
The as clause can appear in either the select clause or the from clause.
String operations
escape character
SQL allows you to define escape characters. Use the escape keyword in the like comparison operation to define escape characters.
Attribute description in select clause
The asterisk * can be used in the select clause to mark "all attributes"
Arrange the display order of tuples
The order by clause allows the tuples in the query results to be displayed in sorted order.
desc means descending order asc means ascending order
where clause predicate
The between comparison operator indicates that a value is between two, closed on the left and closed on the right.
Set operations
and operation
Union operation automatically removes duplicates union all retains all duplicates
intersection operation
intersect operation automatically removes duplicates interscet all retains all duplicates
Difference operation
To find all courses that started in the fall semester of 2009 but not in the spring semester of 2010, you can write:
The except operation outputs from its first input all tuples that do not appear in the second input
null value
If any input of an arithmetic expression is empty, the result of the arithmetic expression (involving such as + - * /) is empty
aggregate function
An aggregate function is a function that takes a set (set or multiset) of values as input and returns a single value. SQL provides five inherent aggregate functions:
- Average: avg
- Minimum value: min
- Maximum value: max
- Sum: sum
- Count: count
The inputs to sum and avg must be sets of numbers, but other operators can also operate on sets of non-numeric data types, such as strings.
Basic gathering
Find the average salary of Computer Science faculty and give the attribute of the resulting relationship a name:
group aggregation
We not only want the aggregate function to operate on a single set of tuples, but also on a set of tuples
One or more attributes given in the group by clause are used to construct the group
Tuples with the same value on all attributes in the group by clause will be grouped together