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 '张%'

     

     

  • 相关阅读:
    670. Maximum Swap
    653. Two Sum IV
    639. Decode Ways II
    636. Exclusive Time of Functions
    621. Task Scheduler
    572. Subtree of Another Tree
    554. Brick Wall
    543. Diameter of Binary Tree
    535. Encode and Decode TinyURL
    博客园自定义背景图片
  • 原文地址:https://www.cnblogs.com/sujulin/p/6552732.html
Copyright © 2011-2022 走看看