Personal Homepage: Personal Homepage
Series Columns: MySQL DatabaseHaha, this is the homework assigned by our teacher, I'm wondering whether to use it or not to post a blog?
Finally, I thought about it, and let's post it. Although it is very simple, it can be practiced for those friends who are just learning databases. Because there is no answer, I don't know if it is right or not. If anyone finds a mistake, please point it out.
topic:
- Check the student number, class and name of each student
- Find all information about the course
- Query which professional classes are in the database
- Query information about courses with more than 60 credit hours
- Query the student number, name and date of birth of students born in 1986
- Query the number of students and courses whose scores are above 80 for three assignments
- Query the student number, name and major class of the student surnamed Zhang
- Query the information of boys in grade 05
Query student IDs and course IDs without homework grades
- Query the total score of homework 1 of the student whose student number is 0538
- Query the number of students who have taken the K001 course
- Query how many classes are in the database
- Query the student number and the average score of homework 1, the average score of homework 2, and the average score of homework 3 for students who take three or more courses (including 3 courses)
It doesn't matter if the friends are 0 based, look at the following blog and then do the questions.
1. Create a database
create database if not exists db2 ;
Ok, the creation is successful, then we open the console
2. Create the table
1. Create a student table
analyze:
Student ID: character type
name: character
gender: character type gender is a word so varchar(1)
Professional Class: Character Type
Date of birth: time type date
Contact phone number: character phone number with 11 digits varchar(11).
drop table if exists student;
create table student
(
id varchar(10) comment '学号',
name varchar(10) NOT NULL comment '姓名',
gender char(1) comment '性别',
class varchar(20) comment '专业班级',
date date comment '出生日期',
iphone varchar(11) comment '联系电话'
)
comment '学生表';
select * from student;
2. Create a class schedule
drop table if exists student_course;
create table student_course
(
course_id varchar(10) comment '课程号',
course_name varchar(15) comment '课程名',
course_number double unsigned comment '学分数',
student_time int unsigned comment '学时数',
teacher varchar(10) comment '任课教师'
)
comment '课程表';
select *
from student_course;
3. Student worksheet
drop table if exists student_homework;
create table student_homework
(
course_id varchar(10) comment '课程号',
id varchar(10) comment '学号',
homework_1 int comment '作业1成绩',
homework_2 int comment '作业2成绩',
homework_3 int comment '作业3成绩'
)
comment '学生作业表';
select *
from student_homework;
3. Add data
Type them out one by one according to the data on the picture, woo woo woo
1. Student table
insert into student
values ('0433', '张艳', '女', '生物04', '1986-9-13', null),
('0496', '李越', '男', '电子04', '1984-2-23', '1381290xxxx'),
('0529', '赵欣', '男', '会计05', '1984-1-27', '1350222xxxx'),
('0531', '张志国', '男', '生物05', '1986-9-10', '1331256xxxx'),
('0538', '于兰兰', '女', '生物05', '1984-2-20', '1331200xxxx'),
('0591', '王丽丽', '女', '电子05', '1984-3-20', '1332080xxxx'),
('0592', '王海强', '男', '电子05', '1986-11-1', null);
Check it out:
select * from student;
2. Class Schedule
INSERT INTO student_course
values ('K001', '计算机图形学', 2.5, 40, '胡晶晶'),
('K002', '计算机应用基础', 3, 48, '任泉'),
('K006', '数据结构', 4, 64, '马跃先'),
('M001', '政治经济学', 4, 64, '孔繁新'),
('S001', '高等数学', 3, 48, '赵晓尘');
Check it out:
select *
from student_course;
3. Student worksheet
insert into student_homework values
('K001','0433',60,75,75),
('K001','0529',70,70,60),
('K001','0531',70,80,80),
('K001','0591',80,90,90),
('K002','0496',80,80,90),
('K002','0529',70,70,85),
('K002','0531',80,80,80),
('K002','0538',65,75,85),
('K002','0592',75,85,85),
('K006','0531',80,80,90),
('K006','0591',80,80,80),
('M001','0496',70,70,80),
('M001','0591',65,75,75),
('S001','0531',80,80,80),
('S001','0538',60,null,80);
Check it out:
select *
from student_homework;
4. Start doing questions
1. Check the student number, class and name of each student
select id,class,name from student;
2. Check all the information of the course
select *
from student_course;
3. Query which professional classes are in the database
select class from student;
4. Inquire about course information with more than 60 credit hours
select course_id,course_name from student_course where student_time>60;
5. Query the student number, name and date of birth of students born in 1986
select id,name,date from student where date>=('1986-1-1') AND date<('1987-1-1');
6. Inquire about the student number and course number of the three homework scores above 80 points
At first I used this query again:
select * from student_homework where homework_1>80 and homework_2>80 and homework_3>80;
I found that there was nothing, so I looked at the data and found that there is no such data for three homework scores above 80.
Therefore, the question: "Inquiry about the student number and course number with a score of 80 or more in three assignments" should include 80 points
So, I modify it:
select * from student_homework where homework_1>=80 and homework_2>=80 and homework_3>=80;
7. Query the student number, name and major class of the student surnamed Zhang
Error example:
Since I haven't written SQL for hundreds of years, I actually wrote (slap myself):
select id,name,class from student where name = '张%';
select id,name,class from student where name like '张%';
8. Query the information of boys in grade 05
select * from student where class like '%05' and gender='男';
9. Query the student number and course number without homework grades
select id,course_id from student_homework where homework_1 is null or homework_2 is null or homework_3 is null ;
10. Query the total score of homework 1 of the student whose student number is 0538
select sum(homework_1) '总分' from student_homework where id='0538';
11. Query the number of students who have taken the K001 course as an elective
select count(*) from student_homework where course_id='K001';
12. Query how many classes are in the database
select count(*) from student where class is not null ;
13. Query the student number and the average score of Homework 1, the average score of Homework 2, and the average score of Homework 3 who have taken three or more courses (including 3 courses)
select student.id, avg(homework_1), avg(homework_2), avg(homework_3)
from student
left join student_homework on student.id = student_homework.id
group by student.id
having count(course_id) >= 3;