【SqlServer】SqlServer基本语法

首选,sql语句不区分大小写

 

数据库操作

创建数据库

create database School

使用数据库

use School

删除数据库

drop database School

如果存在,则删除数据库

if(exists(select * from master.dbo.sysdatabases where dbid = DB_ID('School')))
begin
    use master
  alter database School
  SET single_user
  with rollback immediate 
  drop database School
end

用户模式
MULTI_USER 多用户模式,任何用户都可以连接
SINGLE_USER 单用户模式,数据库一次只能有一个连接,当维护数据库时启动
RESTRICTED_USER 限制模式,

回滚 ROLLBACK,即是在执行操作过程中,如果出现错误,则所有的操作都取消,
比如在添加数据操作时,共要插入10条数据,当在第3条数据时插入出错,这是就需要使用回滚,即出错时全部操作都取消插入

 

查询所有数据库

select * from sysdatabases

查询所有表

select * from sysobjects where xtype = 'U'

xtype = 'U'表示用户表,xtype = 'S'表示系统表


表操作


创建表

create table Student(--学生表
    ID int identity(1000,1) primary key,--ID
    Name nvarchar(64) default 'Name',--姓名
    Grade int, --年级
    Class int, --班级
    Score int) --成绩

identity(1000,1) 表示是否标识,即是否自增,1000标识自增的初值,1表示增量
primary key 表示主键

删除表

drop table Student

复制表数据,Student2不存在

select * into Student2 from Student

复制表数据,Student2存在

insert into Student2(Name,Score) select Name,Score from Student

增加列

alter table Student add Description int

为列添加说明

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'ID' , @level0type=N'SCHEMA',@level0name=N'dbo',
 @level1type=N'TABLE',@level1name=N'Student', @level2type=N'COLUMN',@level2name=N'ID'

---->>关于sp_addextendedproperty

插入数据
一次插入一条数据

insert into Student(Name,Grade,Class,Score) values('Jack',1,3,90)

一次插入多条数据

insert into Student(Name,Grade,Class,Score) values
('Tom',2,2,80),
('Jane',3,2,85),
('Mary',2,1,95),
('Allen',2,2,70)

删除全部数据

delete from Student

删除年级为1的学生

delete from Student where Grade = 1

更新表

update Student set Grade = 3 where Name ='Tom'

查询语句

select * from Student

查询前几条记录top

select top 3 * from Student

查询不重复的记录,比如查询各个年级

select distinct Grade from Student

模糊查询

select * from Student where Name like '%J%'

% 表示0个或多个字符          Name like 'J%'查询以J开头的数据
_ 表示一个字符                    Name like '_ack'查询以任意字符开头的第二字符开始包含ack的数据    
Name like '[ABC]%'             查询以ABC开头的数据
Name not like '[ABC]%'       查询不以ABC开头的数据
Name like '[A-J]%'               查询以A到J开头的数据
Name between 'A' and 'J'   查询以A到J开头的数据(包含A但不包含J的数据)

指定查询范围

select * from Student where Score >= 80 and Score <= 90

select * from Student where Score between 80 and 90

查询指定包含内容

select * from Student where Score in (80,90)

设置别名

select Name as StudentName from Student 

空值判断is null,非空判断is not null

select * from Student where Description is not null

排序 升序

select * from Student order by Grade,Class desc

排序 降序 (排序的每个字段都需要加desc)

select * from Student order by Grade desc,Class desc

having ,由于where无法与聚合函数一起使用,所以使用having

select Name,max(Score) as Score from Student 
group by Name having max(Score) > 80

注意,查询的列字段,必须在group by中,比如要查询Name,必须有group by Name

合并查询结果

select * from Student where Grade = 1
union
select * from Student where Grade = 2

union 不显示重复
union all显示重复

成绩表

create table Score
(ID int identity(1,1),
StudentID int,
CourseID int,
Score float)

删除表

drop table Score

插入数据

insert into Score(StudentID,CourseID,Score) values
(1000,1,90),(1001,1,80),(1002,1,85),(1003,1,88),
(1000,2,99),(1001,2,92),(1010,2,95)

设置外键,将StudentID设置为Student的外键   

alter table Score add constraint FK_StudentID foreign key (StudentID) references Student(ID)

---->>关于约束

join

select *
from Student inner join Score 
on Student.ID = Score.StudentID

inner join 内联接,表中至少一个匹配,返回行(左右表的交集)
left join 左联接,即使右表没有匹配,也从左表返回所有行(左表的完全集)
right join 右联接,即使左表没有匹配,也从右表返回所有行(右表的完全集)
full join 只要其中一个表存在匹配,则返回行(左右表的并集)

 

---->>关于存储过程

 

函数

查询数据总数

select Count(*) from Student

求和

select sum(Score) from Student

求平均值

select avg(Score) from Student

求最大值

select max(Score) from Student

求最小值

select min(Score) from Student

查询最后一个插入的Identity值,比如字段ID设置为自增后插入数据,在插入语句中ID是不用指明的,想获取ID需再次查询

select scope_Identity() 

查询插入数据Rose的ID

insert into Student(Name,Grade,Class,Score) values('Rose',3,1,90)
select scope_Identity()

把字段转成大写upper

select upper(Name) from Student 

把字段转成小写lower

select lower(Name) from Student 

截取字符串substring

select substring(Name,1,1) from Student

查询字符串长度len

select len(Name) from Student

查询时间

select getdate()

条件语句

if else

declare @b bit
set @b = 1
if(@b = 1)
begin 
    print '1'
end
else
begin
    print '0'
end

输出“1”

when then

declare @stateID int
declare @state nvarchar(10)
set @stateID = 404
set @state =case
when @stateID=200 then '成功'
when @stateID=404 then '未找到'
when @stateID=503 then '服务不可用'
end
print @state

输出“未找到”

循环语句

while

declare @i int
declare @sum int

set @i = 0
set @sum = 0

while @i<10 
begin 
    set @sum += @i
    set @i +=1
end

print @sum

输出“45”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GreAmbWang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值