编程语言:SQL Server数据库使用教程,SQL Server增删改查语句

「作者主页」:士别三日wyx
「作者简介」:CSDN top100、阿里云博客专家、华为云享专家、网络安全领域优质创作者
「推荐专栏」:对网络安全感兴趣的小伙伴可以关注专栏《网络安全自学教程》

SQL Server是微软提供的一种关系型数据库,语法跟MySQL类似,这篇文章主要是熟悉SQL Server的语句格式,会脱裤、在告警流量中能认出来即可。

SQL Server

1、表操作

1.1、创建表

bash 复制代码
create table teacher(
    id int primary key,
    name varchar(10) not null,
    age int
    )

1.2、删除表

bash 复制代码
drop table teacher;

1.3、修改表

bash 复制代码
alter table teacher		-- 添加字段
add name varchar(10) not null;

alter table teacher		-- 删除字段
drop column name;

exec sp_rename 'teacher.name','newname','COLUMN';	-- 修改字段

alter table teacher		-- 修改字段类型
alter column name varchar(10) not null;

2、数据操作

2.1、新增

bash 复制代码
insert into test.dbo.users (id,username,password)
values(1,'lisi',123),(2,'lisi',123);

insert into test.dbo.users (id,username,password)	-- 将查询结果插入
select * from test.dbo.users;

2.2、删除

bash 复制代码
delete test.dbo.users where id=1

2.3、修改

bash 复制代码
update test.dbo.users set username='aaa' where id=1;

2.4、查询

bash 复制代码
select * from test.dbo.users    -- 普通条件查询
where id=1;

2.4.1、模糊查询

bash 复制代码
select * from test.dbo.users
where username like '%li%';

2.4.2、范围查询

bash 复制代码
select * from test.dbo.users	-- id在1~3之间的数据
where id between 1 and 3;

select * from test.dbo.users	-- id在1~3以外的数据
where id not between 1 and 3;

2.4.3、子查询

bash 复制代码
select * from test.dbo.users	-- id为1或2或3的数据
where id in(1,2,3);

select * from test.dbo.users	-- id不是1或2或3的数据
where id not in(1,2,3);

2.4.4、排序

bash 复制代码
select * from test.dbo.users	-- 从小到大排序
order by id asc;

select * from test.dbo.users	-- 从大到小排序
order by id desc;

2.4.5、去重

bash 复制代码
select distinct * from test.dbo.users;	-- 去重

2.4.6、前n行

bash 复制代码
select top 3 * from test.dbo.users;		-- 前n行

相关推荐
Hello.Reader5 小时前
Redis 延迟监控深度指南
数据库·redis·缓存
ybq195133454315 小时前
Redis-主从复制-分布式系统
java·数据库·redis
码农12138号5 小时前
BUUCTF在线评测-练习场-WebCTF习题[GXYCTF2019]BabyUpload1-flag获取、解析
web安全·网络安全·文件上传漏洞·buuctf·解析漏洞
Johny_Zhao5 小时前
Docker + CentOS 部署 Zookeeper 集群 + Kubernetes Operator 自动化运维方案
linux·网络安全·docker·信息安全·zookeeper·kubernetes·云计算·系统运维
诗句藏于尽头6 小时前
完成ssl不安全警告
网络协议·安全·ssl
好奇的菜鸟8 小时前
如何在IntelliJ IDEA中设置数据库连接全局共享
java·数据库·intellij-idea
tan180°8 小时前
MySQL表的操作(3)
linux·数据库·c++·vscode·后端·mysql
满昕欢喜8 小时前
SQL Server从入门到项目实践(超值版)读书笔记 20
数据库·sql·sqlserver
Hello.Reader9 小时前
Redis 延迟排查与优化全攻略
数据库·redis·缓存
简佐义的博客10 小时前
破解非模式物种GO/KEGG注释难题
开发语言·数据库·后端·oracle·golang