Database system (1)-data query

1. The purpose of the experiment:

  1. Familiar with SQL to define data tables and indexes;

  2. Can use SQL to complete single-table query, multi-table query and nested query operations of data.

2. Experimental content:

  1. Create a database according to the specified scenario;

  2. Write the corresponding SQL query statement according to the specific query application requirements, and get the correct query result.
    1. Familiar with the data query function of SQL based on single table;
    2. Master the usage of GROUP BY clause, HAVING clause and ORDER BY clause
    based on single table ; 3. Master the predicate based on single table (NOT) IN etc. Usage, master the usage of aggregate functions;
    4. Master the applicable situation of multi-table join query and the method of sentence construction;

3. Experimental process:

1. Create a student course database (stu_course):
create database stu_course; 
2. Creation and modification of student table, course table and student selection table:
2.1. Create a data table:
create table Student(
  	Sno char(9)primary key,
  	Sname char(20)unique,
  	Ssex char(2),
	Sage smallint,
	Sdept char(20)	
);

create table Course (
	Cno char(4)primary key,
	Cname char(4) not null,
  	Cpno char(4),
	Ccredit smallint ,
 	foreign key(Cpno) references Course(Cno) 
);

create table SC(
	Sno char(9),
	Cno char(4),
	Grade smallint,
	primary key(Sno,Cno),
	foreign key(Sno) references Student(Sno),
	foreign key(Cno) references Course(Cno)
);
2.2. Modify the data table:

Add a column of enrollment time in the Student table, the data type is date type:

alter table Student add S_entrance Date;

Change the data type of age from character type (assuming it was originally character type) to integer type:

alter table Student alter column Sage int;

The additional course name must have unique constraints:

alter table Course add unique(Cname);

Delete the Student table:

drop  table Student;
3. Insert data:
INSERT INTO student VALUES ('201215121', '李勇', '男', 20, 'CS');
INSERT INTO student VALUES ('201215122', '刘晨', '女', 19, 'CS');
INSERT INTO student VALUES ('201215123', '王敏', '女', 18, 'MA');
INSERT INTO student VALUES ('201215125', '张立', '男', 19, 'IS');

INSERT INTO course VALUES ('1', '数据库', '5', 4);
INSERT INTO course VALUES ('2', '数学', NULL, 2);
INSERT INTO course VALUES ('3', '信息系统', '1', 4);
INSERT INTO course VALUES ('4', '操作系统', '6', 3);
INSERT INTO course VALUES ('5', '数据结构', '7', 4);
INSERT INTO course VALUES ('6', '数据处理', NULL, 2);
INSERT INTO course VALUES ('7', 'PASCAL语言', '6', 4);

INSERT INTO sc VALUES ('201215121', '1', 92);
INSERT INTO sc VALUES ('201215121', '2', 85);
INSERT INTO sc VALUES ('201215121', '3', 88);
INSERT INTO sc VALUES ('201215122', '2', 90);
INSERT INTO sc VALUES ('201215122', '3', 80);
4. Various types of query operations:
4.1. Single table query operation:

1. Query the specified column :

Check the student ID and name of all students:

select Sno,Sname from Student;

2. Query all columns :

Query the information of all students:

select * from Student;

3. Query the calculated value :

Query the name and year of birth of all students:

select Sname 2021-Sage from Student;

Insert picture description here
4. Eliminate duplicate tuples :

select Distinct Sno from Student; 

5. Query tuples that meet the conditions :

Inquire about all students in the Department of Computer Science:

select Sname from Student where sdept='CS';

Query students younger than 20:

select * from student where Sage <20;

Query the name, department and age of students between 20 and 23 years old:

select sname,sdept,sage from student where sage between 20 and 23; 

Query the name, department and age of students who are not between 20 and 23 years old:

select sname,sdept,sage from student where sage not between 20 and 23; 

6. Determine the set (IN) :

Inquire about the name and gender of students in the Department of Computer Science (CS), Department of Mathematics (MA) and Department of Information (IS).

select Sname,Ssex from student where Sdept in('CS','MA','IS'); 

The query is not the name and gender of students in the Department of Computer Science (CS), Department of Mathematics (MA) and Department of Information (IS).

select Sname,Ssex from student where Sdept not in('CS','MA','IS'); 

7. Character matching (fuzzy query) :

like :字符串匹配
% :代表任意长度字符
_ :代表单个字符

Query the information of the student whose student number is 201215121:

select * from where Sno like '201215121';

Query the name, student ID and gender of all students surnamed Liu:

select Sname,Sno,Ssex from student where Sname like'刘%'

Query the name, student ID and gender of students who are not surnamed Liu:

select Sname,Sno,Ssex from student where Sname not like'刘%'

Query the name and student ID of the student whose second word is "Yang":

select Sname,Sno from student where Sname like'_阳%';

8. Multi-condition query (and, or) :

Query the student names of students under the age of 20 in the Department of Computer Science:

select Sname from student where Sdept='CS' and Sage < '20';

9、order by

Query the student ID and grades of the students who have chosen course No. 3, sorted in descending order of grades (default ascending order):

select Sno,Grade from SC where Cno='3' order by Grade DESC;

10. Aggregate function :

count(*)                     统计元组个数
count([distinct|all] <列名>)  统计一列中值的个数
Sum([distinct|all] <列名>)    求一列总和
Max([distinct|all] <列名>)    求一列中的最大值
Min([distinct|all] <列名>)    求一列中的最小值
Avg([distinct|all] <列名>)    求一列的平均值

Query the total number of students:

select count(*) from student;

Query the total number of elective courses:

select count(distinct Sno) from student;

Check the student's total score of 201215012 elective courses:

select sum(Ccredit) from SC,Course where Sno='201215121' and SC.Cno = Course.Cno;

11. Group by :

Find the number of each course and the corresponding number of courses:

select Cno,count(*) from SC group by Cno;

If you need to filter these groups according to certain conditions after grouping, you can use having to specify the filter conditions.

Query the student ID of students who have taken more than one course:

select Sno from SC group by Sno having count(*)>1;

Note: Aggregate functions cannot be used in the where clause! Need to use having instead

4.2. Multi-table join query operation:

Querying data from two or more tables at a time is called a join query.

Inquire about the situation of each student and his elective courses:

select student.*,SC.* from student,SC where student.Sno=SC.Sno; 

Inquire about the student ID and name of all students who have taken course No. 2 and scored above 90 points:

select student.Sno,Sname from Student,SC where Student.Sno=SC.Sno and SC.Cno='2' and SC.Grade>90;
4.3. Nested query operation:

Four, summary:

  • HAVING is used in conjunction with GROUP BY, placed after GROUP BY, and the function at this time is equivalent to WHERE.

  • There can be no aggregate functions in the conditions behind WHERE, such as SUM(), AVG(), etc., but HAVING can

Guess you like

Origin blog.csdn.net/qq_43531669/article/details/111757037