SQL笔记

use master
go

IF DB_ID('Myschool') IS NOT NULL
drop database Myschool
go

create database Myschool
on(
name=Myschool_dat,
filename='D:\MydateMyschool_dat.mdf',
size=5,
maxsize=50,
filegrowth=5
)
log on
(
name=Myschool_log,
filename='D:\MydateMyschool_log.ldf',
size=3,
maxsize=20,
filegrowth=1
)
go

IF EXISTS(SELECT*FROM SYSLOGINS WHERE NAME='KGC')
DROP LOGIN KGC
GO

--创建带有密码,指定默认数据库关闭强制密码过期验证的登录名
create login kgz with password='bdqn',
default_database=Myschool,
check_expiration=off
go

use Myschool
go

--创建用户捆绑用户名,指定默认默认策略为dbo
create user kgz for login kgz with default_schema=dbo
go

--为用户指定数据角色成员身份
exec sp_addrolemember 'db_owner',kgz
go

create table Unit
(
unitId tinyint not null identity(1,1) primary key,
unitName nvarchar(20) not null
)
go


create table Subjects
(
subjectId tinyint not null identity(1,1),
fkunitId tinyint not null,
subjectName nvarchar(50) not null,
classHours tinyint null,
constraint pk_subject_subjectId primary key(subjectId),
constraint fk_subject_fkunitId_unit_unitId foreign key (fkunitId) references Unit(unitId),
--约束 fk_从表名_从表字段_主表名_主表字段 外键 从表字段 字段 主表(主表字段)
constraint ck_subject_classHours check(classHours>0)
)
go

create table Student
(
studentId int not null identity(1,1),
studentName nvarchar(10) not null,
gender nchar(1) not null default'男',
moblie char(11) not null,
[address] nvarchar(200),
constraint pk_student_studentId primary key (studentId),
constraint un_student_moblie unique(moblie)
)
go

go

create table Result
(
resultId int not null identity(1,1),
fksubjectId tinyint not null ,
fkstudentId int not null,
[state] tinyint not null default 0,
score tinyint ,
testDate date,
constraint pk_reault_resultId primary key (resultId),
constraint fk_reault_fksubjectId_subjects_subjectId foreign key(fksubjectId) references Subjects(subjectId),
constraint fk_reault_fkstudentId_student_studentId foreign key(fkstudentId) references Student (studentId),
constraint ck_result_score check(score>=0)
)
go

--新增单条数据
--insert into Unit(unitName) values ('第一单元')
--select*from Unit

--新增多条数据
insert into Unit(unitName)
select '第一单元'union
select '第二单元'union
select '第三单元'
go


--数据迁移到新表
--create table TUnit(
--unitName nvarchar(10) not null
--)
--go


--insert into TUnit(unitName)
--select unitName from Unit

--select*from TUnit


--一次性创建源表数据
--select unitId,unitName into T2Unit from Unit
--select*from T2Unit


use Myschool
go
--新增一条数据
--insert into Student(unitName) values ('第一单元')
select*from Unit

--新增多条数据
insert into Student(studentName ,moblie ,[address])
select'王万香',13589658741,'江苏南京'union
select'梁靖',13589658742,'江苏连云港'union
select'戴一翔',13589658743,'江苏南京'union
select'汪吴鹏',13589658744,'江苏连云港'union
select'陈锐',13589658745,'安徽芜湖'union
select'段琪',13589658746,'安徽马鞍山'union
select'张庭',13589658747,'江苏南京'union
select'秦斌',13589658748,'浙江温州'union
select'袁林',13589658749,'浙江宁波'union
select'王栋梁',13589658740,'北京朝阳'
go

insert into Student(studentName ,gender,moblie ,[address])
select'尹玉婷','女',13589658750,'江苏盐城'
go
select*from Student

insert into Subjects (fkunitId,subjectName,classHours)
select'1','c#基础',56union
select'1','c#OOP',36union
select'2','SQL',28union
select'2','HTML+CSS',66union
select'3','JS',30union
select'3','ASP.NET',88
go

select*from Subjects

use Myschool
go

insert into Result(fkstudentId,fksubjectId,[state],score,testDate)
select'1','1','0','75','2018/01/05' union
select'1','2','0','91','2018/03/18' union
select'1','3','1',null,null union
select'1','4','0','79','2018/01/18' union
select'1','5','0','80','2018/06/01' union
select'1','6','0','84','2018/07/07' union
select'2','1','0','73','2018/01/05' union
select'2','2','0','100','2018/03/18'union
select'2','3','0','57','2018/08/15' union
select'2','4','0','99','2018/01/18' union
select'2','5','0','84','2018/06/01' union
select'2','6','0','88','2018/07/07' union
select'3','1','1',null,null union
select'3','2','0','50','2018/03/18' union
select'3','3','0','97','2018/08/15' union
select'3','4','0','55','2018/01/18' union
select'3','5','0','61','2018/06/01' union
select'3','6','0','59','2018/07/07' union
select'4','1','0','51','2018/01/05' union
select'4','2','0','57','2018/03/18' union
select'4','3','0','96','2018/08/15' union
select'4','4','0','95','2018/01/18' union
select'4','5','0','54','2018/06/01' union
select'4','6','0','65','2018/07/07' union
select'5','1','0','94','2018/01/05' union
select'5','2','0','71','2018/03/18' union
select'5','3','0','50','2018/08/15' union
select'5','4','0','76','2018/01/18' union
select'5','5','1',null,null union
select'5','6','0','95','2018/07/07' union
select'6','1','0','71','2018/01/05' union
select'6','2','0','76','2018/03/18' union
select'6','3','0','75','2018/08/15' union
select'6','4','0','55','2018/01/18' union
select'6','5','0','70','2018/06/01' union
select'6','6','0','98','2018/07/07' union
select'7','1','0','86','2018/01/05' union
select'7','2','0','87','2018/03/18' union
select'7','3','0','70','2018/08/15' union
select'7','4','0','64','2018/01/18' union
select'7','5','1',null,null union
select'7','6','0','58','2018/07/07' union
select'8','1','0','58','2018/01/05' union
select'8','2','0','51','2018/03/18' union
select'8','3','0','70','2018/08/15' union
select'8','4','0','90','2018/01/18' union
select'8','5','0','87','2018/06/01' union
select'8','6','0','51','2018/07/07' union
select'9','1','1',null,null union
select'9','2','0','87','2018/03/18' union
select'9','3','0','66','2018/08/15' union
select'9','4','0','50','2018/01/18' union
select'9','5','0','93','2018/06/01' union
select'9','6','1',null,null union
select'10','1','0','88','2018/01/05' union
select'10','2','0','68','2018/03/18' union
select'10','3','0','51','2018/08/15' union
select'10','4','0','94','2018/01/18' union
select'10','5','0','79','2018/06/01' union
select'10','6','0','98','2018/07/07'
go

use Myschool
go

--统计平均成绩
select
fkStudentId,AVG(score)平均成绩,COUNT(1)总计 --成绩字段列
from
Result --成绩表
where
[state]<>1 --条件
group by
fkStudentId --分组

--按省份统计男女数量
select
LEFT([address],2)省份,gender 性别,COUNT(1) --省份取前两位字段,性别计数
from
Student --学生表
group by
left([address],2), gender --分组
order by
left([address],2) desc, gender asc --排序




--字段子查询
use Myschool

select
resultId 成绩序号,
(select studentName from student where studentId=fkstudentId)学号,
(select subjectName from subjects where subjectId=fksubjectId)科目,
[state] 考试状态,
score 成绩,
testDate 日期
from
Result


--表子查询
select
*
from
(select
fkstudentId,fksubjectId,[state],score
from
result
)A



--条件子查询
select
*
from
Student
where
studentId
in
(
select
top 3 fkStudentId
from
Result
group by
fkStudentId
order by
AVG(score) DESC
)





--取前十项(分页查询一)
select
top 10 *
from
Result
where
resultId
not in
(
select
top 0
resultId
from
Result
)


--查询前10行,新增行号(分页查询二)
select
*
from
(
select
*,row_number() over(order by score) as rowNo
from
result
)A
where
rowNo
between
1
and
10



--查询缺考最多的学员信息
select
*
from
Student
where
studentId
in
(
select
fkstudentId
from
result
where
[state]=1
group by
fkstudentId
having
COUNT (1)
=
(
select
top 1 COUNT(1)
from
result
where
[state]=1
group by
fkstudentId
order by
COUNT(1) desc
)
)


--查询平均分比总平均分高的学员信息
select
*
from
Student
where
studentId
in
(
select
fkstudentId
from
Result
group by
fkstudentId
having
AVG (score )
>=
(
select
AVG (score)
from
Result
where
[state]=0
)
)






--查询学科成绩最后三名的学科成绩
/*select
subjectName
from
subjects
where
in
(

select
subjectId
from
result
where
subjectId=(
select
top 3 fksubjectId
from
Result
group by
fksubjectId
order by
AVG(score) desc
)
)

use Myschool
go
*/

--查询不同省份,科目学员平均成绩
SELeCT
ADDR 地区,SUBJ 科目 ,avg(score) 平均成绩
from(
select
left(
(
select
[address]
FROM
Student
where
studentId=fkstudentId
),2)ADDR,
(
select
subjectName
from
subjects
where
subjectId=fksubjectId
)SUBJ,

score
from
Result
)T
group by
ADDR,SUBJ
order by
ADDR



--查询江苏成绩最高的学员信息


--内连接查询

select
subjectId,unitName, subjectName, classHours --查询字段
from
Subjects S --从表名 从表别名
inner join
Unit U --主表名 主表别名
on
s.fkunitId =u.unitId --从表别名.外键=主表别名.主键

--查询所有学员单元科目成绩(四表联查内连接,)
select
studentId,studentName,unitName,subjectName ,[state],score ,testDate
from
Result R
inner join
Subjects S
on
R.fksubjectId =s.subjectId
inner join
Unit U
on
s.fkunitId =u.unitId
inner join
Student D
on
R.fkstudentId =D.studentId


--视图(一条复杂语句封装)

if exists (select * from sys.all_views where name='V_Result')
drop view V_Result
go

create view V_Result
as
select
studentId,studentName,unitName,subjectName ,[state],score ,testDate
from
Result R
inner join
Subjects S
on
R.fksubjectId =s.subjectId
inner join
Unit U
on
s.fkunitId =u.unitId
inner join
Student D
on
R.fkstudentId =D.studentId


--select *from V_Result 查询视图

--查询所有学员考试成绩(左外连接)
select
resultId,studentName ,moblie,subjectName,[state],score,testDate
from
Result E
left join
Student S
on
E.fkstudentId=S.studentId
left join
Subjects J
on
E.fkstudentId=j.subjectId



--查询所有学员考试成绩(全外连接) 用于检索学生成绩是否录入或录入成绩是否有成绩
select
resultId,studentName ,moblie,fksubjectId,[state],score,testDate
from
Result E
full join
Student S
on
E.fkstudentId=S.studentId




--创建普通索引
create index ix_studuent_studentName on student(studentName)
select * from Student where studentName='王万香'



--事务 transaction
begin tran --事务开始
delete from Student where studentId between 7 and 10
rollback --事务回滚
commit --提交事务

select* from student


create table Account
(
acc int,
balance money
)
go


--创建"存款"表
create table cunkuan
(
zhanghu char(4),
zijin int
)

--添加数据
insert into cunkuan (zhanghu ,zijin )
select 8888,1500 union
select 1111,200
go

--select * from cunkuan
--创建转账
create proc pro_zhuanMoney
@rtn int out, --传入rtn参数 out输出
@fromZhuan int, --传入转出参数
@tozhuan int, --传入转让参数
@money money --传入资金参数
as
declare @balance money
select @balance=balance from cunkuan where acc=@fromzhuan
if @money>@balance
select @rtn='余额不足'
else
begin
begin tran
declare @srcSum money,@newSum money
select @srcSum=SUM(balance) from cunkuan where Acc in (@fromzhuan,@tozhuan)
update cunkuan set balance=balance-@money where Acc =@fromzhuan
update cunkuan set balance=balance+@money where Acc=@tozhuan
select @newSum =SUM(balance) from cunkuan where Acc =(@fromzhuan,@tozhuan)
if srcSum=@newSum
begin
commit
select @rtn='转账成功'
end
else
begin
rollback
select @rtn='取消操作'
end

end
go

declare @rtn int
select @rtn=-2
exce pro_transferMoney @rtn out,8888,1111,200
select @rtn
go

insert into Account
select 85589043,1500 union
select 85588035,200
go

select * from Account
go

drop proc pro_transferMoney --转账
go

create proc pro_transferMoney --转账
@rtn nvarchar out,
@fromAcc int,
@toAcc int,
@money money
as
declare @balance money --声明balance参数

select @balance=balance from Account where acc = @fromAcc --Account账户
if @money>@balance
select @rtn= -1
else
begin
begin tran
declare @srcSum money,@nowSum money
select @srcSum=SUM(balance) from Account where acc in (@fromAcc,@toAcc)
update Account set balance=balance-@money where acc = @fromAcc
update Account set balance=balance+@money where acc = @toAcc
select @nowSum=SUM(balance) from Account where acc in (@fromAcc,@toAcc)


if @srcSum=@nowSum
begin
commit
select @rtn=1
end
else
begin
rollback
select @rtn=0
end

end

go

declare @rtn int
select @rtn=-2
exec pro_transferMoney @rtn out,85589043,85588035,1500
select @rtn
go


原文地址:https://www.cnblogs.com/x666066/p/10533567.html