MySQL数据库
类型:关系型数据库
优势:
-
复杂的查询:可以使用SQL语句在一个或者多个表之间进行复杂的查询
-
事务支持:可以提高安全性能
一. 进入MySQL数据库
mysql -h主机名 -u用户名 -p 输入密码
简写:
mysql -u root -p 输入密码
二. 介绍数据库
MySQL->小数据库->数据表->字段->数据
三. 对于数据库的操作
命令:库和表: create 增加(创建) drop删除 alter(改) show查
1、库
查看所有数据库 show databases;
进入数据库 use 库名
查看当前所在的数据库 select database();
创建数据库 create database mysql;
创建一个不存在的数据库 create database if not exists mysql;
删除数据库 drop database 库名;
drop database if exists 库名; # 如果不存在, 防止报错
2、表
查看表 show tables;
查看表结构 desc 表名;
创建一个表b和a一样 create table b like a;
创建表
create table 表名(
字段名1 类型 约束条件,
字段名2 类型 约束条件,
…
);
删除表 drop table 表名;
字段类型
(1) 数值类型
类型 | 大小 | 范围(有符号) | 范围(无符号) | 用途 |
---|---|---|---|---|
tinyint | 1字节 | -128~127 | 0~255 | 最小整数值(年龄,状态) |
smallint | 2字节 | -32768-32767 | 0-65535 | 大的整数值 |
int | 4字节 | 0~到4开头的十位 | 大的整数值 | |
float | 4字节 | 单精度浮点型 | ||
double | 8字节 | 双精度浮点型 | ||
decimal | 更加精准的小数类型 |
(2) 日期和时间类型
类型 | 大小 | 范围 | 格式 | 用途 |
---|---|---|---|---|
date | 3个字节 | 1000-01-01~9999-12-31 | YYYY-MM-DD | 存储日期值 |
time | 3个字节 | -838:59:59 ~838:59:59 | HH:MM:SS | 存储时间值 |
year | 1个字节 | 1901~2155 | YYYY | 存储年份值 |
datatime | 8个字节 | 1000-01-01 00:00:00~9999-12-31 23:59:59 | YYYY-MM-DD HH:MM:SS | 混合日期 |
(3) 字符串类型
类型 | 大小 | 用途 |
---|---|---|
char | 0-255字节 | 存储定长字符串 |
varchar | 0-255字节 | 存储变长字符串 |
text | 0-65535字节 | 长文本数据 |
enum('w','m') | 65535个成员 | 枚举:可赋予某个枚举成员来存储 |
set('w','m') | 64个成员 | 集合: 可赋予多个集合成员,用逗号隔开 |
create table if not exists mystring( -- 创建表
username varchar(10),
password char(6),
article text,
sex enum('w','m'),
age int,
num float
);
四. 数据的增删改查
增加: insert into 删除: delete from 修改: update 查询: select
1) insert 添加
指定字段添加值
insert into 表名(字段名1,字段名2,..) values(值1,值2,..)
insert into mystring(username,id,name) values('dsadas',21312312,'312321da');
不指定字段添加值(有多少字段 就要添加多少个值 一一对应)
insert into 表名 values(值1,值2,..)
insert into mystring values('sdsadas',1312312,'212321da');
添加多个值
不指定字段添加多个值
insert into 表名 values(值1,值2,..), (值1,值2,..),..
insert into mystring values('b',52,'b52'),('c',62,'c62');
指定字段添加多个值
insert into 表名(字段名1,字段2,..) values(值1,值2,..),(值1,值2,..),…
insert into mystring(username,id,name) values('2b',52,'b52'),('3c',62,'c62');
2) select 查询
主体结构:
select 字段 from 表名 [where条件][group by having][order by]
不指定字段查询
select * from 表名;
指定字段查询
select 字段1,字段2,.. from 表名;
给查询字段起别名
select 字段名 别名, 字段名 别名 from 表名;
select 字段名 as 别名, 字段名 as 别名 from 表名;
3) delete 删除
主体结构:
delete from 表名 [where]
注意:
where 如果不加 则删除所有数据
4) update 修改
主体结构:
update 表名 set 字段名=值 [, 字段名=值,..][where]
注意:
where 如果不加 会修改一列的数据
where条件:(1-6为基础)
(1) 比较运算符 >、<、>=、<=、!或<>、=
1、> 大于 2、< 小于 3、>= 大于 4、<= 小于 5、! 或<> 不等于 6、= 等于
例:select * from a where id != 1;
(2) 逻辑运算符
1、and 逻辑与(并且) 2、or 逻辑或 3、between and 在…之间 包含值的本身
4、not between and 不在..之间 5、in 在..里 6、not in 不在..里
select * from a where id between 3 and 9 and id<3 or id >9;
select * from a where id in(1,2,10) and id not in (5,8,12);
(3) 子查询sql 的条件 还是一条sql语句
select * from a where id in (select id from user where password =123456);
(4) oder by排序
order by 字段名 asc/desc(升序/降序)
select * from a order by id desc; -- 按照id降序
select * from a order by id asc; -- 按照id升序
select * from a where age > 72 order by age;
注意:不加asc 默认升序
order by 要放在所有数据都处理完毕 再将数据排序显示
(5) is 或 not is
因为null是一个特殊的值不能使用比较运算符操作
select * from a where username is null;
select * from a where username is not null;
(6) limit 取值
limit x,y 即从x+1的位置取y条数据
limit y 从索引0的位置取出y条数据
select * from a order by age desc limit 0,2; -- 0表示从0开始第一条数据 2 表示查询两条即 1,2条数据
select * from a order by age desc limit 2;
select * from a where age between 112 and 255 and username is not null order by id desc limit 1;
(7) MySQL 聚合函数
count(字段名) 统计个数
select count(*) from a;
max(字段名) 最大值
select max(id) from a;
min(字段名) 最小值
sum(字段名) 求和
avg(字段名) 平均数
select count(*) as con,max(age) as mage,min(age) as minage,sum(age) sumage,avg(age) as avgage from a;
(8) group by 分组
select classid,count(*) as con from a group by classid;
select sex,count(*) as con from a group by sex;
select sex,count(*) as con from a group by sex order by con desc;
select classid,sex,count(*) from a group by classid,sex;
having 条件:
select classid,sex,count(*) as con from a group by classid,sex having con>1; # 按照班级和性别划分 查询人数>1
select classid,sex,count(*) as con from a group by classid,sex having con>1 and sex='w'; # 查询 人数>1且性别为w
select classid,sex,count(*) as con from a group by classid,sex having classid in('python1708');
(9) like 模糊查询
'%value%' 值包含就查询
select * from a where username like '%四%'
'value%' 以value值作为开头的数据
select * from a where username like ‘四%';
'%value' 以value值作为结尾的数据
select * from a where username like ‘%张';
select * from a where username like '%张%' and age>72 order by age desc limit 2;
(10) distinct 去除重复数据
select distinct age from a;
select age from a group by age;