zoukankan      html  css  js  c++  java
  • 数据库各类模板

    --------------------------------创建视图-------------------------------------
    
    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)
    
      
      
    

      

  • 相关阅读:
    Visual Studio 2010使用Visual Assist X的方法
    SQL Server 2000 评估版 升级到 SQL Server 2000 零售版
    双网卡多网络单主机同时访问
    开发即过程!立此纪念一个IT新名词的诞生
    delphi dxBarManager1 目录遍历 转为RzCheckTree2树
    5320 软件集合
    delphi tree 从一个表复制到另一个表
    DELPHI 排课系统课表
    长沙金思维 出现在GOOGLE的 金思维 相关搜索里啦!!
    如何在DBGrid的每一行前加一个单选框?
  • 原文地址:https://www.cnblogs.com/victor-huang/p/9021529.html
Copyright © 2011-2022 走看看