﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-JAVA聚会-随笔分类-oracle</title><link>http://www.blogjava.net/kelly/category/49860.html</link><description>我新开了淘宝，欢迎光临我的小店：http://shop58314633.taobao.com</description><language>zh-cn</language><lastBuildDate>Mon, 02 Dec 2013 05:23:13 GMT</lastBuildDate><pubDate>Mon, 02 Dec 2013 05:23:13 GMT</pubDate><ttl>60</ttl><item><title> oracle 快速删除大批量数据方法（全部删除，条件删除，删除大量重复记录）-转</title><link>http://www.blogjava.net/kelly/archive/2013/12/02/407090.html</link><dc:creator>kelly</dc:creator><author>kelly</author><pubDate>Mon, 02 Dec 2013 02:49:00 GMT</pubDate><guid>http://www.blogjava.net/kelly/archive/2013/12/02/407090.html</guid><wfw:comment>http://www.blogjava.net/kelly/comments/407090.html</wfw:comment><comments>http://www.blogjava.net/kelly/archive/2013/12/02/407090.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/kelly/comments/commentRss/407090.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/kelly/services/trackbacks/407090.html</trackback:ping><description><![CDATA[<p style="text-align: center; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px"><strong><span style="font-size: large">全部删除</span></strong></p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">如果是删除某个表的所有数据，并且不需要回滚，使用 TRUNCATE 就ok了。关于Trancate 参见<a style="color: rgb(51,102,153); text-decoration: none" href="http://blog.csdn.net/gnolhh168/archive/2011/05/24/6442561.aspx">这里http://blog.csdn.net/gnolhh168/archive/2011/05/24/6442561.aspx</a></p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">SQL&gt; truncate &nbsp; table &nbsp; table_name;</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">&nbsp;</p>
<p style="text-align: center; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px"><span style="font-size: large"><strong>条件删除</strong></span></p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">如果删除数据有条件，如 delete from tablename where col1 = 'lucy';这时除了加索引外, 你可以删除时加NO &nbsp; LOGGING选项,不写日志加快删除速度</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">&nbsp;</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">引用某人的一句话&#8220;几千万条记录的表都不分区，明显有问题嘛。Oracle的技术支持工程师建议，2,000,000条以上记录的表，应该考虑分区，你完全可以按照时间为维度来建表，每个月的数据存放在一个分区表中，以后要删除一个月的数据，直接truncate &nbsp; table即可，不记录日志，速度很快。&#8221;</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">&nbsp;</p>
<p style="text-align: center; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px"><span style="font-size: large"><strong>&nbsp;删除大量重复记录</strong></span></p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">《转》做项目的时候，一位同事导数据的时候，不小心把一个表中的数据全都搞重了，也就是说，这个表里所有的记录都有一条重复的。这个表的数据是千万级的，而且是生产系统。也就是说，不能把所有的记录都删除，而且必须快速的把重复记录删掉。</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">对此，总结了一下删除重复记录的方法，以及每种方法的优缺点。</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">为了陈诉方便，假设表名为Tbl，表中有三列col1，col2，col3，其中col1，col2是主键，并且，col1，col2上加了索引。</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">1、通过创建临时表</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">可以把数据先导入到一个临时表中，然后删除原表的数据，再把数据导回原表，SQL语句如下：</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">&nbsp;</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">creat table tbl_tmp (select distinct* from tbl);</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">truncate table tbl;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //清空表记录i</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">nsert into tbl select * from tbl_tmp;//将临时表中的数据插回来。</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">&nbsp;</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">这种方法可以实现需求，但是很明显，对于一个千万级记录的表，这种方法很慢，在生产系统中，这会给系统带来很大的开销，不可行。</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">2、利用rowid</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">在oracle中，每一条记录都有一个rowid，rowid在整个数据库中是唯一的，rowid确定了每条记录是oracle中的哪一个数据文件、块、行上。在重复的记录中，可能所有列的内容都相同，但rowid不会相同。SQL语句如下：</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">&nbsp;</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">delete from tbl where rowid in (select a.rowid</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">from tbl a,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tbl b</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">where a.rowid&gt;b.rowid and a.col1=b.col1 and a.col2 = b.col2)&nbsp;</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">&nbsp;</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">如果已经知道每条记录只有一条重复的，这个sql语句适用。但是如果每条记录的重复记录有N条，这个N是未知的，就要考虑适用下面这种方法了。</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">3、利用max或min函数</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">这里也要使用rowid，与上面不同的是结合max或min函数来实现。SQL语句如下</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">&nbsp;</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">delete from tbl a</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">where rowid not in (</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; select max(b.rowid)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;from tbl b</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; where a.col1=b.col1 and a.col2 = b.col2);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //这里max使用min也可以&nbsp;</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">&nbsp;</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">或者用下面的语句</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">&nbsp;</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">delete from tbl awhere rowid&lt;(</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; select max(b.rowid)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;from tbl b</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;where a.col1=b.col1 and a.col2 = b.col2);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //这里如果把max换成min的话，前面的where子句中需要把"&lt;"改为"&gt;"&nbsp;</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">&nbsp;</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">跟上面的方法思路基本是一样的，不过使用了group by，减少了显性的比较条件，提高效率。SQL语句如下：</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">&nbsp;</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">deletefrom tbl where rowid not in (</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; select max(rowid)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; from tbl tgroup by t.col1, t.col2);</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">&nbsp;</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px"><span style="background-color: rgb(255,0,0)">delete from tbl where (col1, col2)&nbsp; in (</span></p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px"><span style="background-color: rgb(255,0,0)">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; select col1,col2</span></p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px"><span style="background-color: rgb(255,0,0)">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; from tblgroup bycol1,col2havingcount(*)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &gt;1) and rowidnotin(selectnin(rowid)fromtblgroup bycol1,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; col2havingcount(*) &gt;1)&nbsp;&nbsp;&nbsp; ----？？？</span></p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">还有一种方法，对于表中有重复记录的记录比较少的，并且有索引的情况，比较适用。假定col1，col2上有索引，并且tbl表中有重复记录的记录比较少，SQL语句如下4、利用group by，提高效率</p><img src ="http://www.blogjava.net/kelly/aggbug/407090.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/kelly/" target="_blank">kelly</a> 2013-12-02 10:49 <a href="http://www.blogjava.net/kelly/archive/2013/12/02/407090.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>numtodsinterval and interval for oracle时间加减函数-转</title><link>http://www.blogjava.net/kelly/archive/2013/11/22/406656.html</link><dc:creator>kelly</dc:creator><author>kelly</author><pubDate>Fri, 22 Nov 2013 02:39:00 GMT</pubDate><guid>http://www.blogjava.net/kelly/archive/2013/11/22/406656.html</guid><wfw:comment>http://www.blogjava.net/kelly/comments/406656.html</wfw:comment><comments>http://www.blogjava.net/kelly/archive/2013/11/22/406656.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/kelly/comments/commentRss/406656.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/kelly/services/trackbacks/406656.html</trackback:ping><description><![CDATA[<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">&nbsp;</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">一、&nbsp; 语法：NUMTOYMINTERVAL ( n , 'char_expr' )<br />&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;char_expr：日期描述，可以是YEAR和MONTH；<br /><br />&nbsp;&nbsp;作用：可以将数字转换成相应的日期单位时间<br /><br />&nbsp;&nbsp;比如：NUMTOYMINTERVAL ( 1, 'MONTH' ) 表示一个月，注意：此时跟add_months有点区别，后续有例子会讲到。<br />&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;NUMTOYMINTERVAL ( 1, 'YEAR' ) 表示一年</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">&nbsp;</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">对于day、hour、minute、second使用的是numtodsinterval函数，方法和numtoyminterval一样。后面可以跟变量</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">二、interval后面只能用数字</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">&nbsp;</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">下面是举例：</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">SQL&gt; select<span class="Apple-converted-space">&nbsp;</span><strong>add_months</strong><span class="Apple-converted-space">&nbsp;</span>(to_date('20110228','yyyymmdd'),1) from dual;<br /><br />ADD_MONTH<br />---------<br /><strong>31-MAR-11</strong><span class="Apple-converted-space">&nbsp;</span><br /><br />SQL&gt;<span class="Apple-converted-space">&nbsp;</span><br />SQL&gt; select add_months(to_date('20110228','yyyymmdd'),-1) from&nbsp; dual;<br /><br />ADD_MONTH<br />---------<br />31-JAN-11<br /><br />SQL&gt;<span class="Apple-converted-space">&nbsp;</span><br />SQL&gt;<span class="Apple-converted-space">&nbsp;</span><br />SQL&gt; select to_date('2007-02-28','yyyy-mm-dd')+<strong>numtoyminterval</strong><span class="Apple-converted-space">&nbsp;</span>(1,'month') from dual;<br /><br />TO_DATE('<br />---------<br /><strong>28-MAR-07</strong><span class="Apple-converted-space">&nbsp;</span><br /><br />SQL&gt; select to_date('2007-02-28','yyyy-mm-dd')-<strong>numtoyminterval</strong><span class="Apple-converted-space">&nbsp;</span>(1,'month') from dual;<br /><br />TO_DATE('<br />---------<br /><strong>28-JAN-07</strong></p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px"><strong><br />此时，注意跟add_months返回结果的区别。</strong></p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">&nbsp;</p>
<p style="text-align: left; widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; font: 14px/26px Arial; white-space: normal; orphans: 2; color: rgb(51,51,51); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">SQL&gt; select to_date('2007-02-28','yyyy-mm-dd')+numtodsinterval(1,'day') from dual;<br /><br />TO_DATE('<br />---------<br />01-MAR-07<br /><br /><br />SQL&gt;<span class="Apple-converted-space">&nbsp;</span><br />SQL&gt; select to_date('2007-02-28','yyyy-mm-dd')+interval '+3' hour from dual;<br /><br />TO_DATE('<br />---------<br />28-FEB-07<br /><br /><br />SQL&gt; select to_date('2007-02-28','yyyy-mm-dd')+interval '+1' month from dual;<br /><br />TO_DATE('<br />---------<br />28-MAR-07</p><img src ="http://www.blogjava.net/kelly/aggbug/406656.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/kelly/" target="_blank">kelly</a> 2013-11-22 10:39 <a href="http://www.blogjava.net/kelly/archive/2013/11/22/406656.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>ora-01033:oracle initialization or shutdown in progress 解决方法</title><link>http://www.blogjava.net/kelly/archive/2013/04/08/397524.html</link><dc:creator>kelly</dc:creator><author>kelly</author><pubDate>Mon, 08 Apr 2013 06:53:00 GMT</pubDate><guid>http://www.blogjava.net/kelly/archive/2013/04/08/397524.html</guid><wfw:comment>http://www.blogjava.net/kelly/comments/397524.html</wfw:comment><comments>http://www.blogjava.net/kelly/archive/2013/04/08/397524.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/kelly/comments/commentRss/397524.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/kelly/services/trackbacks/397524.html</trackback:ping><description><![CDATA[<span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; display: inline !important; font: 12pt/21px tahoma, helvetica, arial; white-space: normal; orphans: 2; float: none; color: rgb(69,69,69); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">今天使用Oracle遇到了这个问题ora-01033:oracle initialization or shutdown in progress，经过分析研究终于解决了，记下来</span><span style="font-size: 12pt">.</span><br /><span style="font-size: 12pt">我遇到的错误是</span><strong style="font-size: 12pt">ORA-00600: 内部错误代码，参数: [kcratr1_lostwrt]</strong><br style="font-size: 12pt" /><span style="font-size: 12pt"><br /></span><span style="color: red; font-size: 12pt">分析过程：<br /></span><span style="font-size: 12pt"><br />运行中输入cmd，、输入sqlplus&nbsp;/nolog，然后用&nbsp;用sys登陆。</span><br />
<p>Microsoft Windows [版本 5.2.3790]<br />(C) 版权所有 1985-2003 Microsoft Corp.</p>
<p>E:\Documents and Settings\Administrator&gt;sqlplus /nolog</p>
<p>SQL*Plus: Release 9.2.0.1.0 - Production on 星期一 4月 8 14:20:05 2</p>
<p>Copyright (c) 1982, 2002, Oracle Corporation.&nbsp; All rights reserved.<br />SQL&gt; connect sys/sys as sysdba;<br />已连接。<br />SQL&gt; shutdown normal<br />ORA-01109: 数据库未打开</p>
<p><br />已经卸载数据库。<br />ORACLE 例程已经关闭。<br />SQL&gt; startup mount<br />ORACLE 例程已经启动。</p>
<p align="left">Total System Global Area&nbsp; 135338868 bytes<br />Fixed Size&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 453492 bytes<br />Variable Size&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 109051904 bytes<br />Database Buffers&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 25165824 bytes<br />Redo Buffers&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 667648 bytes<br />数据库装载完毕。<br /><br /><span style="line-height: 21px; widows: 2; text-transform: none; background-color: rgb(255,255,255); font-variant: normal; font-style: normal; text-indent: 0px; letter-spacing: normal; display: inline !important; font-family: tahoma, helvetica, arial; white-space: normal; orphans: 2; float: none; color: rgb(69,69,69); font-size: 14px; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">SQL&gt; alter database open;<br />出现以下错误：<br /><strong>ORA-00600: 内部错误代码，参数: [kcratr1_lostwrt]<br /><br /><strong><font color="#454545">出现该错误是因为系统强制关机造成的!<br />症状为数据库无法打开!</font></strong><br />解决这个错误：<br /></strong>SQL&gt; shutdown normal<br />ORA-01109: 数据库未打开 </p>
<p><br />已经卸载数据库。<br />ORACLE 例程已经关闭。<br />SQL&gt; startup mount<br />ORACLE 例程已经启动。</p>
<p align="left">Total System Global Area 135338868 bytes<br />Fixed Size 453492 bytes<br />Variable Size 109051904 bytes<br />Database Buffers 25165824 bytes<br />Redo Buffers 667648 bytes<br />数据库装载完毕。<br /><br /><span style="color: red">SQL&gt;recover database;</span><br /><span style="color: red">完成介质恢复</span><br /><span style="widows: 2; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; display: inline !important; font: 14px/21px tahoma, helvetica, arial; white-space: normal; orphans: 2; float: none; color: rgb(69,69,69); word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px">SQL&gt; alter database open;<br /></span><br />数据库已更改<br />SQL&gt;exit;<br /><br /><br /></p>
<p align="left"><br /><br />&nbsp;</p>
<p align="left"><br style="font-size: 12pt" /><br /><br />&nbsp;</p></span><img src ="http://www.blogjava.net/kelly/aggbug/397524.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/kelly/" target="_blank">kelly</a> 2013-04-08 14:53 <a href="http://www.blogjava.net/kelly/archive/2013/04/08/397524.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Linux下启动Oracle服务和监听程序</title><link>http://www.blogjava.net/kelly/archive/2011/09/26/359505.html</link><dc:creator>kelly</dc:creator><author>kelly</author><pubDate>Mon, 26 Sep 2011 06:28:00 GMT</pubDate><guid>http://www.blogjava.net/kelly/archive/2011/09/26/359505.html</guid><wfw:comment>http://www.blogjava.net/kelly/comments/359505.html</wfw:comment><comments>http://www.blogjava.net/kelly/archive/2011/09/26/359505.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/kelly/comments/commentRss/359505.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/kelly/services/trackbacks/359505.html</trackback:ping><description><![CDATA[<span style="line-height: 19px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px" class="Apple-style-span">
<p style="margin: 0px 0px 14px" id="fp"><span style="line-height: 19px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px">
</span></p><p style="margin: 0px 0px 14px" id="fp">oracle数据库是重量级的，其管理非常复杂，将其在linux平台上的启动和关闭步骤整理一下。</p>
<p style="margin: 0px 0px 14px">安装完毕oracle以后，需要创建oracle系统用户，并在/home/oracle下面的.bash_profile添加几个环境变量：ORACLE_SID,ORACLE_BASE,ORACLE_HOME。比如：</p>
<p style="margin: 0px 0px 14px">export ORACLE_SID=test&nbsp; export ORACLE_BASE=oracle_install_dir export ORACLE_HOME=xxx</p>
<p style="margin: 0px 0px 14px">启动步骤：注意$代表shell命令提示符，这里的oracle是9.0以上版本。<br /></p>
<p>切换到oracle用户下面：<br />$ su - oracle</p>
<p>$ sqlplus / nolog</p>
<p>sql&gt; conn / as sysdba</p>
<p>sql&gt; startup (启动数据库，一般不需要加参数，只要设置好环境变量）</p>
<p>sql&gt;shutdown （关闭数据库其参数 ：shutdown有四个参数，四个参数的含义如下：<br />Normal&nbsp;需要等待所有的用户断开连接<br />Immediate&nbsp;等待用户完成当前的语句<br />Transactional&nbsp;等待用户完成当前的事务<br />Abort&nbsp;不做任何等待，直接关闭数据库<br />normal需要在所有连接用户断开后才执行关闭数据库任务，所以有的时候看起来好象命令没有运行一样！在执行这个命令后不允许新的连接<br />immediate在用户执行完正在执行的语句后就断开用户连接，并不允许新用户连接。<br />transactional&nbsp;在拥护执行完当前事物后断开连接，并不允许新的用户连接数据库。<br />abort&nbsp;执行强行断开连接并直接关闭数据库。<br /></p>
<p>sql&gt; quit (退出sql模式)<br /><br />启动监听器：<br /></p>
<p>$ su - oracle<br />然后切换到$oracle_home/bin下面，执行下面命令</p>
<p>$lsnrctl start (启动监听器）<br />$lsnrctl status (查看监听器启动状态)<br />$lsnrctl stop(关闭监听器)</p>
<p>&nbsp;</p>
<p style="margin: 0px 0px 14px">经常遇到的问题：</p>
<p style="margin: 0px 0px 14px">1）权限问题，解决方法，切换到oracle用户；</p>
<p style="margin: 0px 0px 14px">2）没有关闭监听器 ，解决方法：关闭监听器</p>
<p style="margin: 0px 0px 14px">3）有oracle实例没有关闭，解决办法：关闭oracle实例</p>
<p style="margin: 0px 0px 14px">4）环境变量设置不全，解决办法：修改环境变量</p></span>
<h2 style="line-height: normal; margin: 0px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: #444444; font-size: 12px"><a id="viewpost1_TitleUrl" class="singleposttitle" href="http://www.cnitblog.com/wufajiaru/archive/2008/02/20/39978.html"></a></h2><img src ="http://www.blogjava.net/kelly/aggbug/359505.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/kelly/" target="_blank">kelly</a> 2011-09-26 14:28 <a href="http://www.blogjava.net/kelly/archive/2011/09/26/359505.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>转：oracle备份数据库命令</title><link>http://www.blogjava.net/kelly/archive/2011/09/26/359504.html</link><dc:creator>kelly</dc:creator><author>kelly</author><pubDate>Mon, 26 Sep 2011 06:27:00 GMT</pubDate><guid>http://www.blogjava.net/kelly/archive/2011/09/26/359504.html</guid><wfw:comment>http://www.blogjava.net/kelly/comments/359504.html</wfw:comment><comments>http://www.blogjava.net/kelly/archive/2011/09/26/359504.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/kelly/comments/commentRss/359504.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/kelly/services/trackbacks/359504.html</trackback:ping><description><![CDATA[<span style="line-height: 20px; font-family: Arial; letter-spacing: 1px; color: #989a59; font-size: 12px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px" class="Apple-style-span"><span style="line-height: 20px; font-family: Arial; letter-spacing: 1px; color: #989a59; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px">导出 exp userid/pwdid@servername file=dataname.dmp<br style="line-height: normal" />导入 imp username/pwdid@servername file=dataname.dmp<br style="line-height: normal" />Oracle数据导入导出imp/exp就相当于oracle数据还原与备份。exp命令可以把数据从远程数据库服务器导出到本地的dmp文 件，imp命令可以把dmp文件从本地导入到远处的数据库服务器中。 利用这个功能可以构建两个相同的数据库，一个用来测试，一个用来正式使用。<br style="line-height: normal" /><br style="line-height: normal" />执行环境：可以在SQLPLUS.EXE或者DOS（命令行）中执行，<br style="line-height: normal" />DOS中可以执行时由于 在oracle 8i 中 安装目录\ora81\BIN被设置为全局路径，<br style="line-height: normal" />该目录下有EXP.EXE与IMP.EXE文件被用来执行导入导出。<br style="line-height: normal" />oracle用java编写，SQLPLUS.EXE、EXP.EXE、IMP.EXE这两个文件有可能是被包装后的类文件。<br style="line-height: normal" />SQLPLUS.EXE调用EXP.EXE、IMP.EXE所包裹的类，完成导入导出功能。<br style="line-height: normal" /><br style="line-height: normal" />下面介绍的是导入导出的实例。<br style="line-height: normal" />数据导出：<br style="line-height: normal" />1 将数据库TEST完全导出,用户名system 密码manager 导出到D:\daochu.dmp中<br style="line-height: normal" />&nbsp;&nbsp; exp&nbsp;<a style="line-height: 20px; font-family: Verdana, Arial, Helvetica, sans-serif; color: #989a59; font-size: 12px; font-weight: normal; text-decoration: none" href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#115;&#121;&#115;&#116;&#101;&#109;&#47;&#109;&#97;&#110;&#97;&#103;&#101;&#114;&#64;&#84;&#69;&#83;&#84;"><u style="line-height: normal">system/manager@TEST</u></a>&nbsp;file=d:\daochu.dmp full=y<br style="line-height: normal" />2 将数据库中system用户与sys用户的表导出<br style="line-height: normal" />&nbsp;&nbsp; exp&nbsp;<a style="line-height: 20px; font-family: Verdana, Arial, Helvetica, sans-serif; color: #989a59; font-size: 12px; font-weight: normal; text-decoration: none" href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#115;&#121;&#115;&#116;&#101;&#109;&#47;&#109;&#97;&#110;&#97;&#103;&#101;&#114;&#64;&#84;&#69;&#83;&#84;"><u style="line-height: normal">system/manager@TEST</u></a>&nbsp;file=d:\daochu.dmp owner=(system,sys)<br style="line-height: normal" />3 将数据库中的表inner_notify、notify_staff_relat导出<br style="line-height: normal" />&nbsp;&nbsp;&nbsp; exp&nbsp;<a style="line-height: 20px; font-family: Verdana, Arial, Helvetica, sans-serif; color: #989a59; font-size: 12px; font-weight: normal; text-decoration: none" href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#97;&#105;&#99;&#104;&#97;&#110;&#110;&#101;&#108;&#47;&#97;&#105;&#99;&#104;&#97;&#110;&#110;&#101;&#108;&#64;&#84;&#69;&#83;&#84;&#68;&#66;&#50;"><u style="line-height: normal">aichannel/aichannel@TESTDB2</u></a>&nbsp;file= d:\data\newsmgnt.dmp tables=(inner_notify,notify_staff_relat)&nbsp;<br style="line-height: normal" /><br style="line-height: normal" />4 将数据库中的表table1中的字段filed1以"00"打头的数据导出<br style="line-height: normal" />&nbsp;&nbsp; exp&nbsp;<a style="line-height: 20px; font-family: Verdana, Arial, Helvetica, sans-serif; color: #989a59; font-size: 12px; font-weight: normal; text-decoration: none" href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#115;&#121;&#115;&#116;&#101;&#109;&#47;&#109;&#97;&#110;&#97;&#103;&#101;&#114;&#64;&#84;&#69;&#83;&#84;"><u style="line-height: normal">system/manager@TEST</u></a>&nbsp;file=d:\daochu.dmp tables=(table1) query=\" where filed1 like '00%'\"<br style="line-height: normal" /><br style="line-height: normal" />上面是常用的导出，对于压缩，既用winzip把dmp文件可以很好的压缩。<br style="line-height: normal" />也可以在上面命令后面 加上 compress=y 来实现。
<p style="line-height: normal">数据的导入<br style="line-height: normal" />1 将D:\daochu.dmp 中的数据导入 TEST数据库中。<br style="line-height: normal" />&nbsp;&nbsp; imp&nbsp;<a style="line-height: 20px; font-family: Verdana, Arial, Helvetica, sans-serif; color: #989a59; font-size: 12px; font-weight: normal; text-decoration: none" href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#115;&#121;&#115;&#116;&#101;&#109;&#47;&#109;&#97;&#110;&#97;&#103;&#101;&#114;&#64;&#84;&#69;&#83;&#84;"><u style="line-height: normal">system/manager@TEST</u></a>&nbsp;file=d:\daochu.dmp<br style="line-height: normal" />&nbsp;&nbsp; imp&nbsp;<a style="line-height: 20px; font-family: Verdana, Arial, Helvetica, sans-serif; color: #989a59; font-size: 12px; font-weight: normal; text-decoration: none" href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#97;&#105;&#99;&#104;&#97;&#110;&#110;&#101;&#108;&#47;&#97;&#105;&#99;&#104;&#97;&#110;&#110;&#101;&#108;&#64;&#72;&#85;&#83;&#84;"><u style="line-height: normal">aichannel/aichannel@HUST</u></a>&nbsp;full=y file=file= d:\data\newsmgnt.dmp ignore=y&nbsp;<br style="line-height: normal" />&nbsp;&nbsp; 上面可能有点问题，因为有的表已经存在，然后它就报错，对该表就不进行导入。<br style="line-height: normal" />&nbsp;&nbsp; 在后面加上 ignore=y 就可以了。<br style="line-height: normal" />2 将d:\daochu.dmp中的表table1 导入<br style="line-height: normal" />imp&nbsp;<a style="line-height: 20px; font-family: Verdana, Arial, Helvetica, sans-serif; color: #989a59; font-size: 12px; font-weight: normal; text-decoration: none" href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#115;&#121;&#115;&#116;&#101;&#109;&#47;&#109;&#97;&#110;&#97;&#103;&#101;&#114;&#64;&#84;&#69;&#83;&#84;"><u style="line-height: normal">system/manager@TEST</u></a>&nbsp;file=d:\daochu.dmp tables=(table1)&nbsp;<br style="line-height: normal" /><br style="line-height: normal" />基本上上面的导入导出够用了。不少情况要先是将表彻底删除，然后导入。<br style="line-height: normal" /><br style="line-height: normal" />注意：<br style="line-height: normal" />操作者要有足够的权限，权限不够它会提示。<br style="line-height: normal" />数据库时可以连上的。可以用tnsping TEST 来获得数据库TEST能否连上。</p>
<p style="line-height: normal">附录一：<br style="line-height: normal" />给用户增加导入数据权限的操作<br style="line-height: normal" />第一,启动sql*puls<br style="line-height: normal" />第二，以system/manager登陆<br style="line-height: normal" />第三，create user 用户名 IDENTIFIED BY 密码 （如果已经创建过用户，这步可以省略）<br style="line-height: normal" />第四，GRANT CREATE USER,DROP USER,ALTER USER ,CREATE ANY VIEW ,<br style="line-height: normal" />&nbsp;&nbsp; DROP ANY VIEW,EXP_FULL_DATABASE,IMP_FULL_DATABASE,<br style="line-height: normal" />&nbsp;&nbsp;&nbsp; DBA,CONNECT,RESOURCE,CREATE SESSION TO 用户名字&nbsp;<br style="line-height: normal" />第五, 运行-cmd-进入dmp文件所在的目录,&nbsp;<br style="line-height: normal" />&nbsp;&nbsp;&nbsp; imp userid=system/manager full=y file=*.dmp<br style="line-height: normal" />&nbsp;&nbsp;&nbsp;&nbsp; 或者 imp userid=system/manager full=y file=filename.dmp</p>
<p style="line-height: normal">执行示例:<br style="line-height: normal" />F:\Work\Oracle_Data\backup&gt;imp userid=test/test full=y file=inner_notify.dmp</p>
<p style="line-height: normal">屏幕显示<br style="line-height: normal" />Import: Release 8.1.7.0.0 - Production on 星期四 2月 16 16:50:05 2006<br style="line-height: normal" />(c) Copyright 2000 Oracle Corporation. All rights reserved.</p>
<p style="line-height: normal">连接到: Oracle8i Enterprise Edition Release 8.1.7.0.0 - Production<br style="line-height: normal" />With the Partitioning option<br style="line-height: normal" />JServer Release 8.1.7.0.0 - Production</p>
<p style="line-height: normal">经由常规路径导出由EXPORT:V08.01.07创建的文件<br style="line-height: normal" />已经完成ZHS16GBK字符集和ZHS16GBK NCHAR 字符集中的导入<br style="line-height: normal" />导出服务器使用UTF8 NCHAR 字符集 (可能的ncharset转换)<br style="line-height: normal" />. 正在将AICHANNEL的对象导入到 AICHANNEL<br style="line-height: normal" />. . 正在导入表&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "INNER_NOTIFY"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4行被导入<br style="line-height: normal" />准备启用约束条件...<br style="line-height: normal" />成功终止导入，但出现警告。</p>
<p style="line-height: normal"><br style="line-height: normal" />附录二：<br style="line-height: normal" />Oracle 不允许直接改变表的拥有者, 利用Export/Import可以达到这一目的.<br style="line-height: normal" />先建立import9.par,<br style="line-height: normal" />然后，使用时命令如下：imp parfile=/filepath/import9.par<br style="line-height: normal" />例 import9.par 内容如下：<br style="line-height: normal" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FROMUSER=TGPMS&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TOUSER=TGPMS2&nbsp;&nbsp;&nbsp;&nbsp; （注：把表的拥有者由FROMUSER改为TOUSER，FROMUSER和TOUSER的用户可以不同）&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br style="line-height: normal" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ROWS=Y<br style="line-height: normal" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; INDEXES=Y<br style="line-height: normal" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GRANTS=Y<br style="line-height: normal" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CONSTRAINTS=Y<br style="line-height: normal" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BUFFER=409600<br style="line-height: normal" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; file==/backup/ctgpc_20030623.dmp<br style="line-height: normal" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; log==/backup/import_20030623.log</p></span></span><span style="line-height: normal; font-family: Arial; letter-spacing: 1px; color: #989a59; font-size: 12px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px" class="Apple-style-span">经验证，属合格产品！</span><img src ="http://www.blogjava.net/kelly/aggbug/359504.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/kelly/" target="_blank">kelly</a> 2011-09-26 14:27 <a href="http://www.blogjava.net/kelly/archive/2011/09/26/359504.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>