如题,谢谢!

解决方案 »

  1.   


    --级联引用完整性约束create table ta( a int primary key)
    insert into ta select 1 union select 2 union select 3
    go
    create table tb(b int,constraint fk_tb foreign key (b) references ta(a))
    insert into tb select 1 union select 2
    go
    select * from ta
    select * from tb
    go
    update ta set a=8 where a=1--impossible
    go
    alter table tb drop constraint fk_tb
    go
    alter table tb 
    add constraint fk_tb foreign key (b) references ta(a) on update cascade
    go
    update ta set a=8 where a=1--Nothing is impossible
    go
    select * from ta
    select * from tb
      

  2.   

    set nocount on
    if object_id('tb') is not null drop table tb
    if object_id('ta') is not null drop table ta
    create table ta( a int primary key)
    insert into ta select 1 union select 2
    go
    create table tb(b int,constraint fk_tb foreign key (b) references ta(a))
    insert into tb select 1 union select 2
    go
    select * from ta
    select * from tb
    /*
    a
    1
    2b
    1
    2
    */
    go
    update ta set a=8 where a=1--impossible
    go
    alter table tb drop constraint fk_tb
    go
    alter table tb add constraint fk_tb foreign key (b) references ta(a) on update cascade
    go
    update ta set a=8 where a=1--Nothing is impossible
    go
    select * from ta
    select * from tb
    /*
    a
    2
    8b
    8
    2
    */
    set nocount off
      

  3.   

    LS的,怎么删除ta上的主键???