select *
from sc_tm_warehouse w
where w.sc_tm_warehouse_id in (1020898,1022938)
for update;我要将PK键sc_tm_warehouse_id=1020898,1022938,这两条数据互换,但执行时报违反唯一约束PK
请问要怎么操作呢?谢谢大家了
这2条数据都有其他表关联外键的。

解决方案 »

  1.   

    先修改第一条ID为一个不存在的ID:XXXX,主表,从表都要改.再把第二条的改成第一条的ID值.
    再把那个修改的XXXX改成第二条的值.
      

  2.   

    楼上说的没错,一般来说,俺都是使用Sqldevelop的form工具直接修改,使用SQL语句比较麻烦
      

  3.   

    从表太多了,改起来太麻烦了。
    这是一个仓库(sc_tm_warehouse ),有门店,库存等很多表相关联。
      

  4.   

    select * into temp from a where a.id in(1020898,1022938)update a
    set a.x=b.x
    from a, temp b
    where a.id<>b.id and a.id=1020898update a
    set a.x=b.x
    from a, temp b
    where a.id<>b.id and a.id=1022938drop table temp
      

  5.   

    select * into temp from a where a.id in(1020898,1022938)update a
    set a.x=b.x
    from a, temp b
    where a.id<>b.id and a.id=1020898update a
    set a.x=b.x
    from a, temp b
    where a.id<>b.id and a.id=1022938drop table temp
    这样的话,只要改主表就可以了,从表就不用动了.
    如果你的表设计是按规范设计的话.
      

  6.   

    create table t_pk(id number,name varchar2(10),age number);
    create table t_fk(id number,class varchar2(10),fid number);
    alter table t_pk add constraint t_pk1 primary key (id);
    alter table t_fk add constraint t_pk2 primary key (id);
    alter table t_fk add constraint t_pk_fk foreign key (fid) references t_pk(id);insert into t_pk values(1,'zhangsan',10);
    insert into t_pk values(2,'lisi',20);
    commit;
    insert into t_fk values(100,'class1',1);
    insert into t_fk values(200,'class2',2);
    commit;--1、禁掉外键:
    alter table t_fk disable constraint t_pk_fk;
    --2、备份
    create table t_pk_bak as select * from t_pk where id in(1,2);
    --3、删除数据
    delete from t_pk where  id in(1,2);
    --4、改变PK还原数据
    insert into t_pk select decode(id,1,2,1),name,age from t_pk_bak ;
    --5、还原外键
    alter TABLE t_fk enable constraint t_pk_fk;
    --6、drop备份
    drop table t_pk_bak;
      

  7.   

    form工具???请写下操作步骤好么。。没用过啊