现有一个test表,表中数据如下:
name      password
---------- ----------
1          1
1          1
1          1
1          1
怎么样删除表中重复的行,只留一行。我使用的是Oracle 9i 数据库。
如果表中有N行重复的记录就删除N-1行。sql语句怎么写?稍加解释下?谢谢!

解决方案 »

  1.   


    delete from test t1
    where t1.rowid !=(select max(t2.rowid) from test t2 where t1.name=t2.name and t1.password = t2.password)
      

  2.   

    建议建临时表筛选有用的数据,再delete原表数据,然后将临时表数据insert回原表中
      

  3.   

    //查询出不重复的记录 存放于临时表test1中
    create table test1 as select distinct * from test;
    //删除原表
    drop table test;
    //将临时表重命名为test
    rename test1 to test;
    或者
    //查询出不重复的记录 存放于临时表test1中
    create table test1 as select distinct * from test;
    //删除原表中的所用记录
    delete from test;
    //降临时表的数据插入test表
    insert into test select * from test1;
    //删除临时表
    drop table test1;
      

  4.   

    比较复杂的方法就是采用重新建表的方式,
    select distinct * from 表名如果你要求比较严格的话,那么先建立表,然后删除原来的表,再对原来的表进行改名操作,请一定要注意另外在原来的表上进行操作的情况为:使用rowid,通过物理地址的方式,删除重复的数据而只留一条