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

相关推荐
Cyanto40 分钟前
深入MyBatis:CRUD操作与高级查询实战
java·数据库·mybatis
datascome1 小时前
文章发布易优CMS(Eyoucms)网站技巧
数据库·经验分享·爬虫·数据采集·eyoucms·易优cms
有想法的py工程师2 小时前
PostgreSQL 锁等待监控,查找等待中的锁
数据库
学不会就看2 小时前
Django--02模型和管理站点
数据库·oracle·django
浩浩测试一下3 小时前
Windows 与 Linux 内核安全及 Metasploit/LinEnum 在渗透测试中的综合应用
linux·运维·windows·web安全·网络安全·系统安全·安全架构
←か淡定☆ ヾ3 小时前
SQL Server 2008R2 到 2012 数据库迁移完整指南
数据库·sql server
瀚高PG实验室3 小时前
Arcgis连接HGDB报错
数据库·arcgis·瀚高数据库
IT小辉同学4 小时前
PostgreSQL 与 MySQL 获取字段注释并转换为驼峰命名教程
数据库·mysql·postgresql
xinghunzhiye20104 小时前
redis升级
数据库·redis·缓存
一只fish4 小时前
MySQL 8.0 OCP 1Z0-908 题目解析(21)
数据库·mysql