MySQL之初识MySQL
一、初识MySQL
javaEE:企业级java开发,主要是做Web。
Web分为前端(页面:展示数据库数据)、后台(连接点:连接数据库JDBC,连接前端(控制视图跳转、给前端传递数据))、数据库(存数据)。
必须要学操作系统、数据结构与算法。学好离散数学、数字电路、体系结构、编译原理就更好了,再有实战经验就是优秀的程序猿了。
1.为什么要学习数据库
①岗位需求
②现在的世界是大数据时代,得数据者得天下。
③被迫需求:存数据。
④数据库是所有软件体系中最核心得存在。相应岗位:DBA数据库管理员。
2.什么是数据库
数据库(DB,DateBase)
概念:数据仓库,是个软件,安装在操作系统(linux,windows,mac···)之上。用SQL语句执行,可以储存大量的数据。500万条以下的数据可以直接存储,500万条以上需要进行索引的优化。
作用:存储数据、管理数据。
3.数据库分类
1)关系型数据库:SQL
主要有MySQL、Oracle、Sql Server、DB2、SQLlite
通过表和表之间,行和列之间的关系进行数据存储。
2)非关系型数据库:NoSQL(Not Only)
主要有:Redis,MongoDB
以对象存储,通过对象的自身属性来决定。
3)DBMS(数据库管理系统)
数据库的管理软件,科学有效的管理数据,维护和获取数据。

MySQL,数据库管理系统!
4.MySQL简介
MySQL是一个关系型数据库管理系统。
前身:属于瑞典的MySQL AB公司,如今:属于Oracle旗下产品。
MySQL是最好的RDBMS(Relational Database Management System,关系型数据库管理系统)应用软件之一。
是开源的数据库软件,具有体积小,速度快,总体拥有成本低的特点。
适用于中小型或大型网站,可以做集群。
官网:https://www.mysql.com 版本:5.7(稳定)、8.0
官网下载地址:https://dev.mysql.com/downloads/mysql/
安装建议:Ⅰ尽量不要使用.exe安装,会往注册表里走,不好卸载
Ⅱ尽可能使用压缩包安装
5.安装MySQL
MySQL 5.7下载地址:https://dev.mysql.com/downloads/mysql/5.7.html
1)操作步骤:
①下载后得到zip压缩包。
②解压到要安装的目录,本人解压到的是D:\MySQL\mysql-5.7.32。

③配置环境变量:Ⅰ右击我的电脑——>属性——>高级系统设置——>环境变量


Ⅱ选择PATH——>新建——>添加mysql安装文件下面的bin文件夹地址


④在D:\MySQL\mysql-5.7.32下新建并编辑文件my.ini文件,注意文件路径。

[mysqld]
basedir=D:\MySQL\mysql-5.7.32\
datadir=D:\MySQL\mysql-5.7.32\data\
port=3306
skip-grant-tables

⑤启动管理员模式下的CMD,并将路径切换至mysql下的bin目录,然后输入
mysqld -install
(安装mysql)。
⑥再输入
mysqld --initialize-insecure --user=mysql
初始化数据文件。

⑦然后输入
net start mysql
启动mysql,并用命令
mysql -u root -p
进入mysql的管理界面(密码可为空)

⑧进入界面后修改root密码,密码改为123456。
update mysql.user set authentication_string=password('123456') where user='root' and Host = 'localhost';
⑨最后输入
flush privileges;
刷新权限。

⑩修改my.ini文件,删除最后一句skip-grant-tables(可在前面添加#注释掉)。

⑪重启mysql即可正常使用,分别输入
exit
net stop mysql
net start mysql

⑫进入管理界面,输入密码,出现以下结果就安装好了。

2)安装时可能遇到的问题
①缺少组件.dill
②命令可能输入错误。
③mysql命令行与windows命令行弄混。
④net命令输入不存在,环境变量PATH中与系统盘相关的可能被删除。
安装失败,可以输入
sc delete mysql
清空服务,重新安装。
6.安装SQLyog
1)简单安装,将语言设置为Chinese(Simplified)。





2)打开,新建联系,名称为localhost,连接数据库。



3)右击root@localhost,点击刷新对象浏览器。
4)右击root@localhost,创建一个数据库school,设置基字符集为utf8,数据库排序规则为utf8_general_ci。

注:每一个sqlyog的执行操作,本质就对应了一个sql,可以再软件的历史记录中查看。
5)新建一张表student,右击表,选择创建表,设置表名称为student,引擎为InnoDB,字符集为utf8,核对为utf8_general_ci。

6)填写三个列名id,name,age,并填写后面内容,最后点击保存。

主键:唯一标识,不能重复。
非空:必须要有值,否则就报错。
Zerofill:零填充。
7)查看表:右击student,选择打开表。

8)尝试添加多条记录,刷新即可保存。

7.连接数据库
1)命令行连接
mysql -u root -p123456 --连接数据库
update mysql.user set authentication_string=password('123456') where user='root' and Host = 'localhost'; ---修改用户密码
flush privileges; --刷新权限
show databases; --查看所有数据库
use school --切换数据库:use 数据库名 显示Database changed即切换成功
show tables; --查看数据库中的所有表
describe student; --显示数据库中表的信息:describe 表名
create database westos; --创建一个数据库westos
exit; --退出连接


--:单行注释 /* */:多行注释
2)数据库的语言 核心:CRUD增删改查
DDL定义,DML操作,DQL查询,DCL控制