测试必会的sql基本操作
1、创建数据库
| 1 |  | 
2、查看全部的数据库
| 1 |  | 
3、查看某个数据库的定义的信息:
| 1 |  | 
4、删除数据库
| 1 |  | 
5、切换数据库:
| 1 |  | 
6、查看正在使用的数据库:
| 1 |  | 
7、查看数据库中的所有表:
| 1 |  | 
8、查看表结构:
| 1 |  | 
9、修改表删除列.
| 1 |  | 
10、修改表名
| 1 |  | 
11、修改表的字符集
| 1 |  | 
12、数据类型与约束
- int :整数 有符号和无符号,长度不受限制
- varchar:字符串 中文/数字/字母都是一字字符,受长度限制
- decimal :小数 decimal(5,2)总长度5位,整数3位,小数2位
- 主键 id :int unsigned primary key auto_increment
- 非空 :not null
- 唯一 :unique
- 默认 :default
SQL中表的操作
1、创建表
| 1 |  | 
2、删除表
| 1 |  | 
3、表数据操作
3.1、添加数据
| 1 |  | 
3.2、修改数据
| 1 |  | 
3.3、删除数据
| 1 |  | 
3.4、查询数据
- 3.4.1、查询表中所有信息- 1 - select * from 表名 where 条件
- 3.4.2、查询表中指定字段- 1 - select 字段1,字段2,字段3...from 表名 where 条件
- 3.4.3、给字段取别名- 1 - select 字段1 (as) 别名,字段2 别名 from 表名 where 条件
- 3.4.4、去重- 1 - select distinct 字段 from 表名 where 条件
- 3.4.5、比较运算符- 1 - select * from 表名 where age>20
- 3.4.6、逻辑运算符- and 满足所有条件 
- or 满足其中任意一个条件 
- not 不满足条件 
 
- 3.4.7、模糊查询- like
- % 0到任意多个字符
- _表示任意一个字符
 
- 3.4.8、范围查询- in 在一个非连续的范围内
- between and 在一个连续的范围内 闭区间 小的数据在前
 
- 3.4.9、空查询- is null
- ‘’空字符串
 
3.5、多表查询
- 3.5.1、排序 - 1 
 2- select * from 表名 where 条件 order by 列1 (asc)|desc,列2 asc|desc
 select * from student where 条件 order by convert(字段 using gbk)asc|desc 纯中文转换后排序
- 3.5.2、聚合函数 - 1 
 2
 3
 4
 5
 6- count: 总数 select count(*/字段)from 表名 where 条件
 max: 最大值 select max(age) from 表名 where 条件
 min: 最小值 select min(age) from 表名 where 条件
 avg: 平均值 select avg(grade) from 表名 where 条件
 sum: 求和 select sum(grade) from 表名 where 条件
 select avg(age)as 平均年龄,max(age)最大年龄,min(age)最小年龄 from 表名 where 条件
- 3.5.3、分组 - 1 - select * from 表名 group by 字段,字段2 having 条件
- 3.5.4、分页 - 1 
 2- select * from 表名 limit 0,5 从第一行数据开始,显示5行
 select * from 表名 limit (n-1)*m,m n代表第几页,m代表每页显示多少条数据
- 3.5.5、等值连接 - 1 
 2
 3
 4- 方式一 :
 select * from 表1,表2,表3 where 表1.列=表2.列 and 表2.列=表3.列 where 条件
 方式二 内连接 取交集:
 select * from 表1 inner join 表2 on 表1.列=表2.列 inner join 表3 on 表2.列=表3.列 where 条件
- 3.5.6、左连接 - 左边的表全显示,右边表能匹配的上的数据连接显示,匹配不上(没有的)以null补充 - 1 - select * from 表1 left join 表2 on 表1.列=表2.列 left join 表3 on 表2.列=表3.列 where 条件
- 3.5.7、右连接 - 右边的表全显示,左边表能匹配上的数据连接显示,匹配不上(没有的)以null补充 - 1 - select * from 表1 right join 表2 on 表1.列=表2.列 right join 表3 on 表2.列=表3.列 where 条件
- 3.5.8、自关联 - 1 - select * from 表 别名1 inner join 表 别名2 on 别名1.aid=别名2.pid
- 3.5.9、子查询- 子查询结果输出的是一行一列 - 1 - select * from student where age>(select avg(age) from student)- 子查询的结果输出的是一列多行 - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11- in:
 select * from student where age in
 (select age from student where age=18)
 any/some: >:大于最小的; <: 小于最大的
 select * from student where age>/< any
 (select age from student where age between 18 and 22)
 all: >:大于最大的; <: 小于最小的
 select * from student where age>/<all
 (select age from student where age between 18 and 22)- 子查询的结果输出的是一行多列 - 1 - select * from student where(name,sex)=(select name,sex from student where sex='男' order by age desc limit 1)- 子查询的输出结果是一个表 - 1 - select * from scores inner join (select cno from courses where cname in('数据库',‘系统测试’) as c on scores.cno=c.cno
3.6、数据分表
| 1 |  | 
 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 GGLSS!
 评论
WalineValine








