SQL Server命令大全

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

select姓名from学生列表//查询姓名列

select学号,姓名from学生列表//查询学号和姓名列

select * from 学生列表//查询所有数据

select count(姓名)from学生列表 //查询姓名列中姓名的数目

select avg(成绩) from 学生列表//查询成绩列中所有成绩的平均值

select sum(成绩)from学生列表//查询成绩总和

create table学生列表(ID BIGINT PRIMARY KEY NOT NULL identity(1,1),学号char(10),姓名char(8),成绩float(4)) //创建含有主键的表,并且让初始值=1,自增+1

create table学生列表(学号BIGINT PRIMARY KEY NOT NULL,姓名char(8),成绩float(4))//可以将学号设为主键,以后的插入操作不能让学号相同

create procedure GetXMByName

@name char(15)=null

as

select学号,姓名from学生列表where姓名=@Name

一:创建表

1.建立一个学生表Student它由学号Sno姓名Sname、性别Ssex、年龄Sage、所在系Sdeot

五个属性组成。其中学号不能为空,值是唯一的,并且姓名取值也唯一。

create table Student(Sno char(5)not null unique,Sname char(20) unique,Ssex char(1),Sage int,Sdept char(15))

二:修改基本表

1.向Student表增加“入学时间”列,其数据类型为日期型。

alter table Student add Scone date

2.将年龄的数据类型改为半字长整数

alter table Student alter column Sage smallint

3.删除学生姓名必须取唯一值的约束

alter table Student drop constraint Sname

三:删除表:

drop table学生

四:建立索引//索引是系统自动使用,来减少查询时间

1.建立索引create index Stusname on Student(Sname)

建立聚集索引

create clustered index Stusname on Student(Sname)

2.删除索引

drop index Stusname on Student

五.查询

一.单表查询

A.选择表中若干列

1. 查询全体学生的学号与姓名

select Sno,Sname from Student

2.查询经过计算的值

查询所有学生的姓名及出生年份select Sname,2012-Sage from Student

3.定义列名的别名

select Sname NAME,'Year of Birth:'BIRTH,2012-Sage BIRTHDAY,LOWER (Sdept)DEPARTMENT from Student

B.选择表中若干行

1. 查询选修了课程的学生学号

select Sno from SC = select all Sno from SC

2. 去掉查询中重复的行

select distinct Sno from SC

3查询计科专业的全体学生名单

select Sname from Student where Sdept='计科'

4.查询所有年龄在20岁以上的学生姓名及其年龄

select Sname,Sage from Student where Sage>20

select Sname,Sage from Student where not Sage<=20

5.查询年龄在20-22岁(包括20和22)之间的学生的姓名

select Sname from Student where Sage Between 20 and 22

6.查询年龄不在20-22岁(包括20和22)之间的学生的姓名

select Sname from Student where Sage not Between 20 and 22

7.查询计科和信息专业的所有学生姓名

select Sname from Student where Sdept in('计科','信息')

8.查询不是计科也不是信息专业的所有学生姓名

select Sname from Student where Sdept not in('计科','信息')

9.字符匹配

a%b 表示任意长度,以a开头以b结尾的字符串。

a_b表示以a开头以b结尾,长度为3的字符串。

如果命名中有_,则可以用 \_ 表示,\% \’等同理。

查询学号为11999051的学生详细情况

select*from Student where Sno like 11999051

查询所有姓靳的学生姓名

select*from Student where Sname like'靳% '

查询姓靳且全名为三个汉字的学生姓名

select*from Student where Sname like'靳__'

查询所有不姓靳的学生姓名

select*from Student where Sname not like'靳%'

查询所有学号以1开头,且倒数第三个数是0的所有学生

select*from Student where Sno like'1%0__'

10.涉及空值的查询

查询学生选修课程后成绩为空的学生学号和相应的课程

select Sno,Cno from SC where Grade is nullv //这里的is不能用”=”替换

11.多重条件查询(AND和OR,AND优先级高于OR,但可以用括号改变优先级)

相关文档
最新文档