MySQL数据库基础学习
1-数据库相关名词
- 数据库: DataBase(DB);
- 数据库管理员:DataBase Administrator(DBA);
- 数据库管理系统:DataBase Management System(DBMS);
- 数据库系统:DataBase System(DBS);
2-MySQL介绍
-
MySQL:数据库
-
SQL语言:
-
DDL(Data defination language):数据定义语言
关键字有 create /alter /drop …等
-
DML (Data Manipulation language) : 数据操作语言
关键字有 insert /update /delete… 等
-
DQL(Data quary language ):数据查询语言
关键字 select …
-
DCL(Data control language):数据控制语言
关键字 grant / comit/rollback…
-
-
MySQL功能:对数据进行持久化存储
3-MySQL环境搭建(windows)
- 从官网下载对应的版本
- 直接点击安装程序,进行安装
- 安装过程直接点击下一步就行(可以选择自动配置环境变量,也可安装完成后手动配置)
- 查看数据库安装情况:
- 1、输入 services.msc ,进入windows 服务管理。查看是否有MySQL服务正在运行
- 2、使用cmd命令,
myslq -uroot -p
,可以进入数据库即安装成功
4-连接MySQL
[]中的内容表示可写可不写
-
语法格式 :
mysql [-h IP地址/主机名] -u root -p [密码] [数据库名]
-- 连接方式1:连接数据库,但不指定数据库名 mysql -u root -p -- 连接方式2:连接数据库,同时制定数据库名 mysql -u root -p student -- 连接方式3:连接指定IP地址的数据库 mysql -h 127.0.0.1 -u root -p student -- 连接方式4: 可将-u和用户名连接,-p和密码连接 mysql -uroot -p123 student
5-MySQL基本操作
-
除
use 数据库名
等少数命令外,都需要以分号结尾。 -
MySQL 命令可以在任何空格的位置进行换行,命令一般是以分号作为结束。
-
\c
取消一条命令的执行。 -
help
、h
、?
都可用来查看帮助信息 -
\s
查看当前数据库服务器的状态 -
\G
将查询结果纵向显示 -
quit
、\q
、exit
都是退出数据库命令
5.1-针对数据库的基本操作
-- 查看MySQL中的所有数据库
show databases;
-- 查看当前正在使用的数据库
select database();
-- 创建一个数据库
create database 数据库名;
-- 进入一个数据库
use 数据库名
-- 显示创建数据库的信息
show create database 数据库名;
-- 删除数据库
drop database 数据库名;
5.2-针对数据表的基本操作
- 数据表结构字段:
- feild : 字段名
- type : 字段类型
- null : 是否允许为空
- key : 索引
- defualt : 默认值
- extra : 属性
-- 显示当前数据库中存在的所有数据表
show tables;
-- 创建一个数据表,未加约束条件
-- 语法: create table 表名(字段名1 字段类型 [约束条件],...)
create table `stu`(`id` int,`name` varchar(60));
-- 查看数据表的结构
-- 语法: desc 表名;
desc `stu`;
-- 修改表结构
-- 语法: alter table 表名 操作
alter table `stu` add `sex` varchar(20);
-- 插入数据
-- 语法: insert into 表名(字段1...) values (值1...)
insert into `stu`(`name`,`age`,`sex`) values('Paul',18,'m');
-- 查询数据
-- 语法:select 字段1... from 表名
select `name`,`age`,`sex` from `stu`;
-- 修改数据
-- 语法:update 表名 set 字段名 = 值 [where 条件];
update `stu` set `age` = 18 where `name` = 'Lily';
-- 删除数据
-- 语法:delete from 表名 [where 条件];
delete from `stu` where `name` = 'Lily';
-- 删除数据表
-- 语法: drop table 表名;
drop table `stu`;
-- 显示创建表的内容
-- 语法 : show create table 表名;
show create table `stu`;
6-数据库四大数据类型
6.1-数值类型
数值类型 | 占用空间(字节) | 描述 |
---|---|---|
tinyint | 1 | 非常小的整数 |
smallint | 2 | 比较小的整数 |
mediumint | 3 | 中等大小的整数 |
int | 4 | 标准的整数 |
bigint | 8 | 大整数类型 |
float | 4~8 | 单精度浮点型 |
double | 8 | 双精度浮点型 |
decimal | 自定义 | 以字符串的形式显示浮点数 |
粗体表示经常用到的类型
- 类型后加的数值,如 int(3),代表显示3位,并不是只保存了3位
- float(5,2) ,表示浮点数,除小数点总共可表示5位,小数点后两位
6.2-字符串类型
加粗表示常用
字符串类型 | 空间(字节) | 描述 |
---|---|---|
char[M] | M 长度 0~255之间 | 不可变长字符串 |
varchar[M] | length +1 长度 0~65535之间 | 可变长字符串 |
tinyblob/tinytext | length +1 | 小二进制和文本串 |
blob/text | length +2 | 小二进制和文本串 |
mediumblob/mediumtext | length+3 | 中二进制和文本串 |
longblob/longtext | length+4 | 大二进制和文本串 |
enum | 1/2 | 枚举 |
set | 1/2/3/4/8 | 集合 |
-
枚举类型,在定义的时候需要给出所有的枚举值,赋值的时候必须要从给定的值中选择一个
-
集合类型,在定义的时候需要给出集合,赋值 可以选择一个或者多个
-
blob/text 通常都是用来存储文件之类的大型数据
-
char 类型长度不可变,少于给定长度会进行补0显示,空间占用大,但是查询比varchar快,varchar长度可变,空间占用小。
6.3-时间和日期类型
时间和日期类型实际中基本不使用,使用时间戳代替
时间日期类型 | 占用空间(字节) | 描述 |
---|---|---|
date | 3 | YYYY-MM-DD |
time | 3 | hh-mm-ss |
datetime | 8 | YYYY-MM-DD hh-mm-ss |
timestamp | 4 | YYYYMMDDhhmmss |
year | 1 | YYYY |
6.4-NULL值
- NULL代表一个值,不是没有值
- NULL不能进行算术运算,或者说运算之后得到的结果还是NULL
- 在MySQL中 0 和 NULL 代表false ,其它值代表true
7-字段属性
- unsigned :无符号正整数
- zerofill : 用0 补齐不足的位数
- auto_increment: 自增
- null / not null : 允许为空/不允许为空
- default : 对字段设置默认值
8-索引
- primary key : 主键,一个数据表只有一个字段能设置
- unique : 唯一索引,加了唯一索引的字段,不能添加重复的值
- index : 普通索引
- fulltext : 全文索引
索引其实也是一张表,按照索引的规则重新存了一份数据,每次查询都会先去索引表查询,找到了位置 再从原数据表查找
索引的好处:使用索引,可以大大的提高数据的查询效率
索引的坏处:保存数据的时候需要保存索引表数据,会多占用空间,对数据进行 insert、delete、update等操作,不但要更新原数据,还要更新索引数据,会降低 insert、delete、update 的效率。
9-数据表的类型
- MyISAM:
- 优点:成熟稳定,效率高,便于管理
- 缺点:不支持事务处理,不支持外键
- innoDB :
- 优点:可以进行事务处理
- 缺点:数据表效率一般
MySQL默认创建的数据表为 innoDB类型
-
innoDB 和 MyISAM类型的切换
-- 语法 :alter table 表名 engine = 表类型; alter table `stu` engine = 'MyISAM';
10-修改数据表结构
-- 语法: alter table 表名 操作
-- 修改字段名同时修改属性
-- 语法: alter table 表名 change 原字段名 修改后字段名 字段完整信息;
alter table `stu` change `name` `stuName` varchar(128) not null;
-- 修改字段时,不修改字段名
-- 语法: alter table 表名 modify 字段名 字段完整信息;
alter table `stu` modify `stuName` varchar(64) not null;
-- 添加新字段
-- 语法: alter table 表名 add 新字段名 字段完整信息;
alter table `stu` add `test11` int unsigned not null;
-- 删除字段
-- 语法: alter table 表名 drop 字段名;
alter table `stu` drop `test11`;
-- 修改表名
-- 语法: alter table 原表名 rename as 新表名;
alter table `stu` rename as `student`;
-- 更改表的自增属性 ,只有在表为空的时候才会生效
-- 语法:alter table 表名 auto_increment = 'xx';
alter table `stu` auto_increment = 1;
11-删除/恢复数据库
- **方式一:**使用可视化工具
- 导出数据库结构和数据
- 删除数据库
- 创建数据库
- 导入之前备份的数据
- **方式二:**使用cmd 命令
- 使用cmd命令:
mysqldump -u用户名 -p密码 数据库名 > 文件名.sql
- 进入数据库:
mysql -u用户名 -p密码
- 删除备份的数据库:
drop database 数据库名
- 退出数据库
exit
- 创建数据库:
create database 数据库名;
- 恢复数据库 cmd 窗口 :
mysql -u用户名 -p密码 数据库名 < 备份文件路径
- 使用cmd命令:
-- 删除数据库
-- 语法: drop database 数据库名;
-- 删除数据表
-- 语法: drop table 数据表名;
12-insert 命令
-- 方法一: insert into 表名(字段1...字段n) values (值1...值n);
-- 推荐方法:一个值对应一个字段,不容易出错
-- 方法二: insert into 表名 values(值1...值n);
-- 不推荐:该方法需要严格按照表结构的字段进行赋值,自增属性字段的值可以用null占位;
-- 方法三: insert into 表名[字段1...字段n] values(值1...值n),(值1...值n)...;
-- 不推荐:一次插入多条数据,通常都是出现在备份sql中
-- 方法四: insert into 表名(字段1...字段n) select 值1,...,值n;
-- 不推荐: 通常是查询的这个表的结果可以插入下一个表的时候使用
13-update 修改数据
-- 语法 : update 表名 set 字段名 = 值 [where 条件];
update `stu` set `name` = 'test' where `id` = 2;
14-delete 删除数据
-- delete 用于删除数据表中的数据
-- 语法: delete from 表名 [where 条件];
delete from `stu` where `id` = 2;
15-select 查询
15.1-select语句结构
-- select 语句中,[]为子查询语句,可根据需要编写,不是强制要写,但是要写一定需要按照顺序写
select */字段名 from 表名
[where 查询条件]
[group by 字段名] [having 分组条件]
[order by 字段名] [asc / desc]
[limit [M,]N]
15.2-select 常用查询
-- 查询列表中的指定字段信息
-- 语法: select 字段名,... from 表名;
select `name`,`age` from `stu`;
-- 查询表中的所有字段信息
-- 语法: select * from 表名; (不推荐,效率低)
select * from `stu`;
-- 根据指定条件查询字段信息
-- 语法: select 字段名,... from 表名 where 条件;
select `name`,`age` from `stu` where `id` = 1;
-- 将查询结果分组
-- 语法: select 字段名,... from 表名 group by 字段名 [having 条件];
select `name`,`age` from `stu` group by `age` having `age` > 18;
-- 将查询结果排序
-- 语法:select 字段名,... from 表名 order by 字段名;
select `name`,`age` from `stu` order by `age`;
-- 控制显示数据条数
-- 语法:select 字段名,... from 表名 order by 字段名 limit 3,5;
-- limit 3,5 表示跳过前3条,显示5条数据,也可直接写 limit 5,表示显示5条数据
select `name`,`age` from `stu` order by `age` limit 3,5;
-- 将查询出来的字段重命名
-- 语法: select 字段名 [as] 重命名 from 表名;
-- as 可写可不写
16-运算符
- 逻辑运算符
- 逻辑与 :AND 或者 &&
- 逻辑或 :OR 或者 ||
- 逻辑非 :NOT 或者 !
- 逻辑异或 :XOR 或者 ^
-- 逻辑与
-- 查找年龄大于20岁并且小于50岁
-- 推荐写法
select `name`,`age` from `stu` where `age` > 20 and age < 50;
-- 不推荐写法
select `name`,`age` from `stu` where `age` > 20 && age < 50;
-- 逻辑或
-- 查找年龄小于20岁并且大于50岁
-- 推荐写法
select `name`,`age` from `stu` where `age` < 20 or age > 50;
-- 不推荐写法
select `name`,`age` from `stu` where `age` < 20 || age > 50;
-- 逻辑或
-- 查找年龄不在20岁到50岁之间的
-- 推荐写法
select `name`,`age` from `stu` where not(`age` > 20 and age < 50);
-- 不推荐写法
select `name`,`age` from `stu` where !(`age` > 20 and age < 50);
-- 逻辑异或
-- 查找id为单数 并且年龄 大于20 不同时成立的数据
-- 推荐写法
select `name`,`age` from `stu` where `age` > 20 xor `id` %2 ==1;
-- 不推荐写法
select `name`,`age` from `stu` where `age` > 20 ^ `id` %2 ==1;
- 关系运算符
- = :判断是否相等
- <=> :判断是否相等,可以判断NULL(不推荐)
- != :判断是否不相等
- <> :判断是否不相等(不推荐)
- is null :是否为空,空为真 (null 判断推荐写法)
- is not null :是否不为空,不为空为真(not null 判断推荐写法)
- between … and : 从哪到哪之间,符合条件为真
- not between … and : 不在哪到哪之间
- like : 匹配字符串,匹配到的结果为真
- not like : 匹配字符串,未匹配到的结果为真
- in : 判断是否在范围内
- not in: 判断是否不再范围内
-- 判断相等写法(推荐)
-- 查找年龄等于18岁的人对应信息
select `name`,`age` from `stu` where `age` = 18;
-- 判断不相等写法(推荐)
-- 查找年龄不等于18岁的人对应信息
select `name`,`age` from `stu` where `age` != 18;
-- 判断为null写法(推荐)
-- 查找姓名为空的人对应信息
select `name`,`age` from `stu` where `name` is null;
-- 判断为not null写法(推荐)
-- 查找姓名不为空的人对应信息
select `name`,`age` from `stu` where `name` is not null;
-- 判断为between ... and 写法(推荐)
-- 查找年龄在 20到50岁之间的人的信息
select `name`,`age` from `stu` where `name` between 20 and 50;
-- 判断为not between ... and 写法(推荐)
-- 查找年龄不在 20到50岁之间的人的信息
select `name`,`age` from `stu` where `name` not between 20 and 50;
-- 判断为like 写法(推荐)
-- 查找名字中包含a的人的信息('%' 不定长通配符)
select `name`,`age` from `stu` where `name` like "%a%";
-- 判断为like 写法(推荐)
-- 查找名字第三个单词是a的人的信息('_' 单个字符通配符)
select `name`,`age` from `stu` where `name` like "__a%";
-- 判断 in
-- 找出性别为 男的信息
select `name`,`age` from `stu` where `sex` in ('m');
-- 判断 in
-- 找出性别不为 男的信息
select `name`,`age` from `stu` where `sex` not in ('m');
- 算术运算符
+、-、*、/、%等,参考基本的算术运算符
17-MySQL常用函数方法
count()
: 计算总数avg()
: 求平均值sum()
: 求和max()
: 找出最大值min()
:找出最小值concat(String str1,String str2)
:连接两个字符串,通常用来连接两个查询结果
-- 查询数据库总数据条数
select count(*) from `stu`;
-- 求学生的平均成绩
select avg(`score`) from `stu`;
-- 求学生的总成绩
select sum(`score`) from `stu`;
-- 求学生的最好成绩
select max(`score`) from `stu`;
-- 求学生最差的成绩
select min(`score`) from `stu`;
-- 将学习最好的成绩和最差的成绩连接显示
select concat((select max(`score`) from `stu`;),
(select min(`score`) from `stu`;) );