「作者主頁」:士別三日wyx
「作者簡介」:CSDN top100、阿里云博客專家、華為云享專家、網絡安全領域優質創作者
「推薦專欄」:對網絡安全感興趣的小伙伴可以關注專欄《網絡安全自學教程》
SQL Server是微軟提供的一種關系型數據庫,語法跟MySQL類似,這篇文章主要是熟悉SQL Server的語句格式,會脫褲、在告警流量中能認出來即可。
SQL Server
- 1、表操作
- 1.1、創建表
- 1.2、刪除表
- 1.3、修改表
- 2、數據操作
- 2.1、新增
- 2.2、刪除
- 2.3、修改
- 2.4、查詢
- 2.4.1、模糊查詢
- 2.4.2、范圍查詢
- 2.4.3、子查詢
- 2.4.4、排序
- 2.4.5、去重
- 2.4.6、前n行
1、表操作
1.1、創建表
create table teacher(id int primary key,name varchar(10) not null,age int)
1.2、刪除表
drop table teacher;
1.3、修改表
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、新增
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、刪除
delete test.dbo.users where id=1
2.3、修改
update test.dbo.users set username='aaa' where id=1;
2.4、查詢
select * from test.dbo.users -- 普通條件查詢
where id=1;
?
2.4.1、模糊查詢
select * from test.dbo.users
where username like '%li%';
2.4.2、范圍查詢
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、子查詢
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、排序
select * from test.dbo.users -- 從小到大排序
order by id asc;select * from test.dbo.users -- 從大到小排序
order by id desc;
2.4.5、去重
select distinct * from test.dbo.users; -- 去重
2.4.6、前n行
select top 3 * from test.dbo.users; -- 前n行
?