oracle 几个常用语句

1、复制表结构

     create table table_name as (select * from table_name where 1 = 2 )

2、插入表的数据

     insert into table_name (select * from table_name)

 1 + 2 :   create table table_name as (select * from table_name )

查看表结构:在sqlplus中,desc /describe table_name

                  (describe /desc is sqlplus command but sql statement)

3、删除表中完全重复数据。

  先建立一个临时表,将数据放进去。

  create table temp as

   (

    select  distinct  *  from table_data

   );

    然后查询一下是否操作成功,事务是否提交。

    注意:修改表后,数据会放到缓存中,并没有写入,一样也可以查询出对表的修改。

   select count(*) from temp;

  

   截断原来的表, 再将数据插入即可。

   truncate table table_data;
   insert into table_data (select * from temp);

   提交事务。

   将临时表删除。 drop table temp;

 

4、删除某个字段重复数据。


这个在网上有很多相关的解决方法,较常见的有
delete from table where id not in (select min(id) from table group by name)
delete from table where field in (select field from table group by field having count(*) > 1)

上面的方法在删除小数量级的数据时还有用,当一旦处理的数据是几十万或者更多时就出问题了,一般的机器估计一运行就马上给费了。其实稍有点常识的算一算就知道这样的语句会有多大的运算量了,它的运算量至少是以乘方的形式递增的,想想就恐怖。

我在这里主要是要给出对于大数量级的表的重复数据删除的解决方案,其实也很简单,也是利用了一个过渡表来实现
insert in tabletemp select * from table
delete from table as a where a.id > (select min(b.id) from table1 as b where b.field=a.field)
drop table tabletemp
这样利用了数据库的索引的优势,大大的减少运算量

 

5UNION的一个限制是两个 SQL 语句所产生的栏位需要是同样的资料种类。另外,当我们用 UNION这个指令时,我们只会看到不同的资料值 (类似 SELECT DISTINCT) union只是将两个结果联结起来一起显示,并不是联结两个表 。

  UNION ALL 这个指令的目的也是要将两个 SQL 语句的结果合并在一起。 UNION ALL UNION 不同之处在于 UNION ALL 会将每一笔符合条件的资料都列出来,无论资料值有无重复。

 

6、联合批量更新:

     update college set(address,postcode,email,phone,fax,website,linkman)=
            (select address,postcode,email,phone,fax,website,linkman from college_temp
            where college.yxdm = college_temp.yxdm);

 

猜你喜欢

转载自yaweidai.iteye.com/blog/1408216