MySQL Data Query small practice

operation

# 创建班级表
create table class (
cls_id int auto_increment primary key,
cls_name varchar(10) not null default ''
);

insert into class (cls_name) values ('三年二班'), ('一年三班'), ('二年一班');

# 创建老师表
create table teacher (
t_id int auto_increment primary key,
t_name varchar(10) not null default ''
);

insert into teacher (t_name) values ('罗老师'), ('厨子'), ('乔帮主');

#创建学生表
create table student (
s_id int auto_increment primary key,
s_name varchar(10) not null default '',
gender enum('男', '女') not null default '男',
class_id int not null default 1,
constraint fk_std_cls foreign key (class_id) references class(cls_id)
);

insert into student (s_name, gender, class_id ) values ('雷军', '男', 1), ('余大嘴', '男', 2), ('黄章', '女', 3);

# 创建课程表
create table course (
c_id int auto_increment primary key,
c_name varchar(10) not null default '',
teacher_id int not null default 1,
constraint fk_course_teacher foreign key (teacher_id ) references teacher(t_id)
);

insert into course (c_name, teacher_id ) values ('Python', 3), ('C', 1), ('Go',2);


# 创建成绩表
create table score (
scr_id int auto_increment primary key,
student_id int not null default 1,
course_id int not null default 1,
scr_num int not null default 0,
constraint fk_scr_std foreign key (student_id ) references student(s_id),
constraint fk_scr_crs foreign key (course_id ) references course(c_id)
);

insert into score (student_id, course_id, scr_num) values (1, 3, 80), (2, 1, 70), (1, 2, 60);

# 班级
+--------+----------+
| cls_id | cls_name |
+--------+----------+
|      1 | 三年二班 |
|      2 | 一年三班 |
|      3 | 二年一班 |
+--------+----------+

# 老师
+------+--------+
| t_id | t_name |
+------+--------+
|    1 | 罗老师 |
|    2 | 厨子   |
|    3 | 乔帮主 |
+------+--------+

# 学生
+------+--------+--------+----------+
| s_id | s_name | gender | class_id |
+------+--------+--------+----------+
|    1 | 雷军   | 男     |        1 |
|    2 | 余大嘴 | 男     |        2 |
|    3 | 黄章   | 女     |        3 |
+------+--------+--------+----------+

# 课程
+------+--------+------------+
| c_id | c_name | teacher_id |
+------+--------+------------+
|    1 | Python |          3 |
|    2 | C      |          1 |
|    3 | Go     |          2 |
+------+--------+------------+

# 分数
+--------+------------+-----------+---------+
| scr_id | student_id | course_id | scr_num |
+--------+------------+-----------+---------+
|      1 |          1 |         3 |      80 |
|      2 |          2 |         1 |      70 |
|      3 |          1 |         2 |      60 |
+--------+------------+-----------+---------+
  • Queries for all students is more than 60 points name and student number (DISTINCT: de-emphasis)
mysql> select distinct s_name, student_id  from score left join student on student_id=student.s_id where scr_num > 60;
+--------+------------+
| s_name | student_id |
+--------+------------+
| 雷军   |          1 |
| 余大嘴 |          2 |
+--------+------------+
2 rows in set (0.00 sec)
  • Discover every teacher teaches courses and the number of teachers information
mysql> select t_name, count(t_name) as crs_count from course left join teacher on teacher_id=teacher.t_id group by t_name;
+--------+-----------+
| t_name | crs_count |
+--------+-----------+
| 乔帮主 |         1 |
| 厨子   |         1 |
| 罗老师 |         1 |
+--------+-----------+
3 rows in set (0.00 sec)
  • Class information query of students and student information is located
mysql> select * from student left join class on class_id=class.cls_id;
+------+--------+--------+----------+--------+----------+
| s_id | s_name | gender | class_id | cls_id | cls_name |
+------+--------+--------+----------+--------+----------+
|    1 | 雷军   | 男     |        1 |      1 | 三年二班 |
|    2 | 余大嘴 | 男     |        2 |      2 | 一年三班 |
|    3 | 黄章   | 女     |        3 |      3 | 二年一班 |
+------+--------+--------+----------+--------+----------+
3 rows in set (0.00 sec)
  • The number of students in the number of boys and girls

mysql> select gender, count(gender) from student group by gender;
+--------+---------------+
| gender | count(gender) |
+--------+---------------+
| 男     |             2 |
| 女     |             1 |
+--------+---------------+
2 rows in set (0.00 sec)
  • Get all study "Python" student school number, name and achievements

mysql> select c_name, s_id, s_name, scr_num from score left join student on student_id=student.s_id left join course on course_id=course.c_id where c_name='Python';
+--------+------+--------+---------+
| c_name | s_id | s_name | scr_num |
+--------+------+--------+---------+
| Python |    2 | 余大嘴 |      70 |
+--------+------+--------+---------+
1 row in set (0.00 sec)
  • Discover an average score greater than 60 points of information and the average score of students
mysql> select s_id, s_name, avg(scr_num) from score left join student on student_id=student.s_id group by s_name having avg(scr_num) > 60;
+------+--------+--------------+
| s_id | s_name | avg(scr_num) |
+------+--------+--------------+
|    2 | 余大嘴 |      70.0000 |
|    1 | 雷军   |      70.0000 |
+------+--------+--------------+
2 rows in set (0.00 sec)
  • Queries teacher named "Law" of the number;
mysql> select t_name, count(t_name) from teacher group by t_name having t_name like '罗%';
+--------+---------------+
| t_name | count(t_name) |
+--------+---------------+
| 罗老师 |             1 |
+--------+---------------+
  • Queries course score less than 70 points the students school, name
mysql> select s_id, s_name, scr_num from score left join student on student_id=student.s_id where scr_num < 70;
+------+--------+---------+
| s_id | s_name | scr_num |
+------+--------+---------+
|    1 | 雷军   |      60 |
+------+--------+---------+
1 row in set (0.00 sec)
  • Query subjects highest score and lowest points: Displayed in the following form: course name, highest score, lowest score;
mysql> select c_name, max(scr_num), min(scr_num) from score left join course on course_id=course.c_id group by c_name;
+--------+--------------+--------------+
| c_name | max(scr_num) | min(scr_num) |
+--------+--------------+--------------+
| C      |           60 |           60 |
| Go     |           80 |           80 |
| Python |           70 |           70 |
+--------+--------------+--------------+
3 rows in set (0.00 sec)
  • Query name "Ray," the list of students
mysql> select * from student where s_name like '雷%';
+------+--------+--------+----------+
| s_id | s_name | gender | class_id |
+------+--------+--------+----------+
|    1 | 雷军   | 男     |        1 |
+------+--------+--------+----------+
1 row in set (0.00 sec)
  • Queries grade point average in each course, results in ascending order according to grade point average, grade point average is the same, according to the course numbers in descending order

mysql> select c_name, avg(scr_num) from score left join course on course_id=course.c_id group by c_name order by avg(scr_num) asc, c_id desc;
+--------+--------------+
| c_name | avg(scr_num) |
+--------+--------------+
| C      |      60.0000 |
| Python |      70.0000 |
| Go     |      80.0000 |
+--------+--------------+
3 rows in set (0.00 sec)
  • Query No. 3 courses and course grade school student number and name in 70 points or more
mysql> select s_id, s_name from score left join student on student_id=student.s_id left join course on course_id=course.c_id where c_id=3 and scr_num > 70;
+------+--------+
| s_id | s_name |
+------+--------+
|    1 | 雷军   |
+------+--------+
1 row in set (0.00 sec)
  • Query "2" course students score less than 70, in descending order by score of student number
mysql> select s_id from score left join student on student_id=student.s_id left join course on course_id=course.c_id where c_id=2 and scr_num < 70 order by scr_num desc;
+------+
| s_id |
+------+
|    1 |
+------+
1 row in set (0.00 sec)
  • "1" Course delete student number "2" of the students achievements

mysql> delete from score where student_id=2 and course_id=1;
Query OK, 1 row affected (0.00 sec)

mysql> select * from score;
+--------+------------+-----------+---------+
| scr_id | student_id | course_id | scr_num |
+--------+------------+-----------+---------+
|      1 |          1 |         3 |      80 |
|      3 |          1 |         2 |      60 |
+--------+------------+-----------+---------+
2 rows in set (0.00 sec)
  • Delete learning "Joe closer" lesson SC table records

Guess you like

Origin www.cnblogs.com/bigb/p/11769012.html