常用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 global variabels like '%pro%';
    show session variabels like '%pro%';
    show global variabels like '%buffer_pool%';
    show global status;
    show session status;
    show table status like 'students';
    show tables;
    show table status like 'v_test';
    show variabels like '%commit%';
    show index from students;
    show profiles;
    show create table classes;
    show index from 表名;
    show profiles;   #查看sql执行时间
 
CREATE
    create database kkk;
    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;
    create index age_index on students(age);
    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登录
    mysql -h 192.168.254.24 -P8066 -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;"
 
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;
 
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);
    #如果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);  #如果有些不能为空的字段没有默认值会报错
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;   #删除成功,级联删除
 
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';
 
REVOKE
    
    revoke all privileges on *.* from test;
    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;
 
DROP
 
    drop database aaa;
    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;
    flush privileges;
    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 classes;
    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 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 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;  #查询后会有一个总的行数统计,这个统计是准确的。
--但是这种查询来看总的行数的方式很可能会出现问题,一旦数据量较大,那么会出现卡死的现象,也就是说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 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 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 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 * 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 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 * 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 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;
 
--左右关联查询
    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 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;
    --多个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';
 
 
 

MariaDB——SQL语句分类汇总的更多相关文章

  1. SQL语句详细汇总

    SQL语句详细汇总 | 浏览:3061 | 更新:2013-06-10 19:50 一.基础 1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 d ...

  2. 五、mysql中sql语句分类及常用操作

    1.sql语句分类: DQL语句 数据查询语言 select DML语句 数据操作语言 insert delete update DDL语句 数据定义语言 create drop alter TCL语 ...

  3. MySQL(三)之SQL语句分类、基本操作、三大范式

    一.SQL语句的分类     DML(Data Manipulation Langauge,数据操纵/管理语言) (insert,delete,update,select)     DDL(Data ...

  4. Oracle中SQL语句分类

    Oracle中SQL语句分类如下:1.DML语句 insert/delete/update/select/merge/explan plan/lock table2.DDL语句 create/atlt ...

  5. EBS系统管理常用SQL语句整理汇总(参考网上资料&其他人博客)

    --1查找系统用户基本信息 SELECT user_id, user_name, description, employeE_id, person_party_id FROM fnd_user; -- ...

  6. SQL语句详细汇总[转]

    经典SQL语句大全 一.基础 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname3.说明:备份sql serv ...

  7. SQL语句分类

    SQL Structured Query Language SQL是结构化查询语言,是一种用来操作RDBMS的数据库语言,当前关系型数据库都支持使用SQL语言进行操作,也就是说可以通过 SQL 操作 ...

  8. oracle Sql语句分类

    dml语句:数据操作语句[insert,update,delete] ddl语句:数据定义语言[create table,drop table] dql语句:数据查询语句[select] dtl语句: ...

  9. SQL语句分类和语法

    DQL:Data QueryLanguage 数据查询语言 作用:   查询表中的字段 命令:   select         查询 ⑦select 查询列表①from 表1 别名②连接类型 joi ...

随机推荐

  1. Ajax请求状态200,却走error的函数

    已经将近一个月没有正式使用Ajax的请求代码了,参加工作后,这是第一次使用,感觉有些生疏,但基本代码还是记得的,写起来也不费劲,但正因为如此,问题就来了,由于经验不足,本来导致问题的原因不是那个,我却 ...

  2. Linux OS 集群 免密登录

    1. ssh-keygen  生成密钥 2. ssh-copy-id  集群主机名 参考: [图文详解]linux下配置远程免密登录

  3. 【PAT甲级】1052 Linked List Sorting (25 分)

    题意: 输入一个正整数N(<=100000),和一个链表的头结点地址.接着输入N行,每行包括一个结点的地址,结点存放的值(-1e5~1e5),指向下一个结点的地址.地址由五位包含前导零的正整数组 ...

  4. 树莓派4B踩坑指南 - (6)安装常用软件及相关设置

    安装软件 安装LibreOffice中文包 sudo apt-get install libreoffice-l10n-zh-cn sudo reboot 安装codeblocks并汉化: sudo ...

  5. python字符串操作方法详解

      字符串 字符串序列用于表示和存储文本,python中字符串是不可变对象.字符串是一个有序的字符的集合,用于存储和表示基本的文本信息,一对单,双或三引号中间包含的内容称之为字符串.其中三引号可以由多 ...

  6. linux环境安装包方式

    概述 安装有很多种,有时我们会混淆视听不知在什么场景或什么情况下用什么命令,下面讲解下几种安装命令的使用.希望对大家有帮助~ 详解 pip install kuming或 python -m pip ...

  7. 虚拟机下修改ip配置

    // centos ip 配置vim /etc/sysconfig/network-scripts/ifcfg-eth0 // 虚拟机下删除里面的内容vim /etc/udev/rules.d/70- ...

  8. Docker 之registry私有仓库搭建

    Docker 之registry私有仓库搭建 官方提供的私有仓库docker registry用法 https://yeasy.gitbooks.io/docker_practice/reposito ...

  9. Python3.5学习之旅——day1

    本节内容: 1.Python介绍 2.Hello World程序 3.变量\字符编码 4.用户输入 5.if-else语句 6.循环语句 一.Python介绍 Python是一种动态解释性的强类型定义 ...

  10. C、C++ 不得宠,微软正开发新的编程语言!

    76 年迭代与更新,编程语言界,谁主沉浮? 在六百多种编程语言中,对于开发者而言,其主观印象中或只有几种主流的编程语言,如 Java.C.C++.C#.新贵 Python 等,而之所以它们能够成为主流 ...