查看,进入,修改,删除
show 查看
show databases
---- 查看所有数据库show tables
---- 查看所有表(需要先use进入数据库内)
use 进入
use 数据库名
---- 连接数据库后,查看所有数据库,然后use选择进入某个数据库
drop 删除
drop database 数据库名
---- 删除数据库drop table 表名
---- 删除表
desc 排列
desc 表名
---- 查看表排列结构
alter 修改
-
添加列
alter table 表名 add 列名 类型; alter table 表名 add 列名 类型 default 默认值; alter table 表名 add 列名 类型 primary key auto_increment;
-
删除列
alter table 表名 drop column 列名;
-
修改列类型
alter table 表名 modify column 列名 类型;
-
修改列 类型+名称+约束
alter table 表名 change 原列名 新列名 新类型 新约束; -- 修改后 如果未设置约束,会删除原来所有约束和默认值
-
修改列默认值
alter table 表名 alter 列名 set default 新默认值;
-
删除列默认值
alter table 表名 alter 列名 drop default;
-
添加主键
alter table 表名 add primary key(列名);
-
删除主键
alter table 表名 drop primary key;
建数据库、表
create 创建
-
database类
-
default charset
---- 默认字符集(指定编码格式,一般utf8) -
collate
---- 排序规则utf8_general_ci
---- 不区分大小写utf8_general_cs
---- 区分大小写utf8_bin
---- 字符串每个字符串用二进制数据编译存储。 区分大小写,而且可以存二进制的内容
-
示例
# 创建一个数据库 creat database 自定义数据库名 default charset utf8 collate utf8_general_ci
-
-
table类
-
数据类型
decimal(整体位数,小数位数)
---- 例:decimal(8,2)
整数最多6位, 小数最多2位, 有多余小数会四舍五入int
---- 整数, 范围(- 2147483648 ~ 2147483647),还有其他的 一般用不到char(n)
---- 字符串(效率高)- 存储空间固定, 不管实际存储,开辟的空间都是n个字符
- n最大为255字符, (注: 是字符)
- 长度不够时自动在后面补空格. 末尾不能有空格会自动删除(前面和中间可以有)
varchar(n)
---- 字符串(效率不高)- 存储空不固定, 根据实际存储决定开辟的空间
- n最大为16128字符, (注: 是字符) (8.0.26版本亲测,4以上的版本应该都是这个)
date
---- 时间, 格式: yyyy-mm-dd, 年月日datetime
---- 时间, 格式: yy-mm-dd hh:mm:ss, 年月日时分秒
-
约束
null
---- 允许为空(默认)not null
---- 不允许为空default
---- 默认值(设置字段时不需要=,空格即可), 插入数据不给某字段数据的时,默认为设置的默认值primary key
---- 主键约束; 不允许为空且不能重复; 一张表只能有一个字段为主键unique
---- 唯一约束; 允许为空但不能重复, 未限制数量,可以设置很多唯一约束auto_increment
---- 自增; 每张表只能有1个自增字段,一般设置给主键- 默认初始值从1开始, 步长为+1
- 可在建表时,添加完字段后,在最后括号外设置
=n
指定从几开始, 也可后期设置初始值和步长,具体的百度吧
default charset
---- 默认字符集(指定编码格式,一般utf8)- 可在建表时,添加完字段后,在最后括号外设置,也可后期单独修改,具体的百度吧
-
注释
comment
---- 注释,给字段添加中文注释
-
示例
-- 格式, []代表可写也可不写 create table 表名( 字段名 数据类型 [约束], 字段名 数据类型 [约束] .... )default charset=utf8; -- 示例 create table user_info( id int primary key auto_increment, -- 主键 (不允许为空且不能重复) 自增 name varchar(16) not null comment '姓名', -- 不允许为空 注释这个字段是存放'姓名'的 email varchar(32) null default '无', -- 允许为空 插入数据时,如果不给email列设置值, 默认为'无' age int default 18 -- 插入数据时, 如果不给age设置值, 默认为18 )default charset=utf8 auto_increment=1001; -- 设置默认字符集=utf8 自增初始值=1001
-
增删改注释
insert into 新增(插入)数据
-
格式
insert into 表名(列名,列名,列名) values(对应列的值,对应列的值,对应列的值);
-
示例
insert into user_info(name,password) values('空心','666'); -- 普通的插入一条数据 insert into user_info(name,password) values('佩奇','666'),('小明','123'); -- 一次插入多行数据 insert into user_info values('苏子豪','666'); -- 插入数据必须按顺序对应所有的列才能这样写
delete from 删除数据
-
格式
delete from 表名; -- 删除表里所有数据 delete from 表名 where 条件; -- 删除指定数据
-
示例
delete from user_info; -- 删除表里所有数据 delete from user_info where name='小明'; -- 删除所有 name=小明 的数据 delete from user_info where id>9; -- 删除所有id>9的数据 delete from user_info where name='佩奇' and password='666'; -- 删除所有 name=小明 同时 password=666 的数据
update 修改数据
-
格式
update 表名 set 列名=值; update 表名 set 列名=值 where 条件;
-
函数
-
concat(列名,要拼接的字符串)
---- 字符串拼接 -
示例
update user_info set name='空心'; -- 把所有name列的值修改为'空心' update user_info set name='空心' where id=1; -- 修改id=1那一行的name值为'空心' update user_info set age=age+1; -- 修改所有age,在原来的基础上+1, 必须是整数 update user_info set age=age+1 where id=2; -- 修改id=2那一行的age,在原来的基础上+1 update user_info set name=concat(name,'66') where id=3; -- 字符串相加; 修改id=3那一行的name,拼接上'66'
查看建表时设置的comment注释
- 格式
show full columns from 表名;
查
准备
- 数据库任意
- 创建两个表
- 添加数据
create table depart(id int auto_increment primary key,
title varchar(16) not null)default charset=utf8;
create table info(id int auto_increment primary key,
name varchar(16) not null,
email varchar(32),
age int,
depart_id int)default charset=utf8;
insert into depart(title) values('开发'),('运营'),('销售');
insert into info(name,email,age,depart_id) values('空心','2448545222@qq.com',18,1);
insert into info(name,email,age,depart_id) values('佩奇','423123@163.com',28,2);
insert into info(name,email,age,depart_id) values('alex','25222@163.com',38,3);
insert into info(name,email,age,depart_id) values('小明','12345522@126.com',28,3);
insert into info(name,email,age,depart_id) values('小心','2448545222@126.com',58,3);
insert into info(name,email,age,depart_id) values('小空','89764322@126.com',18,2);
insert into info(name,email,age,depart_id) values('甲壳虫','645875222@qq.com',38,1);
insert into info(name,email,age,depart_id) values('紫寒','9999922@163.com',8,2);
insert into info(name,email,age,depart_id) values('浩东','666666222@qq.com',88,1);
insert into info(name,email,age,depart_id) values('小花','666666222@qq.com',28,1);
基础查询
select 查询数据
-
格式
# 少用select * 尽量根据需求指定列名 因为数据量大的情况下会很慢 select * from 表名; -- 查看表的所有数据 select 列名,列名,列名 from 表名; -- 查看表里指定列的数据 select 列名,列名 as 别名 from 表名; -- 查看表里指定列的数据, 给某些列显示的时候起个别名 select 列名或*,自定义内容 as 别名 from 表名; -- 查看表中指定的数据,同时再加一列自定的内容显示出来 # 条件查询 select * from 表名 where 条件; -- 在所有数据中搜索符合条件的数据 select * from 表名 where 条件 and 条件; -- 在所有数据中搜索符合条件的数据(and, or, >,<,=,!=)连正则都能用, select * from 表名 where 字段名 between 2 and 4; -- 搜索2和4之间数据(包括2,4),般搜id或者年龄什么的,找区间范围 select * from 表名 where 字段名 in (x,x,x); -- 搜索表里这个字段里等于x,x,x的3个数据 select * from 表名 where 字段名 not in (x,x,x); -- 搜索表里这个字段里不等于x,x,x的数据 # 子查询看案例吧 # 模糊查询 # % 代表通配符,匹配任意字符 select * from 表名 where 字段名 like '%内容%'; select * from 表名 where 字段名 like '%内容'; select * from 表名 where 字段名 like '内%容'; select * from 表名 where 字段名 like '内容%'; # _ 代表一个任意字符,匹配任意字符,下划线可以是多个 select * from 表名 where 字段名 like '_内容_'; select * from 表名 where 字段名 like '_内容'; select * from 表名 where 字段名 like '内___容'; select * from 表名 where 字段名 like '内容___';
-
示例
-
条件查询
select * from info where age > 30;
select * from info where id < 1;
select * from info where id >= 1;
select * from info where id <= 1;
select * from info where id = 1;
select * from info where id != 1;
select * from info where id between 2 and 4;
select * from info where age < 58 and depart_id = 2;
select * from info where age < 10 or depart_id > 1;
select * from info where (age < 10 or depart_id < 3) and name = '空心'; -- ()是优先级, 先找完里面的条件,在继续筛选外面的
select * from info where id in (1,4,6); -- 找表里id=1,4,6的数据
select * from info where id not in (1,4,6);
- 子查询
# 相当于select * from info where id in (1,2,3); 括号里优先执行,得到1,2,3 子查询的列名只能有一个
select * from info where id in (select id from depart);
# exists (select * from depart where id = 5) 判断()里的语句是否成立,如果成立, 执行前面的语句,不成立则不查, == 查询info表里的所有数据, 条件是 如果depart表里id=5存在,就查, 不存在就不查
select * from info where exists (select * from depart where id = 5);
# 跟上面相反
select * from info where not exists (select * from depart where id = 5);
# 先执行()里的, 得到一个临时的表,命名为T, 然后就相当于 select * from T where age < 28;
select * from (select * from info where id > 5) as T where age < 28;;
- 基础连表查询
# 写where条件的时候,字段名前加上表名
select * from depart,info where info.depart_id = 3 and depart.id = 3;
- 通配符查询, 一般用于模糊搜索, 在数据量少的情况下使用, 因为效率慢
select * from info where name like '小%';
select * from info where email like '%@qq.com';
select * from info where name like '_心';
# 可以组合使用
select * from info where email like '%@__.com';
指定列(映射)
# 这段语句== select id,name,1 as nid,3 as mid,email from info;
select
id,
name,
(select min(id) from depart) as nid,-- max/min/sum == 最大的/最小的/求和
(select max(id) from depart) as mid, -- max/min/sum
(select sum(id) from depart) as sid, -- max/min/sum
email
from info;
# == select id,name,(depart里info.depart_id = depart.id的tiele) as '职位', frome info;
# 效率低,用得少
select
id,
name,
(select title from depart where info.depart_id = depart.id) as '职位'
from info;
case when
可以理解为if else
- 格式
select
字段, -- 随意,也可不写
case 字段 -- 如果这个字段, 也可不写字段,直接在when后面写表达式
when 值 -- 等于这个值
then 新值1 -- 更改显示为一个新的值
else 新值2 -- 否则都显示为 另一个新的值
end 别名 -- 结束,这里把as省略了,直接结束后面写别名
- 示例
# 示例一
select
id,
name,
# 如果 depart_id-1 等于 1 表格里显示'民工' end as '描述1' 其他的都会显示null
case depart_id-1 when 1 then '民工' end '描述1',
# 如果 depart_id 等于 1 表格里显示'民工' 否则 都显示 '其他' end as '描述1'
case depart_id when 1 then '民工' else '其他' end as '描述2',
case depart_id -- 如果 depart_id
when 1 then '民工' -- 等于 1 表格里显示'民工'
when 2 then '破销售' -- 等于 2 表格里显示 '破销售'
else '摸鱼的' end '描述3' -- 否则 都显示 '摸鱼的' as '描述3'
from info;
# 示例二
select
id,
name,
case -- 如果
when age<28 then '年轻' -- age<28 显示为'年轻'
when age<50 then '中年' -- 字面意思,不解释了
else '老人' end '身体状况'
from info;
排序
order by 排序
- 格式
select * from 表名 order by 列名 desc; -- 倒序
select * from 表名 order by 列名 asc; -- 正序
- 示例
select * from info order by age desc; -- 根据age列倒序排
select * from info order by age asc; -- 根据age列正序排
# 先根据age正序排序,如果age一样,那么根据id倒叙排序
select * from info order by age asc,id desc;
# 先执行查表操作,最后才排序
select * from info where id>6 or name like '小%' order by age asc;
取部分
limit 取开头
offset 设置获取的起点(不包括起点)
- 示例
select * from info limit 5; -- 根据默认排序取前5行
select * from info order by id desc limit 3; -- 先排序,在获取前3行
select * from info where depart_id=2 order by id desc limit 3; -- 先排序,在获取前3行
select * from info limit 3 offset 2; -- 根据默认排序 从第二行开始 获取前3条数据
- 例如
数据库表中: 1000条数据, 分页查询,每页展示10条数据
select * from info limit 10 offset 0; -- 第一页
select * from info limit 10 offset 10; -- 第二页
select * from info limit 10 offset 20; -- 第三页
....
聚合函数
sum() 求表中某个字段取值的总和
avg() 求表中某个字段取值的平均值
max() 取表中某个字段最大的值
min() 取表中某个字段最小的值
count() 返回某个字段中非null值的行的数目, count(*)包含null
具体用法在其他示例都会用到, 用到的时候在看
分组
group by 分组
- 格式
select 字段1 from info group by 字段1; -- 把相同的值合并到一起
- 示例
select age from info group by age; -- 把相同的值合并到一起
select
age, -- 8 18 28 38 88
max( id ), -- 每个age最大的id
min( id ), -- 每个age最小的id
count( id ),-- 每个age的id合并到一起的行数
sum( id ), -- 每个age的id相加
avg( id ) -- 每个age的id的平均数
from
info
group by
age; -- 把相同的age合并到一起
# 把相同的值合并道一起, count(1)相当于又插入一列值是1的列, 然后返回有几行合并到一起,count()里也可以是字段名,都一样
select age,count(1) from info group by age;
# 查看每个部门有多少个人
select
depart_id, -- 分组合并后的depart_id == 1,2,3
count( depart_id ) -- 每个depart_id合并了几行,一行==1个人
from
info
group by
depart_id;
- 分组后再次对每个聚合函数进行筛选
having 聚合函数的条件判断
# 只看人数大于2的部门
select
depart_id, -- 分组合并后的depart_id == 1,2,3
count( depart_id ) -- 每个depart_id合并了几行,一行==1个人
from
info
group by
depart_id
having count(depart_id) > 2;
- 到目前为止的函数执行顺序
where
group by
having
order by
limit
# 示例
# 把id>2的根据age分组合并,筛选合并后的行数大于1的, 根据age倒叙排序, 取第一行
select age,count(id) from info where id>2 group by age having count(id)>1 order by age desc limit 1;
- 要查询的表 info
- 条件 id>2
- 根据age分组
- 对分组后的数据再根据聚合条件过滤
- 根据age从大到小倒叙排序
- 获取第一条
左右连表
left join 左外连接
right join 右外连接
- 格式
select 字段 from 主表 left outer join 从表 on 主表.x = 从表.x; -- 左外连表, 简写可以把outer去掉
select 字段 from 从表 right outer join 主表 on 主表.x = 从表.x; -- 右外连表, 简写可以把outer去掉
# 可以很多表一块连,就继续在后面添加连表语句就行
select 字段 from 从表 right outer join 主表 on 主表.x = 从表.x left outer join 从表 on 主表.x = 从表.x;;
- 示例
# info为主表,depart为从表的左连接
select info.name,info.age,depart.title from info left join depart on info.depart_id = depart.id;
# info为从表,depart为主表的右连接
select info.name,info.age,depart.title from info right join depart on info.depart_id = depart.id;
- 上面的看似没什么区别,所以为了展示左右连表有什么区别, 在插入一条数据
insert into depart(title) values('运维');
insert into info(name) values('大明');
- 再次执行上方示例的sql语句,会发现 info 为主表的时候, depart表里的运维没有被展示出来,而且显示了自己的 ‘大明’
- depart为主表的时候, 会多出一行其他值为空的’运维’
- 总结: 谁是主表,就以谁的数据为主, 主表的数据都会被展示出来, 从表的数据有关联的才会展示
inner join 内连接
# 格式
select 字段 from 表 inner join 表 on 条件
- 示例
# 不区分主表从表 只要有关联的都显示出来,无关联的不会显示
select info.id,info.age,depart.title from info inner join depart on depart.id=info.depart_id;
- 到目前为止执行顺序为:
join -- 连接
on -- 条件
where -- 条件
group by -- 分组
having -- 聚合条件
order by -- 排序
limit -- 取值
linux 安装mysql
下载mysql的yum安装源
#下载地址
https://dev.mysql.com/downloads/repo/yum/
#下载
[root@nodetwo software]# wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
安装mysql的yum源
[root@nodetwo software]# yum localinstall mysql80-community-release-el7-3.noarch.rpm
修改yum源文件,安装mysql5.x
[root@nodetwo software]# cd /etc/yum.repos.d/
[root@nodetwo yum.repos.d]# vi mysql-community.repo
#修改文件将5.5的改为1 8.0的改为0
进一步确认是否安装的是mysql5
[root@nodetwo ~]# yum repolist enabled | grep "mysql.*-community.*"
mysql-tools-community/x86_64 MySQL Tools Community 120
mysql55-community/x86_64 MySQL 5.5 Community Server 427
安装mysql5.5
yum install mysql-community-server
修改配置文件表明不区分大小写
vi /etc/my.cnf
#增加
lower_case_table_names=1
启动服务;授权root用户远程登录
systemctl start mysqld
use mysql
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '12345' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> delete from user where host!='%';
Query OK, 6 rows affected (0.00 sec)
mysql> select host,user,password from user;
+------+------+-------------------------------------------+
| host | user | password |
+------+------+-------------------------------------------+
| % | root | *1D522BCFD4644BB074EEB0FEDAF434238B0E32C1 |
+------+------+-------------------------------------------+
1 row in set (0.00 sec)
授权root用户远程登录
--登录mysql
mysql -HIP地址【默认是本机】 -uroot -p
--输入密码
-- 查看所有的数据库
show databases; database,database,database
--创建数据库
create database bd112<名字>;
-- 切换,进入,选择数据库
use bd112;
-- 查看数据库当中所有的表
show tables;
-- 删除数据库
drop database bd112;
--字段类型
--数值类型字段
--tinyint(0~128)
--int (65536)
--double(小数)
--float
--字符串类型
--char (固定长度的字符串,如果给的不够那么上下的默认空格代替)
-- varchar (可变长度的字符串)
-- text (文本)
-- longtext (大文本)
-- 时间类型
-- date (年,月,日)
-- datetime (年,月,日,时,分,秒)
-- datetamps(时间戳)
-- 创建表的命令
create table `user` (
user_id int(4),
user_name varchar(20),
user_age int(3),
user_sex tinyint(1),
user_address varchar(200)
);
-- 查看表结构
desc `user`;
-- 对表结构进行操作的命令
--给表添加一个新的字段
alter table `user` add user_phone char(11);
--修改表字段的类型
alter table `user` modify user_sex char(1);
-- 修改字段名称
alter table `user` change user_age u_age int(3);
-- 删除表字段
alter table `user` drop u_age;
-- 删除表
drop table `user`;
-----------------------------------
-- 给表添加【插入】数据
-- 单行添加
insert into `user` (user_name, user_id, user_sex, user_age, user_address, user_phone)
values ('尼古拉斯·张正',1001,1,18,'莲花乡','12345678910');
insert into user values (1001,'法外狂徒·格雷福斯',30,1,'比尔吉沃特','1452141248');
--多行添加
insert into `user` values
(1003,'原始人',18,1,'32156498721','你猜'),
(1004,'小老弟儿',20,1,'3218654781','不知道');
--查看表中所有的记录
select * from `user`;
-- 修改表数据
-- 修改表数据的过程,一定要有条件约束,否则所有的数据都会被修改
update `user` set user_id = 1002;
update `user` set user_id = 1001 where user_name = '尼古拉斯·张正';
--删除表中的数据
delete from `user`;
delete from `user` where user_id = 1004;