一、SQL
DDL
数据定义语言,用来定义数据库对象(数据库,表,字段)
数值类型
- DECIMAL:
范围依赖于精度(M)和标度(D)的值
123.45 精度5,标度2 - DOUBLE:
score double(4,1) (精度,标度)
字符串类型
- char: char(10) 定长字符串 性能好
- varchar:varchar(10) 变长字符串 性能差(需计算所占用空间)
- blob 二进制数据
- text 文本数据
DML
对表中数据进行增删改
增加数据
插入数据时,字符串和日期型数据应在引号中
DQL(单表查询)
基本查询
- 查询多个字段:
select 字段1,字段2,… from 表名;
select * from 表名;(尽量不要使用)
条件查询
<>或!= 不等于
查询空信息
select * from usertable where idcard is not null;
select * from usertable where idcard is null;
等于用一个等号表示
select * from usertable where age=88;
闭区间
select * from usertable where age between 15 and 20;
in可以简化多个或
select *from usertable where age=18 or age=20 or age=40;
select *from usertable where age in(18,20,40);
like 模糊匹配(_匹配单个字符,%匹配任意个字符)
select *from usertable where name like '__';(名字是两位的)
select *from usertable where idcard like '%x';(最后一位是x)
聚合查询
null值不参与聚合函数运算
分组查询
- 分组前过滤用where,分组后过滤用having
排序查询
- 升序关键字:asc(默认)
- 降序关键字:desc
分页查询
#第一页查询
select * from usertable limit 0,10;
#第一页查询起始索引可省略
select * from usertable limit 10;
select * from usertable limit 10,10;
- 分页查询是数据库的方言,不同数据库有不同的实现
执行顺序与编写顺序

DCL
- 管理数据库用户,控制数据库的访问权限
管理用户
- 查询用户
- 创建用户
- 修改用户密码
- 删除用户
权限控制
- 查询权限
- 授予权限
- 撤销权限

浙公网安备 33010602011771号