当前位置:Gxlcms > 数据库问题 > sqlserver数据库的sql语句使用

sqlserver数据库的sql语句使用

时间:2021-07-01 10:21:17 帮助过:35人阅读

T-SQL查询语句

1. tansact-SQL编程语言

美国国家标准协会(ANSI)和国际标准组织(ISO)为 SQL定义了标准,微软通过用Transact-SQLANSISQL兼容,Transact-SQL还包含了几种能够增强性能的扩展。

 

T-SQL的组成:

 

 数据定义语言(Date Definition Language)语句简称DDL语句

DDL语句用来建立数据库,数据库对象(create,alter,drop

create object_name

alter object_name

drop object_name

举例说明DDL语句的使用:

use schoolDB

create table teacher

(cust_idint ,company varchar(40),contact varchar(30),phone char(12))

go

alter table teacher add ageint default 30

 数据控制语言(Date Control Language)语句

用于改变某个用户或角色相关的权限

grant

deny

revoke

举例说明:

use schoolDB

grant select on products to public

go

 数据操作语言(Date Manipulation Language)语句

 

操作数据库中的数据。可以更改数据库中的数据或查询数据库中的信息

举例说明:

select*from dbo.TStudent  //查询TStudent表中的数据

 

insert dbo.TStudent (StudentID,Sname,sex)values(‘0000001901‘,陈英宏,)//TStudent表中的studentID,Sname,sex列插入相应的记录

 

update dbo.TStudent set Sname=张春海,sex=where studentid=‘0000000569‘//更新表中studentID0000000569的姓名修改为张春海

delete dbo.TStudent where StudentID=‘0000000020‘//删除studentID0000000020的记录

2. Ttansact-SQL语法要素

1. 批处理

go

一个批处理命令通知SQLServer分析并运行一个批处理的所有指令

实际上不是 Transact-SQL语句,只是描述一个批处理。局部变量作用范围局限在一个批内,必须独占一行。

USE schoolDB

go

select * from dbo.TScore--从那张表查找

where mark+9<60

go

2. 表达式

1. 算数运算符

 + - *  /  %

比较运算符 =   <>  >    =  <

!= :不等于,等同于<>

BETWEEN :指定值得包含范围(包含边界)。使用AND分隔开始值和结束值

IS[NOT]NULL :根据使用的关键字,指定是否搜索空值或非空值。如果有任何一个操作数为NULL,则包含运算符或算数运算符的表达式的计算结果为NULL

LIKE:模糊查询,与指定字符串进行模糊匹配

IN:是否在数据范围里面

字符串联运算符 +  空字符不等于空值

表达式是各种符号和对单个数据进行操作

select mark+5 from dbo.TScore where mark<60

2. 逻辑运算符 and or not

NOT:和其他操作符一起使用,取反的操作

AND:组合两个条件,并在两个条件都为TRUE时取值为TRUE

OR:组合两个条件,并在两个条件之一为TRUE时取值为TRUE

3. 通配符

‘_‘ :表示任何单个字符

sname LIKE ‘_cc‘ 查找以cc结尾的所有三个字母名字

% :任意长度的字符串

sname LIKE ‘%CC%‘查找所有包含cc的名字

[]:括号中所指定范围内的一个字符例如sname LIKE ‘[c-p]ion‘将查找Ion结尾且以介于    c p之间的任何单个字符开始的名字

通配符经常与LIKE关键字一起配合使用完成模糊查询,可以使用LIKE和通配符来完成对表的一些特殊约束。

3. 数据查询

3.1. 使用select语句查询数据

3.1.1. 指定列查询

查询所有行

select * from dbo.TStudent

使用where子句指定行

select Sname,sex,Email from dbo.TStudent where Sname=田育朋

 

3.1.2. 过滤数据

使用比较操作符 =  ><>=   <=  <>

select * from dbo.TScore where mark<=60

使用字符比较符 like

% 0个或多个字符串

_ 任何单个的字符

[]在指定区域或集合内的任何单个字符

[^]不在指定区域或集合内的任何单个字符

select*from dbo.TStudent where sname like%‘

select*from dbo.TStudent where sname like‘_[,]_‘

select*from dbo.TStudent where sname like‘_[^,]_‘

3.1.3. 使用逻辑操作符

OR AND NOT使用方法

select*from dbo.TStudent where Sname like%‘and sex=or StudentID=‘0000000112‘

查找不姓高的学生

select*from dbo.TStudent where Sname not like%‘

3.1.4. 查找在一定范围的值

select*from dbo.TScore where mark between 70 and 80

等价于

select*from dbo.TScore where mark>=70 and mark<=80

不包括70 80

尽量使用between而不使用and和比较操作符表示的表达式

如果想返回不在指定区域的行时,使用not between 。这样会降低数据查询的速度。

select*from dbo.TScore where mark not between 70 and 80

指定时间范围

select * from dbo.TStudent where Birthday between‘1983-01-01‘and‘1984-01-01‘

3.1.5. 查询空值

insert dbo.TStudent (StudentID,Sname,sex)values(‘0000001901‘,陈英宏,)

查找班级不为空的学生

select * from dbo.TStudent where Class is not null

查找班级为空的学生

select * from dbo.TStudent where Class is null

使用is not null来查询指定列中非空的行

3.2. 格式化结果集

3.2.1. 给数据排序

select StudentID,subJectID,mark from dbo.TScore order by 2,3 desc

select StudentID,subJectID,mark from dbo.TScore order by subJectID,mark desc

asc升序

desc降序

3.2.2. 消除重复的行

distinct

select distinct Class from dbo.TStudent

3.2.3. 改变字段的名字

select StudentID as学号,Sname as姓名,sex as性别,cardID as身份证号,Birthday as生日,Email as邮件, Class as专业,enterTime as录入时间from dbo.Tstudent

等价于

select StudentID 学号,Sname 姓名,sex 性别,cardID  身份证号,

Birthday  生日,Email 邮件, Class  专业,enterTime  录入时间from dbo.TStudent

3.2.4. 使用符号

符号可能是字母,数字或标识,在结果集中,他们被用作特定的值,以增加结果集的可读性。

select StudentID学号,Sname姓名,sex性别,性别from dbo.Tstudent

3.2.5. 计算列

年龄是计算列

select StudentID as学号,Sname as姓名,sex as性别,cardID

人气教程排行