--------------------------------創建視圖------------------------------------- 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)