您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

sqlServer学习1-sql脚本

bubuko 2022/1/25 19:27:37 sqlserver 字数 22902 阅读 757 来源 http://www.bubuko.com/infolist-5-1.html

创建数据库 use master --选择要操作的数据库 go--批处理命令 --创建数据库 create database TestNewBase --数据库名称 on primary --主文件组 ( name='TestNewBase',--数据库主要数据文件的逻辑名 filename='D: ...

创建数据库

use master --选择要操作的数据库 go--批处理命令

--创建数据库 create database TestNewBase --数据库名称 on primary --主文件组 ( name=‘TestNewBase‘,--数据库主要数据文件的逻辑名 filename=‘D:\课件\DBase\TestNewBase.mdf‘,--主要数据文件的路径(绝对路径) size=5MB,--数据库主要文件的初始大小 filegrowth=1MB--文件的增量 ) log on --创建日志文件 ( name=‘TestNewBase_log‘,--数据库日志文件的逻辑名 filename=‘D:\课件\DBase\TestNewBase_log.ldf‘,--日志文件的路径(绝对路径) size=1MB,--数据库日志文件的初始大小 filegrowth=10%--日志文件的增量 ) go

--删除数据库 drop database TestNewBase go

T-SQL创建表

--数据定义语言 DDL

use TestBase go --创建表 create table ProductInfos ( Id int identity(1001,1) primary key not null, --标识种子,增量 ProNo varchar(50) not null, ProName nvarchar(50) not null, TypeId int not null, Price decimal(18,2) default (0.00) not null, ProCount int default (0) null ) go

create table ProductType ( TypeId int identity(1,1) primary key not null, TypeName nvarchar(20) not null ) go

--删除表 drop table ProductInfos go

T-SQL修改表

use TestBase go --创建表 create table ProductInfos ( Id int identity(1001,1) primary key not null, --标识种子,增量 ProNo varchar(50) not null, ProName nvarchar(20) not null, TypeId int not null, Price decimal(18,2) default (0.00) not null, ProCount int default (0) null, ) go

--删除表 drop table ProductInfos go

 

--创建表之后,进行修改 --不删除原来的表的基础上,进行修改

--添加一列 ProRemark alter table ProductInfos add ProRemark nvarchar(max) null

--删除一列 ProRemark alter table ProductInfos drop column ProRemark

--修改一列 alter table ProductInfos alter column ProNo nvarchar(50) null

--修改列名 一般慎用 --exec sp_rename ‘ProductInfos.ProCount‘,‘Count‘,‘column‘

创建约束

use TestBase go --创建表 create table ProductInfos ( Id int identity(1001,1) primary key not null, --标识种子,增量 ProNo varchar(50) unique not null, ProName nvarchar(20) not null, TypeId int not null foreign key references ProductType(TypeId) , Price decimal(18,2) check(Price<10000) default (0.00) not null, ProCount int default (0) null, ) go

--删除表 drop table ProductInfos go

--在建表完成后,创建约束 create table ProductInfos ( Id int identity(1001,1) not null, --标识种子,增量 ProNo varchar(50) not null, ProName nvarchar(20) not null, TypeId int not null , Price decimal(18,2) not null, ProCount int null ) go

--主键 Id alter table ProductInfos add constraint PK_ProductInfos primary key(Id)

--外键 TypeId alter table ProductInfos add constraint FK_ProductInfos foreign key (TypeId) references ProductType(TypeId)

--unique约束 ProNo alter table ProductInfos add constraint IX_ProductInfos_ProNo unique(ProNo)

--unique ProNo+ProName alter table ProductInfos add constraint IX_ProductInfos_ProNo unique(ProNo,ProName)

--check约束 alter table ProductInfos add constraint CK_ProductInfos_Price check(Price<10000)

--default约束 alter table ProductInfos add constraint DF_ProCount default (0) for ProCount

T-SQL操作数据之插入数据

use TestBase go

--1.单条数据 ---insert DML --1) insert (into) 表名(列名,列名....) values (值,值....) insert into ProductType (TypeName) values (‘工具类‘) --2) insert into 表名(列名,列名....) select 值,值.... insert ProductType(TypeName) select ‘鞋类‘

--一次性插入多条 批量插入操作 --1) insert into ProductType (TypeName) values (‘工具类1‘),(‘工具类2‘),(‘工具类3‘),(‘工具类4‘)

--2) union 去重 union all 允许重复 union all效率比union高 insert Test(MName,Age) --select 5,‘ddddd‘,23 union --select 6,‘dd‘,25 union --select 7,‘bbdddbb‘,20 union

select ‘fff‘,21 union select ‘fff‘,21

--3.克隆数据 将一张表的数所复制到另一张表 --1) 目标表在数据库已经存在 insert into Test(MName) ---目标表 select TypeName from ProductType --源表

--2)目标表之前数据库中不存在,执行操作时自动创建的 select TypeName into Test2 --目标表 from ProductType

T-SQL操作数据之更新删除数据

use TestBase go

--更新数据 update 几乎不会不用where条件 --主键不可以修改 --如果不加where条件,会把整张表的数据都修改了 update Test set MName=‘ssss‘,Age=30 where Id=5

--数据删除 :只是删除数据,表还在; 连同表一起删除 --1)删除数据 delete from Table 不加条件,会删除整个表数据,几乎都要加where条件 标识列 值还是接着删除前的值而自增,而不是从初始值开始 --delete语句会造成标识列的值不连续 delete from Test where Id = 20 --如果我们想删除数据,让标识列的值恢复到初始值,怎么办? truncate table Test --表数据清空,恢复到初始化,标识列也恢复 --表面上看,delete from Test 效果一样. --truncate效率比delete from Test高.delete每删除一条数据,都会在日志里记录.truncate不会记录日志,不激活触发器,drop truncate 是即时操作,不能rollback,delete update insert 事务中,可以恢复. --慎用truncate 一旦删除,不能恢复.

T-SQL查询数据之单表查询

--1.查询一个表所有数据 * 所有列 只需要部分列的数据 内存毕竟有限 select * from UserInfos --选择 --2.查询部分列的数据 推荐使用 select UserId,UserName,Age from UserInfos where Age>30

--3.数据库里表字段 英文名称,程序中列名--中文 --给列命别名 --列名 as 别名 列名 别名 别名=列名 select UserId as 用户编号,UserName 用户名,年龄= Age from UserInfos

--排序 order by --主键,默认就有排序功能 从小到大 --升序 asc --降序:从大到小 desc --不管是否有条件,还是分组,order by 永远放在最后 select UserId,UserName,Age from UserInfos order by Age,UserId

SQL查询之模糊查询

--1. % 0个或多个 匹配任意类型和长度 效率不高 --1) like ‘%ad%‘ 包含于 select * from UserInfos where UserId like ‘%2%‘ --2) like ‘%in‘ 以匹配字符或字符串结尾 select * from UserInfos where UserName like ‘%u‘ --3)like ‘w%‘ 以匹配字符或字符串开头 select * from UserInfos where UserName like ‘l%‘

--2._ 匹配单个字符 限制表达式的字符长度 select * from UserInfos where UserName like ‘_

--3.[] 范围匹配 括号中所有字符中的一个 select * from UserInfos where UserName like ‘ad[mnd]in‘ select * from UserInfos where UserName like ‘ad[m-p]in‘

--4.[^] 不在括号中所有字符之内的单个字符 select * from UserInfos where UserName like ‘adabcin‘

范围查询

--1.select from where 子句 条件 -- 给定范围 --1) 比较运算符 > < >= <= <> select * from UserInfos where Age <30 and DeptId>1 --2)in (2,3,4) not in (2,3,4) 不在这个范围之内 select * from UserInfos where Age in (30,35,24)

select * from UserInfos where Age not in (30,35,24) --子查询 select * from UserInfos where DeptId in ( select DeptId from DeptInfos where DeptId>1 )

--3) between and 推荐 等价于 >= and <= select * from UserInfos where Age between 20 and 33 --Age>=20 and Age<=33

--2.前面多少条、百比比 select top 10 * from UserInfos select top 100 percent * from UserInfos

聚合函数

--select count(1) 伪造列 select 1 from UserInfos select count(1) Record from UserInfos --一般统计一个表的记录数

--sum 求和 相加 select sum(Age) from UserInfos

--agv 求平均 select avg(Age) from UserInfos

--max 求最大 select max(Age) from UserInfos

--min 求最小 select min(Age) from UserInfos

分组函数

--group by 分组 -- 语法:select ... where ... group by .... order by .... --统计各部分有多少个用户 --select 出现的列名,必须出现在group by之后或包含在聚合函数中 Select DeptId,count(1) 用户数 from UserInfos --where Age>26 group by DeptId having DeptId>1 --分组后的筛选条件 order by DeptId desc

连接查询之内连接

--inner join on 关联条件 select UserId,UserName,Age,u.DeptId,DeptName from UserInfos u inner join DeptInfos d on d.DeptId=u.DeptId --and .... where Age>25

--隐式写法 select UserId,UserName,Age,u.DeptId,DeptName from UserInfos u, DeptInfos d where d.DeptId=u.DeptId and Age>40

sqlServer学习1-sql脚本

原文:https://www.cnblogs.com/wxhao/p/13624853.html


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶