SQL Server的数据库是有组织的数据的集合,这种数据集合具有逻辑结构并得到数据库系统的管理的维护。
数据库由包含数据的基本表和对象(如视图,索引,存储过程和触发器等)组成,其主要用途时处理数据管理活动产生的信息.
使用T-SQL语句创建数据库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
create database zhangboSTU
on primary //数据库文件
(name = 'zhangbo_data',filename = 'C:\data\zhangbo_data.mdf',size = 5MB,maxsize = 20MB,filegrowth = 20% //曾长方式
)
log on //日志文件
(name = 'zhangbo_log',filename = 'C:\data\zhangbo_log.log',size = 2MB,maxsize = 10MB,filegrowth = 2MB
)
|
使用T-SQL语句修改数据库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
//增加数据库空间
alter database 数据库名
modify file
(name = 逻辑文件名,,
size = 文件大小)
//增加数据库文件
alter database 数据库名
add file | add log file
()
//删除数据库文件
alter database 数据库名
remove file 文件名
|
使用T-SQL语句删除数据库
使用T-SQL语句创建表
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
create table course(cno char(4) primary key not null, //主键cname char(10),classhour tinyint,credit tinyint
)
create table student(sno char(7) primary key not null,sname nvarchar(10) not null,ssex nchar(1) not null,sage tinyint ,en_time datetime,specialty nvarchar(50),grade nvarchar(50)
)
|
使用T-SQL语句修改表
1
2
3
4
5
6
7
8
|
use teaching
go
alter table student
alter column sname nvarchar(20) NULL
alter table course
add teacher nvarchar(20)
alter table student
drop column sage
|
使用T-SQL语句创建主键和外键约束
1
2
3
4
5
6
7
8
9
|
create table sc
(sno char(7) not null,cno char(4)not null,score intconstraint pk_ss primary key(sno,cno),constraint stu_foreign foreign key(sno) references student(sno),constraint cou_foreign foreign key(cno) references course(cno)
)
|
在修改表时设置主键约束
1
2
|
alter table student
add constraint pk_st primary key(sno)
|
在修改表时设置UNIQUE约束
1
2
|
alter table student
add constraint uk_st unique(id)
|
在使用UNIQUE约束时应考虑的问题
UNIQUE约束所在列允许空值,但是主键约束所在的列不允许空值;
在一个表可以有多个UNIQUE约束;
UNIQUE约束强制在指定的列上创建一个唯一性索引
在修改表时定义默认值约束
1
2
|
alter table student
add constraint dk_stu default(getdate()) for en_time with values
|
在修改表时创建CHECK约束
1
2
|
alter table sc
add constraint cj_constraint CHECK(score>=0 and score<=100)
|
使用T-SQL语句插入数据
1
2
|
insert into score(sno,cno,score)
values(130200,'C01',90),(130220,'C02',50)
|
使用T-SQL语句修改数据
1
|
update score set cno='C03' where sno='130220'
|
使用T-SQL语句删除数据
1
|
delete student where sno='130200'
|
使用MERGE语句插入,修改,删除数据
1
2
3
4
5
|
merge ProductNew as d using Product as s
on s.ProductID = d.ProductID
when not matched then insert(ProductID,ProductName,price)
values(s.ProductID,s.ProductName,price);
select * from ProductNew
|
使用T-SQL语句投影查询
1
2
|
select sno as 学号 , sname 姓名 from student
select sno,cno,score150 = score *1.5 from sc
|
使用T-SQL语句选择查询
1
2
3
4
5
6
7
|
select * from sc where score>=60
select * from student where specialty='计算机' and ssex='男'
select * from student where specialty='计算机' or ssex='男'
select * from sc where score not between 80 and 90
select * from student where specialty in('计算机','通信')
select * from student where sname like '张%'
select * from sc where score is null
|
使用T-SQL语句分组和汇总
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
select ssex , COUNT(ssex) as 人数 from student
group by ssex
select sno ,COUNT(cno) as 学修课程数 from sc
group by sno having COUNT(cno)>=2
select AVG(score) as 平均成绩 from sc where sno=1302001
select cno,AVG(score) as 平均成绩 ,count(sno) as 选课人数 from sc
group by cno
with cube
select specialty,ssex,count(*) as 人数 from student
group by specialty,ssex
with rollup
select specialty,ssex,count(*) as 人数 from student
group by ssex,specialty
with cube
|
CUBE和ROLLUP的使用
cube指定在结果集内不仅包含由GROUP BY提供的行,还包含汇总行
汇总行针对每个可能的组和子组组合在结果集内返回
rollup指定在结果集内不仅包含由GROUP BY提供的行,还包含汇总行,按层次结构顺序,从组内的最低级别到最高级别汇总行
使用CUBE和ROLLUP时,不支持区分性聚合函数,例如AVG,COUNT等
内连接和全连接
1
2
3
4
5
6
7
|
select student.sname,sc.cno,sc.score from student inner join sc on student.sno=sc.sno
select a.sname,b.cno,b.score from student as a inner join sc as b on a.sno=b.sno
where a.specialty='计算机'
order by a.sname
compute avg(b.score) by a.sname
select * from student a inner join student b
on a.sname=b.sname and a.sno<>b.sno
|
外连接
1
2
3
|
select student.*,sc.cno,sc.score from student left join sc on student.sno=sc.sno
select course.* ,sc.score,student.sname,student.sno from course full join sc on course.cno=sc.cno
full join student on student.sno=sc.sno
|
子查询
1
2
3
4
5
6
7
8
|
select a.sno,a.sname,a.specialty
from student a,student b
where a.specialty=b.specialty and b.sname = '沈艳'
select sname,score from student,sc where student.sno=sc.sno and cno = 'C001' and sc.score >(select score from sc where cno = 'C001' and sno =(select sno from student where sname = '郑丽')
)
|
CAST函数
使用CAST函数进行数据类型转换时,在下列情况下能够被接受:
(1)两个表达式的数据类型完全相同。
(2)两个表达式可隐性转换。
(3)必须显式转换数据类型。
1
2
|
CAST('12.5' AS int)
cast(@i as varchar)
|
SQL Server—查询表结构
在SQL Server中不同于在MySQL或者Oracle中使用desc来查询数据表的表结构,而是使用如下两条语句查询:
1
2
|
sp_help student
sp_columns student
|
内嵌表值函数
1
2
3
4
5
|
create function stu_func(@specialty nvarchar(50)) returns table
as return
(select a.sno,a.sname,b.cno,b.score from student a,sc b where specialty=@specialty and a.sno=b.sno)select * from stu_func('计算机')
|
多语句表值函数
1
2
3
4
5
6
|
create function stu_score(@no char(7)) returns @score table(xs_no char(7),xs_name char(6),kc_name char(10),cj int,xf int)
as
begininsert into @score select s.sno,s.sname,c.cname,c.credit,sc.score from student s,course c,sc sc where s.sno=sc.sno and c.cno=sc.cno and s.sno=@no
return
end
|
创建无参存储过程
1
2
3
4
5
6
7
8
9
10
11
12
|
create procedure stu_avg
as
select sno,AVG(score) as 'avgstore' from sc
group by sno
go
create procedure getstu @number char(7)
as
select stu.sno,sname,specialty,s.cno,s.score from sc s,student stu
where s.sno=@number and s.sno=stu.sno
compute avg(score)
go
exec getstu 1302001
|
创建带output参数的存储过程
1
2
3
4
5
6
7
|
create procedure avg_price @gn varchar(20) , @avgp int output
asselect @avgp=AVG(price) from goods where gname=@gn
declare @avgprice int
exec avg_price '电视',@avgprice output
print cast(@avgprice as char(10))
go
|
启动或禁用表上所有触发器
1
2
3
|
alter table student
disable trigger all
enable trigger all
|
事务
1
2
3
4
|
begin transaction 定义一个事务的开始
commit tran 提交一个事务
rollback tran 回滚事务
save transaction 在事务内设置保存点
|
SQL_server相关推荐
- zabbix监控添加psql数据库和sql_server数据库模板
一.Psql数据库性能监控: 1) zabbix-agent客户端安装pg_monz步骤: [root@zabbix ~]# tarxfz pg_monz-1.0.1.tar.gz [root@zab ...
- (php)thinkphp3.2配置sql_server
(php)thinkphp3.2配置sql_server 标签: php thinkphp sql_server 2016年12月16日 15:20:19631人阅读 评论(0) 收藏 举报 分类: ...
- SQL_Server优化
******************************************* SQL关键字详解 升序排序 ASC -------------------------------------- ...
- 宝塔安装sqlserver_windows 宝塔安装sql_server 2008,php链接配置流程
windows 宝塔安装sql_server 2008,php链接配置流程 第一,在宝塔主页安装,apache,php 5.5版本,sql_server 2008 记住数据库的账号个密码还有服务器名称 ...
- SQL_server 的基本操作
1.---------------数据库基本操作 主键 : 1.不重复 2.不为NULL 外键 1.取消重复行(消除完全一样的行,保留一行) select distinct cloumname1,cl ...
- SQL_server 数据库备份信息查看
select distinct s.database_name, s.first_lsn,s.last_lsn,s.database_backup_lsn,s.backup_finish_date,s ...
- SQL_Server 2008R2数据库安装教程
教程地址:https://jingyan.baidu.com/article/eae078276b7e741fed54857b.html
- 2021-10-29 visual Foxpro连接数据库sql_server并且执行sql语句
打开vf工具,创建一个程序,然后复制黏贴以下代码 *-- 建立连接 lcDSNLess="driver=SQL Server;server=localhost;uid=SA;pwd=你的密码 ...
- 你苦苦寻找的SQL_server总结来啦
目录 一.数据库的创建 二.表的创建 2.1 表的创建 2.2 数据类型 2.3 表的操作 2.4 部分数据导入到另一个表中去 2.5 表的高级操作 三 .变量与赋值(循环.分支)语句 3.1变量 3 ...
最新文章
- the job was canceled什么意思_这些英语短语,因为相差一个“the”导致意思大不相同!...
- hibernate与mybatis的区别和应用场景
- mapx实现热点效果
- 怎么修改docker镜像的名字_Docker这些none:none的镜像,难道就不配拥有名字吗
- Cow Toll Paths(floyd变形)
- java 实现压缩zip的几种方案
- java string contains indexof,java.lang.String.contains()方法实例
- 实时流(直播流)播放、上墙(大屏播放)解决方案
- Python-Matplotlib可视化(8)——图形的输出与保存
- 台式机装mac系统_苹果电脑装双系统mac+win7图文教程
- 在spring boot中3分钟上手RPC框架Dubbo
- 为什么 Math.min() 比 Math.max() 大?
- 微信小程序可滑动周日历组件
- 文法规则自顶向下分析
- 特么,冒泡排序有这么难?
- win10 显示屏测试软件,win10系统检测不到显示器的解决方法
- Matlab中的im2col函数
- PHP+ mysql实现注册登录功能
- 给定经纬度计算距离_根据经纬度计算两点间距离JAVA版
- Minimal Area
热门文章
- 【中移芯昇】4. i2s播放音乐片段
- oracle使用游标insert数据库,数据库游标使用之oracle游标
- 机器学习:基于Apriori算法对中医病症辩证关联规则分析
- c语言怎么判断素数 n 2,C语言判断正整数n(n2)是否是素数
- 自制一个USB2.0拓展坞
- 素數(Prime Number),亦称质数
- 电商4.0来了,你看懂了吗,通证经济又是一个大的风口红利
- Cookie报错: An invalid character [32] was present in the Cookie value
- 印度的软件工业(建议程序员必读)
- 电大计算机信息理工英语2,2018年电大《理工英语2》期末考试试题及答案.pdf