Sql Server学习笔记
2024-10-08 02:37:35
1、指定路径创建数据库
create database student
on--创建库的时候必须写
(
name=student,
filename='E:\database\student.mdf'--关于存放路径问题与本计算机的系统有关,最好创建在文件夹下
)
2、彻底删除数据库
drop database student
3、创建表
--identity 自动增长列
--primary key 主建
use student
go
create table stuinfo
(
stuId int identity primary key,
stuName nvarchar(20) not null,
stuSex nvarchar(1) not null,
stuAge int not null
)
4、添加数据
insert into --添加
--stuinfo 表格
--values(添加的内容)
--添加的内容规则:内容数量与表结构列名一致,顺序一致
--添加的内容:特别的地方、自动增长列除外
--insert into stuinfo values(添加内容)
insert into stuinfo values('sss','男',22)
insert into stuinfo
select 'aa','男','' union
select 'aa','男','' union
select 'aa','男','' union
select 'aa','男',''
5、查询
select * from stuinfo
6、删除表里面的内容(保存表的结构)
delete stuinfo --删除表的所有数据
delete stuinfo where stuAge>18 --删除年龄大于18岁的学生
truncate table stuinfo --当数据库清空时,才有必要,把自动增长列归0
7、修改表里面的内容
update stuinfo set Stuage=15 where Stuage>18 --将学生年龄大于18的改为15岁
8、查询前5的数据
select top 5 stuName as '前5名的学生' from stuinfo
9、查询该表前50%的数据
select top 50 percent * from stuinfo
10、查询该表ID 2-5之间的数据
select * from stuinfo stuID between 2 and 5
11、查询姓名中有”李“字的学生
select * from stuinfo where stuName like '%李%'
12、删除某个字段为空的所在行
delete [SS_OLMS_SZ].[dbo].[SZ_SCADAPoint] where 序号 is NULL
13、将一个表的字段值对应字段插入到另一个表中
INSERT INTO SS_MonitorStation(M_ID,M_Name,GISLongitude,GISLatitude,M_StationNo,M_Description,M_StationTypeID)
SELECT 序号,测点名称1,[北坐标(X)],[东坐标(Y)],数据,备注,TypeID FROM SZ_SCADAPoint
14、修改一个表的字段值,通过另一个表对应的字段值一一对应修改
update a set a.TagName = b.TagName from b where a.DataID=b.DataID
15、循环插入语句(便于测试)
declare @num int
set @num =1
while(@num<5)
begin
set @num = @num+1
insert student (name,age,address) values('a',@num,'b')
end
16、删除数据库里面的所有的表
use student
GO
declare @sql varchar(8000)
while (select count(*) from sysobjects where type='U')>0
begin SELECT @sql='drop table ' + name FROM sysobjects
WHERE (type = 'U') ORDER BY 'drop table ' + name exec(@sql)
end
17、按时间(单位为年),分组计数
select DATEPART(YEAR,Birthday),COUNT(*)FROM stuinfo GROUP BY DATEPART(YEAR,Birthday)
查询效果:
18、修改列名
sp_rename '表名.旧列名','新列名','column'
19、删除有规律的表
declare @i int
declare @s nvarchar(100)
set @i=129
while @i<143
begin
Set @s='drop table dbo.Initial_'+cast(@i as varchar)
print @s
exec(@s)
set @i=@i+1
end
执行效果如下:
20、从已有的表创建一个不存在的表
select * into 新表 from 旧表
最新文章
- Oracle 中的分析函数
- Sharepoint学习笔记—习题系列--70-576习题解析 -(Q25-Q28)
- Spring源码阅读系列总结
- PNG格式的图像文件,创建的图像的MIME类型的头部
- sqlalchemy 优化count()……
- atitit.提升备份文件复制速度(3) ----建立同步删除脚本
- js简单上传进度条
- MVC 路由模块分析(一)
- UIApplication 概述
- MySQL允许远程访问
- AjaxPro框架
- 协议Protocol
- host字段变复杂了
- codility上的问题(18) Rho 2012
- 第7章 桥接模式(Bridge Pattern)
- 【算法系列学习】[kuangbin带你飞]专题十二 基础DP1 F - Piggy-Bank 【完全背包问题】
- linux-之常用命令
- CentOS7快速搭建LNMP环境
- VC获取操作系统位数
- html网页采集
热门文章
- git 删除时报 the branch is not fully merged 这是什么意思
- springboot 2.1.6.RELEASE pom 第一行报错
- Date和Calendar
- 【图数据库】史上超全面的Neo4j使用指南
- Codeforces 1100F(离线 or 在线)
- The 10th Shandong Provincial Collegiate Programming Contest H.Tokens on the Segments(贪心+优先级队列 or 贪心+暴力)
- linux 存取 I/O 内存
- 5.29&#160;SD省队培训D1
- EF 配置多个数据库
- Python10_代码规范和可读性