mysql学习
[...]表示可省略内容,不影响语句执行
sql语句不区分大小写,均可
sql分类
DDL数据定义,DML数据操作,DQL数据查询。DCL数据控制
控制台mysql服务命令:
windows系统下:
mysql -u root -p 登录mysql
net start mysql 启动mysql
net stop mysql 停止mysql
linux系统下:
systemctl start mysqld 启动mysql
systemctl restart mysqld 重启mysql
systemctl stop mysqld 停止mysql
grep 'temporary password' /var/log/mysqld.lod; 查看自动生成的root用户密码
alter user 'root'@'localhost' identified by '123456'; 修改root用户密码
set global validate_password.policy = 0; 修改密码策略等级
set global validate_password.length = 4; 修改密码长度为4
create user 'root'@'%' identified with mysql_native_password by '123456'; 创建用户
grant all on. to 'root'@'%'; 给root用户分配权限
DDL数据库操作:
show database; 查询所有数据库
select database(); 查询当前数据库
创建数据库:create database [if not exists] 数据库名称 [default charset字符集] [collate 排序规则];
删除数据库: drop database[if exists]数据库名称;
使用数据库:use 数据库名称 ;
表操作-查询:
show tables; 查询当前数据库所有表
desc 表名;查询表结构
show create table 表名;查询指定表的建表语句
表操作-创建:
create table 表名(
字段1 字段1类型[comment 字段1注释],
字段2 字段2类型[comment 字段2注释],
字段3 字段3类型[comment 字段3注释],
...
字段n 字段n类型[comment 字段n注释]
)[comment 表注释];
例如:
表操作的数据类型:
表操作-修改:
alter table 表名 add 字段名 类型(长度)[comment 注释] [约束]; 添加字段
alter table 表名 modify 字段名 新数据类型(长度);修改数据类型
alter table 表名 change 旧字段名 新字段名 类型(长度)[comment 注释] [约束];修改字段名和字段类型
alter table 表名 drop 字段名; 删除字段
alter trable 表名 rename to 新表名; 修改表名
drop table [if exists] 表名;删除表
truncate table 表名;删除指定表,并重新创建该表
DML数据库操作:
DML-添加数据
insert into 表名(字段名1,字段名2,...) values(值1,值2,.....); 给指定字段添加数据
insert into 表名 values(值1,值2,....); 给全部字段添加数据
insert into 表名(字段1,字段2,...) values(值1,值2,...),(值1,值2,...)(值1,值2,...);
insert into 表名 values (值1,值2,...),(值1,值2,...),(值1,值2,...);批量添加数据
DML-修改数据
updata 表名 set 字段名1=值1,字段2=值2,...[where 条件];
DML-删除数据
delete from 表名[where 条件]
DQL数据库操作
DQL-语法
select
字段列表
from
表名列表
where
条件列表
group by
分组字段列表
having
分组后条件列表
order by
排序字段列表
limt
分页参数
DQL-基本查询
select 字段1,字段2,字段3...from 表名;
select * from 表名; 查询多个字段
select 字段1[as 别名1],字段2[as 别名2]...from 表名;设置别名
select distinct 字段列表 from 表名;去除重复记录
DQL-条件查询
语法
select 字段列表 from 表名 where 条件列表;
条件列表:
DQL-聚合函数
select 聚合函数(字段列表) from 表名;
DQL-分组查询
select 字段列表 from 表名 [where 条件] group by 分组字段名[having 分组后过滤条件];
DQL-排序查询
select 字段列表 from 表名 order by 字段1 排序方式1,字段2 排序方式2;
排序方式:asc :升序(默认值) ,desc :降序
DQL-分页查询
select 字段列表 from 表名 limit 起始索引,查询记录;
查询年龄为20,21,22,23岁的女性员工信息:
select * from emp where gender ='女' and age in (20,21,22,23);
查询性别为男,并且年龄都在20-40岁(含)以内的姓名为三个字的员工。
select * from emp where gender='男' and age>=20&&age<=40 and name like '___';
统计员工表中,年龄小于60岁的,男性员工和女性员工的人数.
select gender,count(*)from emp where age <60 group by gender;
查询所有年龄小于等于35岁员工的姓名和年龄,并对查询结果按年龄升序排序,如果年龄相同按照入职时间降序排序。
select name,age from emp where age<=35 order by age asc ,entrydate desc;
查询性别为男,并且年龄在20-40岁(含)以内的前5个员工信息,对查询的结果按年龄升序排序,年龄相同的按照入职时间升序排序
select * from emp where gender = '男' and age between 20 and 40 order by age asc, entrydate desc limit 5;
DQL-执行顺序
DCL数据库操作
DCL-管理用户
use mysql;
select *from user;查询用户
create user '用户名‘@'主机名' identified by '密码'; 创建用户
alter user '用户名‘@'主机名' identified with mysql_native_password by '新密码'; 修改用户密码
drop user '用户名‘@'主机名'; 删除用户
DCL-权限控制
函数
字符串函数
流程函数
约束
外键约束
添加外键:
create table 表名(
字段名 数据类型,
.....
[constraint] [外键名称] foreign key (外键字段名) references 主表(主表列名)
);
alter table 表名 add constraint 外键名称 foreign key (外键字段名) references 主表 (主表列名);
删除外键:
alter table 表名 drop foreign key 外键名称;
删除/更新行为:
alter table 表名 add constraint 外键名称 foreign key (外键字段) references 主表名 (主表字段名) on update (cascade) on delete (cascade);
多表查询
连接查询-内连接
select 字段列表 from 表1,表2 where 条件...; 隐式内连接
selecl 字段列表 from 表1 [inner] join 表2 on 连接条件...; 显式内连接
连接查询-外连接
select 字段列表 from 表1 left [outer] join 表2 on 条件...; 左外连接
相当于查询表1(左表)的所有数据 包含 表1和表2交集部分的数据
select 字段列表 from 表1 right [outer] join 表2 on 条件...; 右外连接
相当于查询表2(右表)的所有数据 包含表1和表2交集部分数据
连接查询-自连接
select 字段列表 from 表A 别名A join 别名B on 条件...;
联合查询-union,union all
select 字段列表 from 表A...
union [all]
select 字段列表 from 表B...;
union all会将全部的数据直接合并到一起,union会对合并之后的数据去重。
子查询
标量子查询
sql语句中嵌套select语句,称为嵌套查询,又称子查询。
select * from t1 where column1 = (select column1 from t2);
子查询外部的语句可以是insert/update/delete/select的任何一个.
常用的操作符:+ <> > >= < <=
列子查询
行子查询
子查询返回的结果是一行(可以是多列),这种子查询称为行子查询.
常用的操作符:= .<> .in .not in
表子查询
子查询返回的结果是多行多列,这种子查询称为表子查询.
常用的操作符:in
事务
开启事务:
start transaction 或 begin
事务的四大特性
并发事务问题
事务隔离级别
个人解读:两边同时对数据库进行操作时,在是否commit操作前后产生的一系列问题
注意:事务的隔离级别越高,数据越安全,但是性能越低。