mysql的操作语句共分为四大类,分别是:
DDL 数据定义语言 (Data Definition Language) ,例如:建库,建表
DML 数据操纵语言(Data Manipulation Language) ,例如:对表中的数据进行增删改操作
DQL 数据查询语言(Data Query Language) ,例如:对数据进行查询
DCL 数据控制语言(Data Control Language), 例如:对用户的权限进行设置
1.DDL
创建数据库:create database db1;
查询当前所属库:select database();
进入某个库:use 库名;
不存在则创建数据库:create database if not exists db2;
查询数据库并指定字符集:create database db3 default character set gbk;
查询某个库的字符集:show create database db2;
查看当前mysql使用的字符集:show variables like 'character%';
建表:
CREATE TABLE man(
id bigint(20) zerofill auto_increment not null comment '主键ID',
name varchar(255) default null comment '名称',
sex tinyint(4) default null comment '性别',
age tinyint(3) default null comment '年龄',
unique key (id)
)engine=innodb charset=utf8;
create table 新表名 as select * from 旧表名 where 1=2;
create table 新表名 like 旧表名;
查看数据库中的所有表:show tables;
查看表结构:desc 表名
查看创建表的sql语句:show create table 表名;
修改表名:rename table 旧表名 to 新表名;
添加列:alter table person add name varchar(50) comment '姓名' after id;
修改列类型:alter table person modify name varchar(100);
修改列名:alter table person change name person_name varchar(100);
删除列:alter table person drop name;
删除表:drop table person;
判断表是否存在,存在则删除:drop table if exists person;
2.DML
插入表数据:
insert into person (name,age,sex,created_time) values ('张三',20,'男','2022-01-01');
insert into person values ('张三',20,'男','2022-01-01'); //对应所有字段
将一张表的数据复制到另一张表中:
insert into 表名1(字段名1,字段名2) select 字段名1,字段名2 from 表名2;
新建表复制表结构和数据:
create table 表名1 as select 字段名1,字段名2 from 表名2;
一次性插入多条数据:
insert into 表名 (字段名) values (对应值1),(对应值2),(对应值3);
更新数据:
update 表名 set 字段名1=值1 where 字段名=值;
update 表名 set 字段名1=值1,字段名2=值2 where 字段名=值;
删除数据:
delete from 表名 where 字段名=值;
truncate table 表名;
delete from 表名;
drop table 表名;
三者区别:
delete记录删除操作,可以回退,不释放空间,truncate不记录删除操作,不能回退,drop会删除整张表,释放空间,删除速度drop>truncate>delete
3.DQL
where精确条件:
select * from employee where ename='张三';
select * from employee where sex != 1;
select * from employee where sal <> 8000;
select * from employee where sal > 10000;
where模糊条件:
select * from employee where ename like '%三%';
where范围查询:
select * from employee where sal between 5000 and 10000;
where离散查询:
select * from employee where ename in ('张三','李四','王二','周五');
去重:
select distinct(ename) from employee;
聚合函数:
统计:select count(*) from employee;
求和:select sum(sal) from employee;
最大值:select * from employee where sal= (select max(sal) from employee);
平均值:select avg(sal) from employee;
最小值:select * from employee where sal= (select min(sal) from employee);
字符串连接:select concat(ename,' 在 ',dept) as info from employee;
group by(分组):
select dept,count(*) from employee group by dept;
select dept,job,count(*) from employee group by dept,job;
select job,count(*) from employee group by job;
having(对查询的结果进行筛选,一般用于group by之后):
select job,count(*) from employee group by job having job ='文员';
select dept,job,count(*) from employee group by dept,job having count(*)>=2;
select dept,job,count(*) as 总数 from employee group by dept,job having 总数>=2;
order by(排序):
select * from employee order by sal;
select dept,job,count(*) as 总数 from employee group by dept,job having 总数>=2 order by dept desc;
select dept,job,count(*) as 总数 from employee group by dept,job having 总数>=2 order by dept asc;
执行顺序:where —- group by —– having —— order by
limit:
select * from employee limit 4,5; //从第4条开始,取出5条
exists:
如果exists后的内层查询能查出数据,则返回 TRUE 表示存在;为空则返回 FLASE则不存在
select * from dept a where exists (select 1 from employee b where a.dept=b.dept); //查询出公司有员工的部门的详细信息
select * from dept a where not exists (select 1 from employee b where a.dept=b.dept); //查询出公司没有员工的部门的详细信息
左连接(left join)与右连接(right join):
都属于外连接,左连接表示左边的表数据全部显示,右边表只显示符合条件的记录,没有记录用null代替,右连接则相反。
//列出部门员工和这些部门的员工信息
select a.dname,b.* from dept a left join employee b on a.dept=b.dept;
select b.dname,a.* from employee a right join dept b on b.deptnu=a.deptnu;
内连接与联合查询:
内连接:获取两个表中字段匹配关系的记录
eg:查出张三所在部门的地址:
select a.addr from dept a inner join employee b on a.dept=b.dept and b.ename='张三';
select a.addr from dept a,employee b where a.dept=b.dept and b.ename='张三';
联合查询:把多个查询语句的查询结果结合在一起,UNION去除重复,UNION ALL不去重
eg:对销售员的工资从低到高排序,而文员的工资从高到低排序
(select * from employee a where a.job = '销售员' order by a.sal limit 999999 ) union (select * from employee b where b.job = '文员' order by b.sal desc limit 999999);
4.DCL
查看root用户可以在哪台机器登录:select user,host from mysql.user where user='root';
修改mysql库里边的user表:update mysql.user set host='localhost' where user='root';
刷新权限:flush privileges;
修改用户密码:
set password for [email protected] = password('root');
mysqladmin -urootmysqladmin -uroot -proot password;
update mysql.user set authentication_string=password('root') where user='root' and host='localhost';
忘记密码怎么办:
第一步:修改配置文件my.cnf (默认在/etc/my.cnf),在[mysqld]下面加上 skip-grant-tables (跳过权限的意思)
第二步:重启mysql服务
第三步:mysql -uroot -p 无需密码登录进入
第四步:修改密码
限制用户权限:
授权:
grant 权限1,权限2….. on 数据库对象 to '用户'@'host' identified by 'password';
对现有用户进行授权:对现有用户xxx授予所有库所有表所有权限:
grant all privileges on *.* to 'xxx';
对没有的用户进行授权:创建一个新用户XXX授予test库的所有权限,登录密码123456,任何一台主机登录:
grant all privileges on test.* to 'XXX'@'%' identified by '123456';
对没有的用户进行授权:创建一个新用户XXX授予test库的employee表 查与修改权限,登录密码123456,任何一台主机登录:
grant select,update on test.employee to 'XXX'@'%' identified by '123456'
对没有的用户进行授权:对用户XXX授予test库的employee表insert 权限,登录密码123456,任何一台主机登录:
grant insert on test.employee to 'XXX'@'%' identified by '123456';
回收:
回收XXX用户的所有权限(注意:并没有回收它的登录权限):
revoke all privileges on *.* from 'XXX' @ '%';
flush privileges;
回收XXX用户的所有权限(并回收它的登录权限)
delete from mysql.user where user='xxx';
flush privileges;
回收XXX用户对test库的employee的查与修改权限
revoke select,update on test.employee from 'xxx'@'%';
flush privileges;