zoukankan      html  css  js  c++  java
  • 事务,视图和索引

    --事务
    use MySchool

    --开启事务
    begin transaction 

     --声明局部变量
    declare @errorSum int

    set @errorSum=0

    update Bank set CusterMoney=CusterMoney-1000
    where CusterName='李四'
    set @errorSum=@errorSum+@@ERROR --累计是否有误
    update Bank set CusterMoney=CusterMoney+1000
    where CusterName='张三'
    set @errorSum=@errorSum+@@ERROR --累计是否有误
    if(@errorSum=0)
    begin

    --提交事务
    commit transaction 
    end
    else
    begin

    --回滚事务
    rollback transaction 
    end

    --视图

    --判断是否有视图,若有删除
    if exists (select *from sysobjects where name='vw_Student_Result')
    drop view vw_Student_Result
    go
    --创建学生和成绩视图
    create view vw_Student_result
    as
    select StuName 姓名,Grade 成绩
    from Student,Result
    where Student.StuNo=Result.StuNo
    go
    --查看视图结果
    select *from vw_Student_result

    --索引

    语法:

    create [unique]  [clustered|nonclustered]  index Index_name

    on  table_name (column_name[,column_name]....)

    [with fillfactor=X]

    其中clustered为聚集索引,nonclustered为非聚集索引

    unique指定唯一索引

    fillfactor表示填充因子,指定1~100的值,该值指示索引页填满的空间所占的百分比

    例如:

    --创建学生表的学生姓名的非聚集索引

    create nonclustered index IX_Student_StuName

     on Student(StuName)

    with fillfactor =30

    go

    --索引的应用

    select *from Student
    with (index= IX_Student_StuName)
    where StuName like '张%'

     

     

  • 相关阅读:
    【Exgcd】斩杀线计算大师
    【DP】操作集锦
    【DP】被3整除的子序列
    【DFS序】【CF-1328E】Tree Queries
    【规律】【CF1327-D】Carousel
    Luogu P4774 屠龙勇士
    LOJ 10149 凸多边形的划分
    Luogu P4036 火星人
    Luogu P3193 GT考试
    CF 986C AND Graph
  • 原文地址:https://www.cnblogs.com/sujulin/p/6552732.html
Copyright © 2011-2022 走看看