SQL知识点

关系型数据库:MySql
非关系型数据库:Redis(以键值对的方式)

SQL分几类:
DDL:数据定义语言:create、alter、drop、truncate
DML:数据操作语言:insert、delete、update
DQL:数据查询语言:select from where
DCL:数据控制语言:grant revoke DBA

查询所有的数据库:show databases
(1)数据库的CRUD: 1、创建数据库:create database demo;
2、删除数据库: drop database demo;
3、查询数据库创建语句:show create database demo;
4、修改数据库字符集: alter database 数据库名 character set 字符集


列的类型:在java:int String Boolean Date
在mysql: int char/varchar(长度) Boolean date time datatime timestamp text blob

(2)表的CRUD: 1、查询所有的表:show tables;
2、创建表:create table hello{列名1 int ,列名2...};
3、删除表:drop table 表名;
4、查询表的创建过程:show create table 表名;
5、查询表的创建结构:desc 表名;

6、修改表:添加列(add)、删除列(drop)、 修改列(modify)、修改列名(change)、修改表名(rename)
--添加手机号
alter table emp add phone varchar(11);

修改列(modify) alter table 表名 modify 列名 列的类型
-- 修改手机号的类型
alter table emp modify phone int;

修改列名(change) alter table 表名 change 旧的列名 新列名 列的类型
--- 修改phone ---> mobile
alter table emp change phone mobile int;

删除列(drop) alter table 表名 drop 列名;
-- 删除mobile
alter table emp drop mobile;

修改表名(rename) rename table 旧表名 to 新表名
rename table emp to newemp;

修改表的字符集 alter table 表名 character set 字符集
alter table emp character set gbk;

(3)对表中数据的CRUD:
查询语句的基本结构: select ... from ....
select 要显示的列名 from 从哪张表里面去查询数据 [where 条件:查询的是哪条记录/满足哪个条件]

--简单查询:
---查询所有商品信息:
select * from product;
---查询商品名称和商品价格:
select pname,price from product;

---别名查询. as 的关键字 , as 关键字是可以省略
--列别名:
select pname as 商品名称, price as 商品价格 from product;

select pname as "商品名称", price as 商品价格 from product;
select pname 商品名称, price 商品价格 from product;
--表别名: 多表查询里面使用

---去掉重复的值 distinct
--查询所有商品的名称
select distinct pname from product;

-- 多列去除重复的, 必须是每一列都一样才算重复
select pdesc,cno from product;
select distinct pdesc,cno from product;
-- select运算查询 : 仅仅在查询结果上做了运算 + - * /
select 1+1;
select 3/4;

-- 查询商品的折后价格
select price*0.6 from product;

--条件查询 [where关键字] : 查询表中符合条件的数据
-- 关系/比较运算符: > >= = < <= != <>
!= : SQL Server
<> : 标准SQL
-- 逻辑运算符: and or not
-- 其它运算符:
is null : 是否为空
is not null : 是否不为空
between...and ...: 在某个区间内: 闭区间[10,20] 开区间(10,20) [) (]
in(集合) : 在集合内

-- 查询商品价格>60元的所有商品信息
select * from product where price > 60;

-- where 后的条件写法
--查询商品价格不等于99的所有商品
select * from product where price <> 99;
select * from product where price != 99;
--查询出商品价格不为null的所有商品
select * from product where price != null;
-- null值 : 不确定的,不可预知的内容
select * from product where price is not null;

-- 查询商品价格在10 到 100之间
select * from product where price >=10 and price <=100;
select * from product where price between 10 and 100;
--查询出商品价格 小于35 或者商品价格 大于900
select * from product where price <35 or price>900;

--like: 模糊查询
_ : 匹配单个字符
% : 匹配的任意个数字符

-- 查询出名字中带有辣的所有商品
select * from product where pname like '%辣%';

-- 查询出名字第三个字是辣字的所有商品信息
select * from product where pname like '__辣%';
-- 查询商品名称中包含%的商品信息
insert into product values(null,'黄%鹤楼',null,'饭后一根烟,胜做活神仙',3);

select * from product where pname like'%\%%';
select * from product where pname like'%A%%' escape 'A';

-- in 判断值是否在某个集合/范围
--查询出商品分类ID在 1,3,4里面的所有商品
select * from product where cno in(1,3,4);

--排序查询: order by 关键字/列名 排序规则
asc : 升序 ascend
desc: 降序 descend

--0. 查询所有商品,按照价格进行排序
select * from product order by price asc;


--1.查询所有的商品,按价格进行降序排序 (asc-升序 desc-降序);
select * from product order by price desc;
--2.查询名称有 辣 的商品,按价格降序排序
select * from product where pname like '%辣%' order by price desc;

-- 聚合函数: 统计数据
sum() 求和, avg() 平均值 max()最大值 min()最小值 count()总数
--1.获得所有商品价格的总和:
select sum(price) from product;
--2.获得所有商品的平均价格:
select avg(price) from product;

--3.获得所有商品的个数:
select count(*) from product;

--分组: group by
本质上是先将表中数组,按照分组的条件进行排序,排完序之后,再做统计操作
select 分组的条件,分组之后的操作 from 表名 group by 分组条件 [having 条件过滤];

-- select后控制的是要显示的内容, 只写分组的条件和分组之后的操作

--1.根据cno字段分组,分组后统计商品的个数
select cno,count(*) from product group by cno;


--分组之后的条件过滤: having
--2.根据cno分组,分组统计每组商品的平均价格 并且商品平均价格 > 60
select cno,avg(price) from product group by cno;
select cno,avg(price) from product group by cno having avg(price)>60;


where 和 having的区别
where 分组之前执行 ,不能接聚合函数
having 分组之后执行 ,可以接聚合函数

SQL编写顺序:
select 要查询的列 from 表名 where 条件过滤 group by 分组条件 having 分组之后的过滤 order by 列名;

SQL执行顺序:

上一篇:poj.1988.Cube Stacking(并查集)


下一篇:python multiprocessing 和tcp