Database relational mode statement and sql statement

1. The relationship between the relational mode statement and the sql statement (Question 6 of textbook p70)

① Note the difference between the relational mode statement in the figure and the SQL statement.
② Π is select, σ is select, and the bracket () in spj means from spj.

Example 1:
Insert picture description here

Example 2:

Example 3: (This example is important!!! Pay attention to the juxtaposed connectives, such as from two tables, with a comma in the middle, such as where two conditions, and in the middle)
Insert picture description here
Example 4: (This example Important!!! Use except to find the collection of "no"...)
Insert picture description here
Example 5:
Insert picture description here

2. Use a comma after select and use and after where

3. Asc ascending order, desc descending order. (If not, the default ascending order)

Example 1: Order by Grade desc
is the descending order of grades

Example 2: Order by Sdept, Grade desc
is the ascending order of the department name, and then the grades in the same department are in descending order (there is no after the department name, so it is the default)

4. Select count (*) means statistics, and the result is a number.
Example 1: select count (distinct Sno) from Sc
The number of students who have taken courses.

Example 2: select count (*) from Student
total number of students

5. select avg (Grade) from Sc where Sno='1'
the average grade of students who took the first course

6.select max (Grade) from Sc where Sno='1'
The highest score of the student who took the first course

7. select sum (Grade) from Sc where Sno='1'
the total score of all students who took the first course

8. group by () is the aggregation
of the same kind. Example 1:
select Cno, Count (Sno)
from Sc
group by Cno,
each course number and corresponding number of courses

9.Having count(*) The star will only treat one row as a number, which means that different columns are counted as the same. This having count has a feeling of if.
(Aggregate functions cannot be used in where)

Example 1:
select Sno
from Sc
group by Sno
having group(*)>3
Student IDs of students who have selected more than three courses

Example 2: Aggregate functions cannot be used in where, so you have to use having avg() and having count to represent
Insert picture description here
10.insert
Example 1:
insert
into Sc(Sno,Cno)
values('20180101','1');
Example 2 :
Insert
into Sc
values('20180101','1');

11. Union, intersect, except
union is union, intersect is intersection, except is difference
Insert picture description here
or directly where Con='1' or Con='2'
Insert picture description here
Insert picture description here
here because it is the same Cno, so you cannot use and directly, but nest

Insert picture description here
Insert picture description here
12.update
Insert picture description here
13.delete
Insert picture description here
Insert picture description here

14.create view
Insert picture description here
Insert picture description here

15. Chapter 5 Database Security
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44575911/article/details/107218924