数据库各类模板

时间:2022-11-22 00:40:54
--------------------------------创建视图-------------------------------------

use StudentManager
go
--判断视图是否存在
if exists(select * from sysobjects where name='view_ScoreQuery')
drop view view_ScoreQuery
go
--创建视图
create view view_ScoreQuery
as
select top 1000 Students.StudentId,StudentName,ClassName,
C#=CSharp,SQLDB=SQLServerDB,
ScoreSum=(CSharp+SQLServerDB) from Students
inner join ScoreList on Students.StudentId=ScoreList.StudentId
inner join StudentClass on Students.ClassId=StudentClass.ClassId
order by StudentClass.ClassId
go
--使用视图查询
select * from view_ScoreQuery

-------------------有参存储过程-------------------
use StudentManager
go
if exists(select * from sysobjects where name='usp_ScoreQuery3')
drop procedure usp_ScoreQuery3
go
--创建带参数的存储过程
create procedure usp_ScoreQuery3 
@CSharp int=60,
@DB int=60
as
select Students.StudentId,StudentName,C#=CSharp,DB=SQLServerDB
from Students
inner join ScoreList on Students.StudentId=ScoreList.StudentId
where CSharp<@CSharp or SQLServerDB<@DB
go
--调用带参数的存储过程
exec usp_ScoreQuery3 65 --第二个参数没有赋值,则默认
exec usp_ScoreQuery3 @DB=65
exec usp_ScoreQuery3 default,65 --不使用显示方式赋值
exec usp_ScoreQuery3 --两个参数都是用默认参数

---------------------------事务--------------------------

use StudentManager
go
declare @errorSum int --定义变量,用于累计事务执行过程中的错误
set @errorSum=0 --初始化为0,即无错误
begin transaction
begin	
update CardAccount set CurrentMoney=CurrentMoney-1000
where StudentId=100001
set @errorSum=@errorSum+@@error --累计是否有错误	
update CardAccount set CurrentMoney=CurrentMoney+1000
where StudentId=100002
set @errorSum=@errorSum+@@error --累计是否有错误
if(@errorSum>0)
rollback transaction
else 
commit transaction
end
go
--查询余额
select Students.StudentId,StudentName,CurrentMoney from Students
inner join CardAccount on Students.StudentId=CardAccount.StudentId

-------------------------------------创建各类主外键-------------------
--创建学员信息数据表
use StudentManager
go
if exists (select * from sysobjects where name='Students')
drop table Students
go
create table Students
(
StudentId int identity(100000,1) ,
StudentName varchar(20) not null,
Gender char(2) not null,
Birthday smalldatetime not null,
StudentIdNo numeric(18,0) not null,--身份证号 
PhoneNumber varchar(50),
StudentAddress varchar(500),
ClassId int not null --班级外键
)
go
--创建班级表
if exists(select * from sysobjects where name='StudentClass')
drop table StudentClass
go
create table StudentClass
(
ClassId int primary key,
ClassName varchar(20) not null
)
go
--创建成绩表
if exists(select * from sysobjects where name='ScoreList')
drop table ScoreList
go
create table ScoreList
(
id int identity(1,1) primary key,
StudentId int not null,
CSharp int null,
SQLServerDB int null,
UpdateTime smalldatetime not null
)
go
--创建一卡通账户表
if exists(select * from sysobjects where name='CardAccount')
drop table CardAccount
go
create table CardAccount
(
StudentId int not null,
CurrentMoney money check(CurrentMoney>1) --当前余额(必须大于1元) 
)
go
--创建管理员用户表
if exists(select * from sysobjects where name='Admins')
drop table Admins
create table Admins
(
LoginId int identity(1000,1) primary key,
LoginPwd varchar(200) not null,
AdminName varchar(20) not null
)
go
--创建数据表的各种约束
use StudentManager
go
--创建“主键”约束primary key
if exists(select * from sysobjects where name='pk_StudentId')
alter table Students drop constraint pk_StudentId

alter table Students
add constraint pk_StudentId primary key (StudentId)


--创建唯一约束unique
if exists(select * from sysobjects where name='uq_StudentIdNo')
alter table Students drop constraint uq_StudentIdNo
alter table Students
add constraint uq_StudentIdNo unique (StudentIdNo)

--创建身份证的长度检查约束
if exists(select * from sysobjects where name='ck_StudentIdNo')
alter table Students drop constraint ck_StudentIdNo
alter table Students
add constraint ck_StudentIdNo check (len(StudentIdNo)=18)

--创建默认约束
if exists(select * from sysobjects where name='df_StudentAddress')
alter table Students drop constraint df_StudentAddress
alter table Students 
add constraint df_StudentAddress default ('地址不详' ) for StudentAddress

if exists(select * from sysobjects where name='df_UpdateTime')
alter table ScoreList drop constraint df_UpdateTime
alter table ScoreList 
add constraint df_UpdateTime default (getdate() ) for UpdateTime

--创建外键约束
if exists(select * from sysobjects where name='fk_classId')
alter table Students drop constraint fk_classId
alter table Students
add constraint fk_classId foreign key (ClassId) references StudentClass(ClassId)

if exists(select * from sysobjects where name='fk_StudentId')
alter table ScoreList drop constraint fk_StudentId
alter table ScoreList
add constraint fk_StudentId foreign key(StudentId) references Students(StudentId)

if exists(select * from sysobjects where name='fk_CardStudentId')
alter table CardAccount drop constraint fk_CardStudentId
alter table CardAccount
add constraint fk_CardStudentId foreign key(StudentId) references Students(StudentId)