SQL Server语言关键字
SQL Server语言关键字一、SQL语言基本定义基本类型特殊关键字SQL插入/删除/修改语句create tableinsert into … valuesupdate … set … wheredelete fromdrop tablealter table … addalter table … dropSQL查询语句单关系查询多关系查询附加运算查询聚集函数运算查询综合案例:
一、SQL语言基本定义
基本类型
SQL表中支持很多固有类型,包括:
类型 | 含义 |
---|---|
char(n) | 存放固定长度的字符串,用户指定长度为n。如果没有使用n个长度,则会在末尾添加空格。 |
varchar(n) | 可变长度的字符串,用户指定最大长度为n。char的改进版,大多数情况下我们最好使用varchar。 |
int | 整数类型 |
smallint | 小整数类型(比如年龄、一些小范围的数就可以用) |
numeric(p,d) | 定点数,精度由用户指定。这个数有p位数字(包括一个符号位)d位在小数点右边。 |
real,doubleprecision | 浮点数和双精度浮点数。 |
float(n) | 精度至少为n位的浮点数 |
特殊关键字
SQL中有很多关键字来表达CRUD的操作(增删改查)。在这之外有很多特殊的关键字用来表示一些其他的含义,在总结SQL之前我们有必要了解。
类型 | 含义 | 示例 |
---|---|---|
primary key | 主键,后面括号中作为主键的属性 | primary key(student_id) |
foreign key references | 外键,括号中为外键,references后为外键的表 | foreign key (coures_id) references Course |
not null | 不为空,前面为属性的定义 | name varchar(10) not null |
SQL插入/删除/修改语句
SQL语法使用的最多的就是查询,除了查找语句之外,其他的语句内容都很少。
类型 | 含义 |
---|---|
create table | 创建一张表 |
insert into ……values | 向表中插入一条信息 |
delete from | 从表中删除一条消息 |
update……set……where | 在where的位置,更新内容为set的值 |
drop table | 删除表 |
alter table……add | 向表中添加某个属性 |
alter table……drop | 将表中的某个属性删除 |
create table
--创建一张Student表,表中有4个属性,ID,姓名,年龄和班级,每个属性都有自己的类型。在这张表的主键是IDcreate table Student( ID varchar(20) not null, name varchar(10) not null, age int, class varchar(50) primary key(ID))
insert into … values
--向表中添加一行消息,一个名叫Tom的学生信息,因为age值不是not null,也就不是必须的,所以我们不添加age属性也没问题。insert into Student(ID,name,class) values (10152510302,"Tom","class 1")
update … set … where
--将所有名字叫做Tom的年龄都改成18update Student set age = 18 where name = "Tom"
delete from
--从表中删除所有名字叫Tom的信息。如果delete from不写where查询条件的话会删除整张表的信息,清空整张表。delete from Student where name = "tom"
drop table
--删除Student这张表drop table Student
alter table … add
--向表中添加性别属性,并将表中以存放的信息的sex的值设为null。所有通过这种方式添加的属性都不能被设为not nullalter table Student add sex varchar(5)
alter table … drop
--从表中将class这列属性删除alert table Student drop class
SQL查询语句
SQL最为常用的就是查询,我们使用数据库并从中找出想要的数据,在这个过程中我们可能会编写很多复杂的语句。以下分类总结:
单关系查询
单关系查询就是说只一张表的内容查询有关信息,不涉及其他表,这是查询中作为常见的一种情况我们通过基础的情况来总结一下基本的SQL语法。下面给出涉及到的关键字。
类型 | 含义 |
---|---|
select | 表示要查出表所含有的属性 |
from | 表示要操作的表 |
where | 判断条件,根据判断条件选择信息 |
distinct | 在select后面加入关键字distinct表示将结果去重 |
all | 在select后面加all表示不去重(默认) |
and | 在where中使用and表示将判断条件链接起来 |
or | 在where条件中使用or表示判断条件多选一 |
not | 在where条件中使用not表示判断条件取反 |
示例:
--在Student这张表中查询所有年龄是18岁,并且班级是A的学生的名字,并将结果去重select name distinct from Student where age = 18 and class = "A"
多关系查询
类型 | 含义 |
---|---|
A,B | 在form后面通过逗号连接多张表,表示将这些表进行笛卡尔积运算 |
natural join | 将natural join关键字后面的两张表进行自然连接运算 |
A join B using(c) | 将A和B通过c属性自然连接 |
创建一个数学成绩表,方便多表查询
create table Math( ID varchar(20) not null, name varchar(10) not null, sex varchar(5), scor int, primary key(ID))
示例:
--将Student和Math表自然连接,所得的结果查询出不是B班的男生的成绩select score from Student natural join Math where class<>"B" and sex = "men"
附加运算查询
类型 | 含义 |
---|---|
as | 将as前的关系起个别名,在此语句中可以使用别名代指这个表 |
* | 在select中通过:表名 .* 来查出这个表中的所有属性 |
order by | 让查询结果中的信息按照指定的顺序排序(默认升序,上小下大) |
desc | 在order by之后的属性后使用,表示采用降序排序 |
asc | 在order by之后的属性后使用,表示采用升序排序(默认) |
between | 在where中使用between表示一个数在两个数值之间取值 |
not between | between的反义词,表示一个数在两个数值之外取值 |
union/union all | 将两个SQL语句做并运算,并且自动去重,添加all表示不去重 |
intersect/intersect all | 将两个SQL语句做交运算,并且自动去重,添加all表示不去重 |
except/except all | 将两个SQL语句做差运算,并且自动去重,添加all表示不去重 |
is null | 在where中使用is null表示这是个空值 |
is not null | 在where中使用is not null表示这是不是个空值 |
--将Student和Math两张表做笛卡儿积,结果中所有ID相同的信息,取出他们属于Student属性的部分作为结果。select S.* from Student as S,Math as Mwhere S.ID = M.ID
--在Student和Math自然连接的结果中,找出A班的女生的姓名,并且按照成绩的降序把名字排列出来。select namefrom Student natural join Mathwhere class="A" and sex="women"order by score desc;
--在Math表中,找出成绩在60分到90分之间的学生姓名,并且将姓名按照成绩的升序排列出来。select namefrom Mathwhere score between 60 and 90;order by score asc;
--将上面两个查出来的结果做并集,并且去重。(select namefrom Student natural join Mathwhere class="A" and sex="women"order by score desc)union(select namefrom Mathwhere score between 60 and 90;order by score);
--在Student表中记录的年龄值未填写(为空)的所有人的姓名查出来。select namefrom Studentwhere age is null;
聚集函数运算查询
已经总结了SQL语句的很多关键字,现在我们可任意的进行排序、查找。但是如果我们要获得一些与数据相关的统计信息,但是这些信息在数据库中没有,那么我们要使用SQL中的聚集函数来进行操作。
类型 | 含义 |
---|---|
avg | 平均值 |
min | 最小值 |
max | 最大值 |
sum | 求和 |
count | 计数 |
distinct | 表示将distinct后的属性去重 |
group by | 将在group by 上取值相同的信息分在组里 |
having | 对group by产生的分组进行筛选,可以使用聚集函数 |
例:
--在Student与Math表自然连接的结果中按照班级分组,并且去除那些班级的平均成绩没到60的班级,剩下的班级和该班成绩的平均数(该班成绩的平均数这个属性被重命名为avg_score)作为一张新表被输出出来。select class,avg(score) as avg_scorefrom Student natural join Mathgroup by classhaving avg(score) < 60;
综合案例:
use mastergo--创建数据库语句if exists(select * from sysobjects where name ='ConstructionDB') --查找命令drop DATABASE ConstructionDB --删除 命令--创建数据库Create database ConstructionDBon(name='ConstructionDB_date',filename='D:\ConstructionDB_date.mdf',size=5mb,maxsize=1000mb,filegrowth=5% --增长速度为)log on(name='ConstructionDB_log',filename='D:\ConstructionDB_date.ldf',size=2mb,maxsize=500mb,filegrowth=1mb)use ConstructionDBgo--使用T-SQL语句创建表if object_id(N'stu_tb') is not nullbegin drop table stu_tbend--使用T-SQL语句创建表if object_id(N'class_tb') is not nullbegin drop table class_tbendcreate table class_tb( cid int not null primary key identity(1,1), class_name nvarchar(50))gocreate table stu_tb(id int not null primary key identity(1,1),name nvarchar(20) not null,sex nvarchar(2) not null check (sex in('男','女')) default('男'),cid int not null-- constraint class_stu_id foreign key references class_tb(cid))--1.插入模拟数据insert into class_tb values('1801班')insert into class_tb(class_name) values('1802班')insert into class_tb(class_name) values('1803班'),('1804班')insert into stu_tb(name,cid)values ('小明','1')insert into stu_tb values ('小黑','女','2')insert into stu_tb values ('小白','女','2')insert into stu_tb values ('小红','女','5')insert into stu_tb values ('小蓝','男','3')insert into stu_tb values ('王二','男','4')insert into stu_tb values ('王三','男','4')--2.左连接select * from stu_tb left join class_tb on stu_tb.cid = class_tb.cid--3.右连接select * from stu_tb s right join class_tb c on s.cid = c.cid--4.内连接select * from stu_tb s join class_tb c on s.cid = c.cid--5.全连接select * from stu_tb s full join class_tb c on s.cid = c.cid--6.交叉连接(笛卡尔集)select * from stu_tb s cross join class_tb c--7.条件查询子查询 找出 1801,1802,1803班学生select * from stu_tb where cid in (select cid from class_tb where class_name in ('1801班','1802班','1803班'))--8.分组查询计算各个班级学生的人数select count(id) 人数 ,class_name from stu_tb s , class_tb c where s.cid = c.cid group by c.class_name--9.根据性别统计人数select count(id) ,sex from stu_tb group by sex--10.根据班级ID倒序,学生ID倒序select * from stu_tb order by cid desc,id desc--11.模拟新增1百万条数据到学生表declare @i int set @i = 1while @i<1000000begin insert into stu_tb values ('王三'+convert(nvarchar(20),@i),'男','4')set @i = @i+1end--12.模拟查询select * from stu_tb where name like '%9%'--13.创建索引create index stu_index on stu_tb(name)go--14.创建视图create view stu_view as select name,cid from stu_tbgo--15.创建存储过程create proc stu_proc @name nvarchar(50) ,@sex nvarchar(2),@cid int,@oid int outputasbegin insert stu_tb values(@name,@sex,@cid) select @oid = @cidendgodeclare @oid intexec stu_proc '哈哈','女','1',@oid outputprint @oid--16.事务案例演示begin transaction declare @cid int select @cid = cid from class_tb if @cid =4 begin insert into class_tb values('1110班')end insert into class_tb(cid) values('1110班')print @@error if @@error<>0 begin print '提交事务' commit transactionendelse print '回滚事务' rollback transaction--17.表之间数据迁移 自动生成一张新表select class_name ,cid into newClass from class_tb