练习题MySQL数据库挑战25道练习题(25道mysql数据库)
练习题MySQL数据库:挑战25道练习题
MySQL是现今最流行的关系型数据库管理系统之一。在今天的IT领域中,了解MySQL成为许多工程师的必备技能。因此,练习题MySQL数据库成为了许多初学者最好的方法。下面我们来挑战一下25道练习题,看看你掌握MySQL的程度如何。
1.创建一个数据库和一个表,表名称为students,字段包括学生id、名字、班级和学号。
CREATE DATABASE IF NOT EXISTS test;
USE test;
CREATE TABLE IF NOT EXISTS students(id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,class VARCHAR(50) NOT NULL,
student_num INT NOT NULL);
2.插入5条学生数据。
INSERT INTO students(name,class,student_num) VALUES('张三','一班',1),
('李四','二班',2),('王五','三班',3),('赵六','四班',4),('钱七','五班',5);
3.查询表students中所有数据。
SELECT * FROM students;
4.查询表students中有多少个学生。
SELECT COUNT(*) FROM students;
5.查询表students中学生id大于3的学生。
SELECT * FROM students WHERE id>3;
6.查询表students中班级为二班的学生。
SELECT * FROM students WHERE class='二班';
7.查询表students中名字以“张”开头的学生。
SELECT * FROM students WHERE name LIKE '张%';
8.查询表students中学号为2或4的学生。
SELECT * FROM students WHERE student_num IN(2,4);
9.查询表students中学号不为3的学生。
SELECT * FROM students WHERE student_num!=3;
10.查询表students中学生按照学号升序排列。
SELECT * FROM students ORDER BY student_num ASC;
11.查询表students中学生按照id降序排列。
SELECT * FROM students ORDER BY id DESC;
12.查询表students中学号为2的学生名字修改为“小李子”。
UPDATE students SET name='小李子' WHERE student_num=2;
13.查询表students中删除学号为5的学生。
DELETE FROM students WHERE student_num=5;
14.在表students中添加一个电话号码字段,字段类型为varchar。
ALTER TABLE students ADD phone VARCHAR(20);
15.查询表students中所有学生,并在电话号码字段填入数据。
UPDATE students SET phone='123456789' WHERE id=1;
UPDATE students SET phone='111111111' WHERE id=2;UPDATE students SET phone='222222222' WHERE id=3;
UPDATE students SET phone='333333333' WHERE id=4;
16.查询表students中按照班级分组,计算每个班级的总人数。
SELECT class,COUNT(*) FROM students GROUP BY class;
17.查询表students中按照班级分组,计算每个班级的学号总和。
SELECT class,SUM(student_num) FROM students GROUP BY class;
18.查询表students中按照班级分组,计算每个班级的学生平均学号。
SELECT class,AVG(student_num) FROM students GROUP BY class;
19.查询表students中班级为一班的学生总数和平均学号。
SELECT COUNT(*),AVG(student_num) FROM students WHERE class='一班';
20.查询表students中学号最大值和最小值。
SELECT MAX(student_num),MIN(student_num) FROM students;
21.查询表students中所有学生按照班级升序排列,班级相同的按照学号升序排列。
SELECT * FROM students ORDER BY class ASC,student_num ASC;
22.查询表students中学号为1、2、3、4、5的学生名字和班级。
SELECT name,class FROM students WHERE student_num IN(1,2,3,4,5);
23.查询表students中学号为1、2、3、4、5的学生名字和班级,并按照班级升序排列。
SELECT name,class FROM students WHERE student_num IN(1,2,3,4,5) ORDER BY class ASC;
24.查询表students中学生id在1到3之间的学生名字和班级。
SELECT name,class FROM students WHERE id>1 AND id
25.查询表students中学生id在1到3之间的学生名字和班级,并按照学号降序排列。
SELECT name,class FROM students WHERE id>1 AND id
以上就是25道MySQL练习题。通过这些练习题,你将可以加深对MySQL数据库的了解,提高你的技能。继续挑战自己吧,学习MySQL的道路上还有很长的路要走。