Mysql安装教程和sql练习题(windows环境,内附Mysql安装包和客户端安装包)

Mysql数据库环境搭建过程

一、 安装包准备

安装包资源下载链接:https://pan.baidu.com/s/16gxnmlJmVsSbsRGTa0jTFw
提取码:fixb

二、客户端工具程序准备

资源包下载同上

三、安装mysql数据库
  1. 解压安装程序
  2. 配置环境变量
    (1) 新增 MYSQL_HOME D:\software\mysql\mysql-5.6.31-winx64
    (2) 添加Path ;%MYSQL_HOME%\bin
  3. 以管理员身份运行cmd,进入到安装目录的bin下,执行下面操作
  4. 初始化 : mysqld --initialize --user=mysql --console
    可能会出现异常,如下图:
    Mysql安装教程和sql练习题(windows环境,内附Mysql安装包和客户端安装包)_第1张图片
    报错信息主要如下:
发现报错信息为:
[ERROR] InnoDB: .\ibdata1 can't be opened in read-write mode
[ERROR] InnoDB: The system tablespace must be writable!
ps:此处错误不解决,在启动服务时会失败
解决方案:
  • 通过任务管理器终止mysqld进程
  • 打开mysql安装目录下的data文件夹,重命名这2个文件:ib_logfile0 和
    ib_logfile1,这两个文件在初始化的时候会重新生成
  • 重新执行初始化
  1. 注册服务: mysqld --install MySQL
  2. 启动服务:net start mysql
  3. 登录: mysql -uroot -p ,执行后会要求输入密码,第一次登录时直接按回车即可登录

登录成功后显示:
Mysql安装教程和sql练习题(windows环境,内附Mysql安装包和客户端安装包)_第2张图片

  1. 修改密码: set password for root@localhost = password(‘123456’);、
  2. 关闭服务:net stop mysql
四、安装mysql客户端

解压文件安装即可,过程省略。。。。

放大招

如果上述过程中出现错误,解决不了。直接把解压后的安装路径下的文件全部删除,重新解压再次按照上述步骤执行即可安装成功,亲测有效

--------------------------------------------------------------------------------------------------------------------------------------

mysql语句练习汇总

一、建表语句和数据准备请自行下载,地址为

   GitHub地址: https://github.com/BigData404/tools
   文件为: mysql-create_tables_insert_data.txt

二、练习题

#查询"01"课程比"02"课程成绩高的学生的信息及课程分数

SELECT stu.*,a.`score`,b.`score` FROM t_students stu
LEFT JOIN t_score a ON stu.`stu_id`=a.`stu_id` AND a.`course_id`=1
LEFT JOIN t_score b ON stu.`stu_id`=b.`stu_id` AND b.`course_id`=2
WHERE a.`score` < b.`score`

#查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩

SELECT b.*,a.ss AS '平均成绩' FROM
(SELECT stu_id,AVG(score) AS ss FROM t_score GROUP BY stu_id HAVING AVG(score)<=60) a
LEFT JOIN t_students b ON a.`stu_id`=b.`stu_id`

#查询所有同学的学生编号. 学生姓名. 选课总数. 所有课程的总成绩

SELECT a.stu_id,a.stu_name,b.aa,b.bb
FROM t_students a
LEFT JOIN (SELECT stu_id,COUNT(course_id) AS aa,SUM(score) AS bb FROM t_score GROUP BY stu_id) b ON a.`stu_id`=b.`stu_id`

#查询学过"贝吉塔"老师授课的同学的信息

SELECT * FROM 
t_students a
LEFT JOIN t_score b ON a.`stu_id`=b.`stu_id`
LEFT JOIN t_courses c ON b.course_id=c.course_id
LEFT JOIN t_teachers d ON c.teacher_id=d.teacher_id
WHERE d.teacher_name<>'贝吉塔'

#查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息

SELECT * FROM t_students stu
JOIN (SELECT * FROM t_score WHERE course_id=1) a ON stu.`stu_id`=a.`stu_id`
JOIN (SELECT * FROM t_score WHERE course_id<>2) b ON a.`stu_id`=b.`stu_id`

#查询没有学全所有课程的同学的信息

SELECT * FROM 
(SELECT stu_id,COUNT(DISTINCT course_id) AS con FROM t_score GROUP BY stu_id ) a 
LEFT JOIN t_students c ON a.stu_id=c.stu_id
LEFT JOIN (SELECT COUNT(DISTINCT course_id) AS conn FROM t_courses)  b ON a.con=b.conn 
WHERE b.conn IS NULL

#查询和"01"号的同学学习的课程完全相同的其他同学的信息

SELECT a.*,c.* FROM 
(SELECT stu_id,GROUP_CONCAT(course_id) AS g2  FROM `t_score` WHERE stu_id<>1 GROUP BY stu_id ) a
LEFT JOIN (SELECT stu_id,GROUP_CONCAT(course_id) AS g1 FROM `t_score` WHERE stu_id=1 GROUP BY stu_id) b ON a.g2=b.g1
LEFT JOIN t_students c ON a.stu_id=c.stu_id
WHERE b.`stu_id` IS NOT NULL

#查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩

SELECT * FROM
(
SELECT b.`stu_id`,AVG(b.`score`) FROM 
(SELECT stu_id,COUNT(1) FROM `t_score` WHERE score<60 GROUP BY stu_id HAVING COUNT(1)>1) a
LEFT JOIN t_score b ON a.`stu_id`=b.`stu_id`
GROUP BY b.`stu_id`
) aa 
LEFT JOIN t_students c ON aa.`stu_id`=c.stu_id

#检索"01"课程分数小于60,按分数降序排列的学生信息

SELECT * FROM `t_score` aa
LEFT JOIN t_students c ON aa.`stu_id`=c.stu_id
WHERE score<60 AND course_id=1
ORDER BY score DESC 

你可能感兴趣的:(Mysql)