mysql的操作語句共分為四大類,分別是:
DDL 數(shù)據(jù)定義語言 (Data Definition Language) ,例如:建庫,建表
DML 數(shù)據(jù)操縱語言(Data Manipulation Language) ,例如:對表中的數(shù)據(jù)進(jìn)行增刪改操作
DQL 數(shù)據(jù)查詢語言(Data Query Language) ,例如:對數(shù)據(jù)進(jìn)行查詢
DCL 數(shù)據(jù)控制語言(Data Control Language), 例如:對用戶的權(quán)限進(jìn)行設(shè)置
1.DDL
創(chuàng)建數(shù)據(jù)庫:create database db1;
查詢當(dāng)前所屬庫:select database();
進(jìn)入某個庫:use 庫名;
不存在則創(chuàng)建數(shù)據(jù)庫:create database if not exists db2;
查詢數(shù)據(jù)庫并指定字符集:create database db3 default character set gbk;
查詢某個庫的字符集:show create database db2;
查看當(dāng)前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 舊表名;
查看數(shù)據(jù)庫中的所有表:show tables;
查看表結(jié)構(gòu):desc 表名
查看創(chuàng)建表的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
插入表數(shù)據(jù):
insert into person (name,age,sex,created_time) values ('張三',20,'男','2022-01-01');
insert into person values ('張三',20,'男','2022-01-01'); //對應(yīng)所有字段
將一張表的數(shù)據(jù)復(fù)制到另一張表中:
insert into 表名1(字段名1,字段名2) select 字段名1,字段名2 from 表名2;
新建表復(fù)制表結(jié)構(gòu)和數(shù)據(jù):
create table 表名1 as select 字段名1,字段名2 from 表名2;
一次性插入多條數(shù)據(jù):
insert into 表名 (字段名) values (對應(yīng)值1),(對應(yīng)值2),(對應(yīng)值3);
更新數(shù)據(jù):
update 表名 set 字段名1=值1 where 字段名=值;
update 表名 set 字段名1=值1,字段名2=值2 where 字段名=值;
刪除數(shù)據(jù):
delete from 表名 where 字段名=值;
truncate table 表名;
delete from 表名;
drop table 表名;
三者區(qū)別:
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;
聚合函數(shù):
統(tǒng)計: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(對查詢的結(jié)果進(jìn)行篩選,一般用于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 總數(shù) from employee group by dept,job having 總數(shù)>=2;
order by(排序):
select * from employee order by sal;
select dept,job,count(*) as 總數(shù) from employee group by dept,job having 總數(shù)>=2 order by dept desc;
select dept,job,count(*) as 總數(shù) from employee group by dept,job having 總數(shù)>=2 order by dept asc;
執(zhí)行順序:where —- group by —– having —— order by
limit:
select * from employee limit 4,5; //從第4條開始,取出5條
exists:
如果exists后的內(nèi)層查詢能查出數(shù)據(jù),則返回 TRUE 表示存在;為空則返回 FLASE則不存在
select * from dept a where exists (select 1 from employee b where a.dept=b.dept); //查詢出公司有員工的部門的詳細(xì)信息
select * from dept a where not exists (select 1 from employee b where a.dept=b.dept); //查詢出公司沒有員工的部門的詳細(xì)信息
左連接(left join)與右連接(right join):
都屬于外連接,左連接表示左邊的表數(shù)據(jù)全部顯示,右邊表只顯示符合條件的記錄,沒有記錄用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;
內(nèi)連接與聯(lián)合查詢:
內(nèi)連接:獲取兩個表中字段匹配關(guān)系的記錄
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='張三';
聯(lián)合查詢:把多個查詢語句的查詢結(jié)果結(jié)合在一起,UNION去除重復(fù),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';
刷新權(quán)限: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 (默認(rèn)在/etc/my.cnf),在[mysqld]下面加上 skip-grant-tables (跳過權(quán)限的意思)
第二步:重啟mysql服務(wù)
第三步:mysql -uroot -p 無需密碼登錄進(jìn)入
第四步:修改密碼
限制用戶權(quán)限:
授權(quán):
grant 權(quán)限1,權(quán)限2….. on 數(shù)據(jù)庫對象 to '用戶'@'host' identified by 'password';
對現(xiàn)有用戶進(jìn)行授權(quán):對現(xiàn)有用戶xxx授予所有庫所有表所有權(quán)限:
grant all privileges on *.* to 'xxx';
對沒有的用戶進(jìn)行授權(quán):創(chuàng)建一個新用戶XXX授予test庫的所有權(quán)限,登錄密碼123456,任何一臺主機登錄:
grant all privileges on test.* to 'XXX'@'%' identified by '123456';
對沒有的用戶進(jìn)行授權(quán):創(chuàng)建一個新用戶XXX授予test庫的employee表 查與修改權(quán)限,登錄密碼123456,任何一臺主機登錄:
grant select,update on test.employee to 'XXX'@'%' identified by '123456'
對沒有的用戶進(jìn)行授權(quán):對用戶XXX授予test庫的employee表insert 權(quán)限,登錄密碼123456,任何一臺主機登錄:
grant insert on test.employee to 'XXX'@'%' identified by '123456';
回收:
回收XXX用戶的所有權(quán)限(注意:并沒有回收它的登錄權(quán)限):
revoke all privileges on *.* from 'XXX' @ '%';
flush privileges;
回收XXX用戶的所有權(quán)限(并回收它的登錄權(quán)限)
delete from mysql.user where user='xxx';
flush privileges;
回收XXX用戶對test庫的employee的查與修改權(quán)限
revoke select,update on test.employee from 'xxx'@'%';
flush privileges;