MariaDB——SQL语句分类汇总
常用SQL语句汇总
SQL语句在所有的关系型数据库中都是通用的,算起来sql语句也是一门语言,只不过这门语言的主要操作对象是关系型的数据库,其中最常用的还是查询相关的语句。
- sql语句主要分为:
- DQL:数据查询语言,用于对数据进行查询,如select
- DML:数据库操作语言,对数据库进行增删改查,如:insert,update,delete
- TPL:事物处理语言,对事物进行处理,包括begin,transaction,commit,rollback
- DCL:数据控制语言,如grant,revoke
- DDL:数据定义语言:进行数据库,表的管理等,如create,drop
- CCL:指针控制语言,通过控制指针完成表的操作,如declare cursor
- sql是一门特殊的语言,专门用来操作关系型数据库
- 不区分大小写
SHOW
show tables;
show global variabels;
show create database testdb;
show databases;
show grants;
show grants for test;
show grants for test@localhost;
show databases;
show create database kkk;
show grants for ken@localhost;
show create table students;
show tables;
show global variabels;
show create database testdb;
show databases;
show grants;
show grants for test;
show grants for test@localhost;
show databases;
show create database kkk;
show grants for ken@localhost;
show create table students;
show global variabels like '%pro%';
show session variabels like '%pro%';
show global variabels like '%buffer_pool%';
show session variabels like '%pro%';
show global variabels like '%buffer_pool%';
show global status;
show session status;
show table status like 'students';
show session status;
show table status like 'students';
show tables;
show table status like 'v_test';
show variabels like '%commit%';
show table status like 'v_test';
show variabels like '%commit%';
show index from students;
show profiles;
show create table classes;
show profiles;
show create table classes;
show index from 表名;
show profiles; #查看sql执行时间
show profiles; #查看sql执行时间
CREATE
create database kkk;
create database aaa character set utf8;
create database aaa character set utf8;
create table class (
id tinyint unsigned primary key not null auto_increment,
name varchar(20)
);
create table students (
id smallint unsigned auto_increment primary key,
name varchar(22),
age tinyint unsigned default 0,
high decimal(5,2),
gender enum('male','female','secret') default 'secret',
cls_id tinyint unsigned
);
create table areas(
aid int primary key auto_increment,
name varchar(22),
pid int
);
create table index(
id int primary key,
name varchar(22),
age int,
key (age)
);
create table test(
id tinyint unsigned primary key auto_increment,
name varchar(20),
high decimal(5,2),
gender enum('male','female','none') defaulte 'none',
birthday datetime
);
create view v_info as select c.name as c_name,s.name as s_name from students as s inner join calsses as c on s.cls_id=c.id;
id tinyint unsigned primary key not null auto_increment,
name varchar(20)
);
create table students (
id smallint unsigned auto_increment primary key,
name varchar(22),
age tinyint unsigned default 0,
high decimal(5,2),
gender enum('male','female','secret') default 'secret',
cls_id tinyint unsigned
);
create table areas(
aid int primary key auto_increment,
name varchar(22),
pid int
);
create table index(
id int primary key,
name varchar(22),
age int,
key (age)
);
create table test(
id tinyint unsigned primary key auto_increment,
name varchar(20),
high decimal(5,2),
gender enum('male','female','none') defaulte 'none',
birthday datetime
);
create view v_info as select c.name as c_name,s.name as s_name from students as s inner join calsses as c on s.cls_id=c.id;
create index age_index on students(age);
create index name_index on test_table(name);
create index name_index on test_table(name);
create view 视图名称 as select语句;
create view v_info as select c.name as c_name,s.name as s_name from students as s inner join calsses as c on s.cls_id=c.id;
--如果两个colume都是name那么会报重复的错误,所以起一个别名。
create index 索引名称 on 表名(字段名)
create index age_index on index(age);
create table test_table (id int primary key auto_increment,name varchar(20),title varchar(20));
create index name_index on test_table(name);
create database aaa;
create database kkk character set utf8;
@LOGIN
#mycat登录
#mycat登录
mysql -h 192.168.254.24 -P8066 -utestuser1 -ptestuser1 (可以执行增删改查)
mysql -h 192.168.254.24 -P9066 -utestuser1 -ptestuser1 (可以查看节点状态)
mysql -h 192.168.254.24 -P9066 -utestuser1 -ptestuser1 (可以查看节点状态)
#client登录
mysql -uroot -proot -h 172.16.2.113 -P3306 -D mysql -e 'show tables;'
mysql -uroot -proot -e "show databases;use mysql;show tables;"
mysql -uroot -proot -e "show databases;use mysql;show tables;"
ALTER
alter database testdb character set utf8;
alter database kkk character set utf8;
alter table students add birth datetime;
alter table students modify birth date;
alter table students change birth birthday date;
alter table students add birthday datetime;
alter table students modify birthday date;
alter table students change birthday birth datatime;
alter table students drop birth;
alter table students add is_delete bit(1);
#bit(2): 00 01 10 11
alter table students add constraint fk_students foreign key (gid) references grade(gid) on delete cascade;
alter table students drop foreign key 外键名称;
alter table students add constraint fk foreign key (cls_id) references classes(id);
alter table students drop foreign key fk;
alter table students add constraint fk foreign key (cls_id) references classes(id) on delete cascade;
alter database testdb character set utf8;
alter database kkk character set utf8;
alter table students add birth datetime;
alter table students modify birth date;
alter table students change birth birthday date;
alter table students add birthday datetime;
alter table students modify birthday date;
alter table students change birthday birth datatime;
alter table students drop birth;
alter table students add is_delete bit(1);
#bit(2): 00 01 10 11
alter table students add constraint fk_students foreign key (gid) references grade(gid) on delete cascade;
alter table students drop foreign key 外键名称;
alter table students add constraint fk foreign key (cls_id) references classes(id);
alter table students drop foreign key fk;
alter table students add constraint fk foreign key (cls_id) references classes(id) on delete cascade;
INSERT
insert into students values (1,'ken',23,170,'male',1);
insert into students values (0,'ken1',23,170,'male',1),(0,'ken2',23,170,'male',1);
insert into students values (1,'ken',23,170,'male',1);
insert into students values (0,'ken1',23,170,'male',1),(0,'ken2',23,170,'male',1);
#如果primary key有自增的特性
insert into students values (0,'pheb',24,176.250,’male‘,1);
insert into students values (default,'pheb',24,176.250,’male‘,1);
insert into students values (null,'pheb',24,176.250,’male‘,1);
--三种情况都是走primary-key的默认值
insert into students values (10,'pheb',24,176.250,’male‘,1);
insert into students values (0,'pheb',24,176.250,’male‘,1);
--这个时候再插入数据的时候,就会默认从11开始
insert into students (name,age,high) values ("joy",23,176.24); #如果有些不能为空的字段没有默认值会报错
insert into students values (default,'pheb',24,176.250,’male‘,1);
insert into students values (null,'pheb',24,176.250,’male‘,1);
--三种情况都是走primary-key的默认值
insert into students values (10,'pheb',24,176.250,’male‘,1);
insert into students values (0,'pheb',24,176.250,’male‘,1);
--这个时候再插入数据的时候,就会默认从11开始
insert into students (name,age,high) values ("joy",23,176.24); #如果有些不能为空的字段没有默认值会报错
DELETE
delete from students where id=1;
delete from students; #会清空这张表
delete from students where name='joy';
truncate table students; #不可恢复的删除
delete from students; #可以恢复
delete 删除自增还在,但是如果使用truncate来删除,自增归零。
delete from classes where id=1; #外键约束,无法删除
--先删除子表再删除父表
delete from students where cls_id=1; #如果有外键,且没有级联删除,那么需要删除所有的子表中的相关内容才能删除主表的
delete from classes where id=1; #发现可以删除
delete from classes where id=2; #删除成功,级联删除
delete from classes where id=1; #发现可以删除
delete from classes where id=2; #删除成功,级联删除
GRANT
grant all on *.* to test@‘%’ identified by '123';
grant all on mysql.testtable to test@‘%’ identified by ‘123’;
grant all privileges on mysql.user to user520@localhost identified by 'user520';
grant create,select,alter on msyql.user to ken@localhost identified by 'ken';
grant all on mysql.testtable to test@‘%’ identified by ‘123’;
grant all privileges on mysql.user to user520@localhost identified by 'user520';
grant create,select,alter on msyql.user to ken@localhost identified by 'ken';
REVOKE
revoke all privileges on *.* from test;
revoke all privileges on msyql.user from user520@localhost;
revoke all privileges on msyql.user from user520@localhost;
UPDATE
update user set password=password('root') where user='root';
update students set is_delete=1 where name='joy';
update students set age=22,class=6 where id=10;
update students set age=100,cls_id=3 where id=10;
update students set age=100,cls_id=3 where id<=10;
update students set cls_id=null where id<=6;
update students set high=null where id=1;
update students set is_delete=1 where name='joy';
update students set age=22,class=6 where id=10;
update students set age=100,cls_id=3 where id=10;
update students set age=100,cls_id=3 where id<=10;
update students set cls_id=null where id<=6;
update students set high=null where id=1;
DROP
drop database aaa;
drop table classes; #删除classes表
drop view v_test;
drop index 索引名称 on 表名;
drop table classes; #删除classes表
drop view v_test;
drop index 索引名称 on 表名;
USE、DESC、SET
use mysql;
use testdb;
desc user; #user表存放用户信息
desc class;
desc students;
desc test_table;
use testdb;
desc user; #user表存放用户信息
desc class;
desc students;
desc test_table;
flush privileges;
set session profiling=1;
set @@global.profiling=1;
set global profiling=0;
set profiling=1; #打开sql语句的执行时间
set session profiling=1;
set @@global.profiling=1;
set global profiling=0;
set profiling=1; #打开sql语句的执行时间
source areas.sql; #将sql语句要插入的内容写到文件中,放到连接数据库时所在的工作目录之中,执行source即可。
#确保已经切换到该表所在的库。
SELECT
--基础查询
select user(); #查看当前的用户
select * from user;
select host,user,password from user;
select now(); #查看当前时间
select * from students;
select id,age,name from students;
select * from students;
select * from students where is_delete=1;
select * from user;
select host,user,password from user;
select now(); #查看当前时间
select * from students;
select id,age,name from students;
select * from students;
select * from students where is_delete=1;
select * from classes;
select * from students;
select * from students;
select name as "姓名",age as “年龄” from students; #只是为了显示阅读的方便,没有更改数据库。
select students.name from students;
select s.name from students as s; #起别名常用于关联查询
select distinct age from students; #消除重复项
--条件查询
select * from students where id < 22;
select * from students where age > 38;
select * from students where age > 18 and age < 30;
select * from students where age <= 23;
select * from students where age >= 18 && age <= 38;
select * from students where age < 18 || age > 33;
select * from students where age between 18 and 28; #头和尾都包含了。
select * from students where age > 18 || high > 170;
select * from students where id < 22;
select * from students where age > 38;
select * from students where age > 18 and age < 30;
select * from students where age <= 23;
select * from students where age >= 18 && age <= 38;
select * from students where age < 18 || age > 33;
select * from students where age between 18 and 28; #头和尾都包含了。
select * from students where age > 18 || high > 170;
--模糊查询
select * from students where name like '%锋'; #%就相当于shell中的*
select * from students where name like '%霆%';
select * from students where name like '__'; #一个下划线表示一个字
select * from students where name like '__%';
select * from students where age=16 or age=22;
select * from students where age in (18,34,55,66,22);
select * from students where age not between 18 and 33;
select * from students where name like '%锋'; #%就相当于shell中的*
select * from students where name like '%霆%';
select * from students where name like '__'; #一个下划线表示一个字
select * from students where name like '__%';
select * from students where age=16 or age=22;
select * from students where age in (18,34,55,66,22);
select * from students where age not between 18 and 33;
--查询空不能使用=null来查询,update中set使用=null可行
select * from students where high is null;
select * from students where high=175 and name="joy";
select * from students where high=175 or name="joy";
select * from students where high is not null;
select * from students where high is null;
select * from students where high=175 and name="joy";
select * from students where high=175 or name="joy";
select * from students where high is not null;
--排序
select * from students order by age; #默认从小到大排列
select * from students order by age desc; #descent 降序排列
select * from students order by age desc,high desc;
select * from students order by age asc;
select * from students order by age; #默认从小到大排列
select * from students order by age desc; #descent 降序排列
select * from students order by age desc,high desc;
select * from students order by age asc;
select * from students where age between 18 and 33 and gender='male' order by age asc;
select * from students where age between 18 and 33 and gender=1 order by age desc;
select * from students where age between 18 and 33 and gender=1 order by age desc;
--聚合函数
select * from students; #查询后会有一个总的行数统计,这个统计是准确的。
--但是这种查询来看总的行数的方式很可能会出现问题,一旦数据量较大,那么会出现卡死的现象,也就是说select * from students实际上会将
所有的内容读到你的内存里,大量的io操作会导致系统崩溃。
所以一般会先看看一张表里有多少行,再进行查询的操作。
--查询总行数
select count(name) from students;
select count(*) from students; #这种统计方式很准确,只要任意一个字段占一行都算。
select count(high) from students; #可能会少几行,如果有null的情况。
select count(*) as ’total‘ from students;
select count(name) from students;
select count(*) from students; #这种统计方式很准确,只要任意一个字段占一行都算。
select count(high) from students; #可能会少几行,如果有null的情况。
select count(*) as ’total‘ from students;
--查询最大
select max(age) from students;
select name,max(age) from students; #这条查询语句发生了错位,姓名和max不匹配
select max(age)as ‘max’ from students; #实际上没有办法用max去查询对应的数据
select max(age) from students;
select name,max(age) from students; #这条查询语句发生了错位,姓名和max不匹配
select max(age)as ‘max’ from students; #实际上没有办法用max去查询对应的数据
select * from students where age=100;
select min(age) from students;
select min(age) from students where gender=2;
select max(age) from students where gender=1;
select max(age) from students where gender=2;
select min(age) from students;
select min(age) from students where gender=2;
select max(age) from students where gender=1;
select max(age) from students where gender=2;
select sum(age) from students;
select sum(age)/count(age) from students;
select sum(age)/count(age) as 'average' from students;
select round(sum(age)/count(age),2) as 'average' from students;
select round(avg(high),2) as 'avg' from students; #使用avg自动剔除了空项
select sum(high) from students;
select sum(age)/count(age) from students;
select sum(age)/count(age) as 'average' from students;
select round(sum(age)/count(age),2) as 'average' from students;
select round(avg(high),2) as 'avg' from students; #使用avg自动剔除了空项
select sum(high) from students;
--分组
select distinct gender from students; #查询不重复的
select gender from students gourp by gender;
select gender,count(*) from students group by gender; #统计各组性别的人数。
select gender,group_concat(name) from students group by gender;
select gender,group_concat(name,age) from students group by gender;
select gender,group_concat(name,'|',age,'|',high) from students group by gender;
select gender,group_concat(name,'|',age,'|',high) from students where gender=1 group by gender;
select gender,group_concat(name,'|',age,'|',high) from students where gender=2 group by gender;
select distinct gender from students; #查询不重复的
select gender from students gourp by gender;
select gender,count(*) from students group by gender; #统计各组性别的人数。
select gender,group_concat(name) from students group by gender;
select gender,group_concat(name,age) from students group by gender;
select gender,group_concat(name,'|',age,'|',high) from students group by gender;
select gender,group_concat(name,'|',age,'|',high) from students where gender=1 group by gender;
select gender,group_concat(name,'|',age,'|',high) from students where gender=2 group by gender;
select gender,group_concat(name) from students group by gender having avg(age)>=40;
select gender,avg(age) from students group by gender having avg(age)>=50;
select gender,avg(age) from students group by gender having avg(age)>=10; #这里的having使用where不符合语法规则。
select gender ,group_concat(name) from students group by gender having count(*)>3;
select gender,avg(age) from students group by gender having avg(age)>=50;
select gender,avg(age) from students group by gender having avg(age)>=10; #这里的having使用where不符合语法规则。
select gender ,group_concat(name) from students group by gender having count(*)>3;
--分页显示
select * from students limit 2;
select * from students limit 0,2;
select * from students limit 2,2; #limit 2 与 limit 2,2 区别在于分页
select * from students limit 4,2;
select * from students limit 6,2; #从6行之后的两行
select * from students limit 2;
select * from students limit 0,2;
select * from students limit 2,2; #limit 2 与 limit 2,2 区别在于分页
select * from students limit 4,2;
select * from students limit 6,2; #从6行之后的两行
select * from students where gender=2 order by high desc limit 0,1;
select * from students where gender=2 order by high desc limit 2,1;
select * from students where gender=2 order by high desc limit 3,1;
select * from students where gender=2 order by high desc limit 2,1;
select * from students where gender=2 order by high desc limit 3,1;
select * from students where high is null;
--自关联查询使用inner join
select * from areas as province inner join areas as city on province.aid=city.pid having name='河北省';
--查询出河北省的所有市
select province.name,city.name from areas as province inner join areas as city on province.aid=city.pid having provine.name='河北省';
select province.name,city.name from areas as province inner join areas as city on province.aid=city.pid having provine.name='河北省';
select * from areas as p inner join areas as c on p.aid=c.pid;
select * from areas as p inner join areas as c on p.aid=c.pid having p.name='北京市';
select * from areas as p inner join areas as c on p.aid=c.pid having p.name='北京市';
--子查询
select aid form areas where name='北京市';
select * from areas where pid=1;
--合并
select * from areas where pid=(select aid from areas where name='北京市');
select aid form areas where name='北京市';
select * from areas where pid=1;
--合并
select * from areas where pid=(select aid from areas where name='北京市');
#可以联合多个inner join,嵌套多层。不过结构可能需要慎重考虑。
--表的关联查询
#路由跟踪tracert www.baidu.com
select * from students;
select * from classes;
select * from students inner join classes on students.cls_id=classes.id;
select students.name,classes.name from students inner join classes on students.cls_id=classes.id;
select classes.name as class,students.name from students inner join classes on students.cls_id=classes.id order by class;
#路由跟踪tracert www.baidu.com
select * from students;
select * from classes;
select * from students inner join classes on students.cls_id=classes.id;
select students.name,classes.name from students inner join classes on students.cls_id=classes.id;
select classes.name as class,students.name from students inner join classes on students.cls_id=classes.id order by class;
--左右关联查询
select * from classes left join students on calsses.id=students.cls_id; #以classes为基准
select * from students left join classes on classes.id=students.cls_id; #以students为基准,没有班级的学生班级为空
--left join 和 right join 相当于调换位置。
select * from students right join classes on classes.id=students.cls_id;
select * from classes left join students on calsses.id=students.cls_id; #以classes为基准
select * from students left join classes on classes.id=students.cls_id; #以students为基准,没有班级的学生班级为空
--left join 和 right join 相当于调换位置。
select * from students right join classes on classes.id=students.cls_id;
--关联查询只是查询,并没有真正将两张表真正关联起来。
--外键:真正将两张表关联起来。
select * from classes inner join students on students.cls_id=classes.id; #使用where也可以,但有时会报错。
select classes.name,students.name from classes inner join students on students.cls_id=classes.id;
select calsses.name,students.name from classes inner join students on students.cls_id=classes.id order by classes.name;
select classes.name,students.name from classes inner join students on students.cls_id=classes.id;
select calsses.name,students.name from classes inner join students on students.cls_id=classes.id order by classes.name;
select classes.name,students.name from classes right join students on classes.id=students.cla_id order by classes.name;
--以students为基准 classes没有的为null
select c.name,s.name from classes as c right join students as s on c.id=s.cls_id order by c.name;
--以students为基准 classes没有的为null
select c.name,s.name from classes as c right join students as s on c.id=s.cls_id order by c.name;
--多个inner join,如果c.id=b.id=a.id 写成:c.id=b.id and b.id=a.id
select classes.name,students.name from students inner join classes on students.cls_id=classes.id;
select * from v_info;
--视图只能查询,无法实现增删改查。
select * from test_table where 'name=name-88888';
--创建索引之后查询速度加快
select * from test_table where 'name=name-88888';
select * from test_table where 'name=name-88888';