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

相关推荐
赵渝强老师3 分钟前
【赵渝强老师】openGauss的数据库
数据库·opengauss·国产数据库·高斯数据库
憧憬成为web高手5 分钟前
l33t-hoster
学习·web安全·网络安全
HackTwoHub10 分钟前
Sqli-Scanner SQL注入SKILL自动化挖掘SQL注入,零依赖自动化SQL注入挖掘,赏金猎人
数据库·人工智能·sql·web安全·网络安全·自动化·系统安全
l1t22 分钟前
DuckDB对group by cube / rollup / groupping sets查询的优化
数据库·duckdb
zhengfei6111 小时前
小白级手册——全面剖析红队信息收集思考
网络·安全·web安全
Database_Cool_1 小时前
什么是湖仓一体?和数据仓库的本质区别(附 AnalyticDB MySQL 湖仓一体方案)
数据库·数据仓库·mysql
l1t1 小时前
DeepSeek总结的MariaDB 的 DuckDB 存储引擎
数据库·mariadb
爱网络爱Linux2 小时前
网络安全与渗透测试实用工具大全
web安全·网络安全·信息安全·cisp-pte·cisp·cissp
tiancaijiben2 小时前
阿里云VMware服务完全对接指南:从环境准备到混合云生产级应用
数据库
xsc-xyc2 小时前
用 Tailscale + Syncthing 实现手机、电脑与 NAS 的跨网络文件同步
linux·网络·网络安全·智能手机·电脑