sql2005,with check option 在视图与基表中的用法

来源:互联网 发布:淘宝原味胖次 编辑:程序博客网 时间:2024/06/05 08:19
create  view    stu8_view8    as   select    *  from   stu8  where  ssex ='女'
create  view    stu8_view9    as   select    *  from   stu8  where  ssex ='女'
create   view     stu8_view10   as   select  *  from   stu8    where   ssex = '女'
with  check  option
create   view     stu8_view11   as   select  *  from   stu8    where   ssex = '女'

with  check  option

------------------------------

select   *  into    stu8_view8_view9_view10_view11   from (
select  a.s#,a.sname,a.sage,a.ssex  from    stu8   a
inner   join     stu8_view8  b on  b.s#=a.s#
inner  join    stu8_view9    c  on  c.s#=a.s#
inner  join   stu8_view10    d    on    d.s#=a.s#
inner join    stu8_view11   e   on     e.s#=  a.s#
and   b.s#=a.s#  and   c.s#=a.s#   and  d.s#=a.s#    and   e.s#=a.s#   
and  c.s#=b.s#   and   d.s#=b.s#   and   e.s#=b.s#
and     d.s#=c.s#  and     e.s# = c.s#
and      e.s# = e.s#             )  f        ----t  插入新表成功

---------------------------------

select   *  from   stu8_view8_view9_view10_view11     -----s#    42   45    17   23   11  
drop table  stu8_view8_view9_view10_view11
-----------
select    *  from    stu8 
select    *  from  stu8_view8
select   *  from   stu8_view9
select    *  from  stu8_view10
select   *  from   stu8_view11
---------------

update 作用于where=条件的字段
update   stu8    set  ssex ='男'    where    s#=42

---stu8 记录改变; stu8_view8, stu8_view9, stu8_view10, stu8_view11少了一条记录

update   stu8    set  ssex ='女'    where    s#=3

----stu8 记录改变; stu8_view8, stu8_view9, stu8_view10, stu8_view11多了一条记录

update  stu8_view8    set  ssex ='男'    where    s#=11  ----t        stu8_view8  没有WITH CHECK OPTION 约束

----stu8 记录改变; stu8_view8, stu8_view9, stu8_view10, stu8_view11少了一条记录

update  stu8_view10   set  ssex ='男'    where    s#=11   ----f      stu8_view10  WITH CHECK OPTION 约束

-------stu8 , stu8_view8, stu8_view9, stu8_view10, stu8_view11记录不改变

Msg 550, Level 16, State 1, Line 1
试图进行的插入或更新已失败,原因是目标视图或者目标视图所跨越的某一视图指定了 WITH CHECK OPTION,而该操作的一个或多个结果行又不符合 CHECK OPTION 约束的条件。
语句已终止。

-----------

update 作用于不是 where=条件  的字段

update  stu8      set  sname =null     where    s#=11       

-------stu8 , stu8_view8, stu8_view9, stu8_view10, stu8_view11记录改变

update  stu8_view8      set  sname ='李正'     where    s#=17  

-------stu8 , stu8_view8, stu8_view9, stu8_view10, stu8_view11记录改变

update  stu8_view10      set  sname =null     where    s#=42 

-------stu8 , stu8_view8, stu8_view9, stu8_view10, stu8_view11记录改变

---------------------------------

insert   

insert   into     stu8    values('XXXX','1994-5-1','男',50)  

-------stu8 多了一条记录, stu8_view8, stu8_view9, stu8_view10, stu8_view11记录不改变

insert   into     stu8_view8    values('XXXX','1994-5-1','男',51)    ----t      stu8_view8   没有WITH CHECK OPTION 约束

-------stu8 多了一条记录, stu8_view8, stu8_view9, stu8_view10, stu8_view11记录不改变

insert   into     stu8_view10    values('XXXX','1994-5-1','男',52) ----f      stu8_view10  WITH CHECK OPTION 约束

------Msg 550, Level 16, State 1, Line 1
试图进行的插入或更新已失败,原因是目标视图或者目标视图所跨越的某一视图指定了 WITH CHECK OPTION,而该操作的一个或多个结果行又不符合 CHECK OPTION 约束的条件。
语句已终止。
------stu8 , stu8_view8, stu8_view9, stu8_view10, stu8_view11记录不改变

insert   into     stu8    values('XXXX','1994-5-1','女',53)

-------stu8 , stu8_view8, stu8_view9, stu8_view10, stu8_view11均多了一条记录

insert   into     stu8_view8    values('XXXX','1994-5-1','女',54)  

-------stu8 , stu8_view8, stu8_view9, stu8_view10, stu8_view11均多了一条记录

insert   into     stu8_view10    values('XXXX','1994-5-1','女',55)

-------stu8 , stu8_view8, stu8_view9, stu8_view10, stu8_view11均多了一条记录

delete

delete   stu8   where   s#=42 

-------stu8 , stu8_view8, stu8_view9, stu8_view10, stu8_view11均少了一条记录

delete   stu8_view8   where   s#=11

-------stu8 , stu8_view8, stu8_view9, stu8_view10, stu8_view11均少了一条记录

delete   stu8_view10   where   s#=17  

-------stu8 , stu8_view8, stu8_view9, stu8_view10, stu8_view11均少了一条记录

-----XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

以上是  where=  条件 部分,还有 where>  、<  的情况、

   

















































0 0
原创粉丝点击