MySQL练习

xiaoxiao2021-02-28  16

title: MySQL练习 date: 2015-12-21 22:43:15 categories: MySQL基础语法 tags: MySQL


xl_echo编辑整理,欢迎转载,转载请声明文章来源。更多案例、资料请联系QQ:1280023003

– 查看正在使用的数据库` SELECT DATABASE();

– 创建数据库 CREATE DATABASE xlecho;

– 切换数据库 USE xlecho;

– 创建student555 – 学生id – 学生姓名 – 学生性别 – 学生生日 – 学生科系 – 学生地址 CREATE TABLE student555( sid INT PRIMARY KEY AUTO_INCREMENT, sname VARCHAR(10) NOT NULL, ssex VARCHAR(3) NOT NULL, sbirthday YEAR, sdepartment VARCHAR(10), saddress VARCHAR(30) );

DROP TABLE student555; – 创建score555表 – 分数id – 学生id – 学生外键 – 学科名 – 分数 CREATE TABLE score555( scid INT PRIMARY KEY AUTO_INCREMENT, stuid INT NOT NULL, FOREIGN KEY(stuid) REFERENCES student555(sid), cname VARCHAR(30), score INT );

DROP TABLE score555;

DESC score555;

– 为student555表增加记录 INSERT INTO student555 VALUES( 901,’张老大’, ‘男’,1985,’计算机系’, ‘北京市海淀区’); INSERT INTO student555 VALUES( 902,’张老二’, ‘男’,1986,’中文系’, ‘北京市昌平区’); INSERT INTO student555 VALUES( 903,’张三’, ‘女’,1990,’中文系’, ‘湖南省永州市’); INSERT INTO student555 VALUES( 904,’李四’, ‘男’,1990,’英语系’, ‘辽宁省阜新市’); INSERT INTO student555 VALUES( 905,’王五’, ‘女’,1991,’英语系’, ‘福建省厦门市’); INSERT INTO student555 VALUES( 906,’王六’, ‘男’,1988,’计算机系’, ‘湖南省衡阳市’); – 为score555表增加记录 INSERT INTO score555 VALUES(NULL,901,’计算机’,98); INSERT INTO score555 VALUES(NULL,901, ‘英语’, 80); INSERT INTO score555 VALUES(NULL,902, ‘计算机’,65); INSERT INTO score555 VALUES(NULL,902, ‘中文’,88); INSERT INTO score555 VALUES(NULL,903, ‘中文’,95); INSERT INTO score555 VALUES(NULL,904, ‘计算机’,70); INSERT INTO score555 VALUES(NULL,904, ‘英语’,92); INSERT INTO score555 VALUES(NULL,905, ‘英语’,94); INSERT INTO score555 VALUES(NULL,906, ‘计算机’,90); INSERT INTO score555 VALUES(NULL,906, ‘英语’,85);

– 单表查询 – 查询student555表的所有记录 SELECT * FROM student555; – 查询student555表的第2条到4条记录 SELECT * FROM student555 WHERE sid>=902 AND sid<=904; SELECT * FROM student555 LIMIT 1,3; – 从student555表查询所有学生的学号(id)、姓名(name)和院系(department)的信息 SELECT sid,sname,sdepartment FROM student555; – 从student555表中查询计算机系和英语系的学生的信息 SELECT * FROM student555 WHERE sdepartment=’计算机系’ OR sdepartment=’英语系’; – 从student555表中查询年龄22~28岁的学生信息 SELECT * FROM student555 WHERE 2017 - sbirthday >22 AND 2017 - sbirthday < 28; – 从student555表中查询每个院系有多少人 SELECT sdepartment,COUNT(*) AS ‘人数’ FROM student555 GROUP BY sdepartment; – 从score555表中查询每个科目的最高分 SELECT cname,MAX(score) FROM score555 GROUP BY cname; – 计算每个考试科目的平均成绩 SELECT cname,AVG(score) FROM score555 GROUP BY cname; – 将计算机考试成绩按从高到低进行排序 SELECT * FROM score555 WHERE cname=’计算机’ ORDER BY score DESC;

– 多表查询 – 查询李四的考试科目(c_name)和考试成绩(grade) SELECT cname,score FROM score555 WHERE stuid = (SELECT sid FROM student555 WHERE sname=”李四”)

– 查询所有学生的信息和考试信息 – 显示内连接 SELECT * FROM student555 st INNER JOIN score555 so ON st.sid = so.stuid;

– 隐示内连接 SELECT * FROM student555 st, score555 so WHERE st.sid = so.stuid;

– 计算每个学生的总成绩 SELECT sname,SUM(so.score)AS’总成绩’ FROM student555 st, score555 so WHERE st.sid = so.stuid GROUP BY stuid;

– 查询计算机成绩低于95的学生信息 SELECT * FROM student555 st, score555 so WHERE st.sid = so.stuid AND cname=’计算机’ AND score > 95;

– 查询姓张或者姓王的同学的姓名、院系和考试科目及成绩 SELECT sname,sdepartment,cname,score FROM student555 st, score555 so WHERE st.sid = so.stuid AND sname LIKE ‘张%’ SELECT sname,sdepartment,cname,score FROM student555 st, score555 so WHERE st.sid = so.stuid AND sname LIKE ‘王%’

– 查询都是湖南的学生的姓名、年龄、院系和考试科目及成绩 SELECT sname,sdepartment,cname,saddress,score FROM student555 st, score555 so WHERE st.sid = so.stuid AND saddress LIKE ‘湖南%’;

转载请注明原文地址: https://www.6miu.com/read-1650226.html

最新回复(0)