首页 > 开发 > 综合 > 正文

数据库系列学习(四)-数据的过滤

2024-07-21 02:47:04
字体:
来源:转载
供稿:网友
数据库系列学习(四)-数据的过滤

1.准备学习的数据库

--创建学生表create table T_Student(    --identity表示主键自增长,从1开始,且每次加1    SId int PRimary key identity(1,1),    SName nvarchar(10),    SGender varchar(2) default('男'),    SAge int)--插入数据--全部列名与值一一对应insert into T_Student(SName,SGender,SAge) values('李三','男',13)--全部列名都赋值,则values前边值可省insert into T_Student values('李四','女',14)--因为SGender有默认值,所以写也有有值insert into T_Student(SName,SAge) values('王五',15)insert into T_Student values('赵六','男',16)insert into T_Student values('Kim','男',17)insert into T_Student values('Lily','女',18)insert into T_Student values('Jerry','女',19)

2.select基本用法

(1)简单的数据检索

image

(2)检索出需要的列

image

(3)给列设别名

image

(4)按条件过滤

image

(5)数据汇总

image

(6)排序

image

3.高级数据过滤

(1)通配符过滤

A:单字符匹配

image

B:多字符匹配

image

C:集合匹配

image

D:使用否定匹配法

image

E:使用通配符过滤虽然方便,但是会对数据库进行全表扫描,所以执行速度非常慢

(2)空值检测

首先插入两条记录先

image

开始查询

image

(3)反义运算符

image

(4)多值检测

image

(5)范围检测

image

(6)低效的“where 1 = 1”

在动态组装sql语句时会用到

缺点:使用“1=1”的过滤条件以后数据库系统就无法使用检索等查询优化策略,数据库系统就会被迫对每行数据进行扫描,即全表扫描


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表