编程语言: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行

相关推荐
l1t11 小时前
DeepSeek总结的将 Rust Delta Kernel 集成到 ClickHouse
数据库·clickhouse·rust
qq_2837200512 小时前
万字深度:Chroma 向量数据库全解析 — 核心原理、实战操作、性能优化与工程最佳实践
数据库·性能优化
黄筱筱筱筱筱筱筱12 小时前
二进制包安装MySql服务
数据库
初心未改HD12 小时前
LLM应用开发之向量数据库详解
数据库·人工智能
键盘上的猫头鹰12 小时前
【从零学MySQL(三)】数据增删改(DML)及 SELECT 查询详解
数据库·mysql·数据分析
KaMeidebaby13 小时前
卡梅德生物技术快报|蛋白的过表达质粒构建与生信分析实验全流程复盘
前端·数据库·其他·百度·新浪微博
国科安芯13 小时前
ASM232S抗辐照RS-232收发器的技术架构与空间环境适应性研究
单片机·嵌入式硬件·安全·架构·安全性测试
渣渣盟13 小时前
数据库之两段锁协议相关理论及应用
数据库·关系规范化·两段锁协议
LCG元13 小时前
Istio - 服务网格流量治理深度解析:灰度发布 / 故障注入配置实践
java·数据库·istio
SuniaWang13 小时前
《Agentx专栏》03-架构设计:AgentX的六层架构是如何生长出来的
java·数据库·redis·docker·ai·架构