`
远去的渡口
  • 浏览: 468722 次
  • 性别: Icon_minigender_2
  • 来自: 上海转北京
社区版块
存档分类
最新评论

Oracle 将一个库的数据导入到另一个库

阅读更多

最新更新2011-9-28号。

最近的项目中遇到了一个棘手的问题,由于许多业务都是放在定时中执行,有一个需求是统计商户或者门店创建的活动效果,在近期的3.5版本做了很大调整,因此在新的统计方式需要兼容历史活动,所以得把所有的活动遍历统计一次。考虑到

上线当天晚上定时会跑得很慢,所以上级决定目前只将2011年5月1号之后创建的活动和5月1号前创建并且上线时仍有更新的活动。5月1号之前的活动等上线后,在一个新库中将数据跑出来,然后导入到线上库。如果线上库已经存在的数据,就不能导入。因为以线上环境的数据为准。今天将2011年1月1号到5月1号之间的活动数据在新库上跑出来了,线上已经存在的数据,是5月1号前创建并且在8月18号上线新版本后仍有更新的活动,这一部分数据不能覆盖,以线上的为准。

 

 

先建立链接,连接到线上的数据库服务器,具体操作如图所示,别名ntow,然后输入用户名,密码

 

在完成这个任务时,做了数据验证。

将一个数据库上的analysis_invest_profit_store表中的数据导入
--到线上数据库的analysis_invest_profit_store表中。要求:线上已经存在
--的数据不可覆盖。 表analysis_invest_profit_store主键
--是marketingid,coupontypeid和storeid组合的主键

-- analysis_invest_profit_store@ntow t 表示线上库
   
--1)第一步,找出线上与本地数据库相同的数据   执行结果为49条     
  select s.*
    from analysis_invest_profit_store      s,
         analysis_invest_profit_store@ntow t
   where s.marketingid = t.marketingid
     and s.coupontypeid = t.coupontypeid
     and s.storeid = t.storeid;
           
     
  --2)找出线上库不存在的数据 
  -- 此语句执行结果是451条 
  --本地新库中analysis_invest_profit_store表中一共有500条,验证数据不缺少
     select s.*
  from analysis_invest_profit_store s
 where not exists (select *
          from analysis_invest_profit_store@ntow t
         where s.marketingid = t.marketingid
           and s.coupontypeid = t.coupontypeid
           and s.storeid = t.storeid);
   

 

验证数据条数是正确的,没有多余也没有缺少,所以就做下一步动作,将线上库不存在的数据导入到线上的数据库。

 --3)将线上库不存在的数据导入到线上表  
insert into analysis_invest_profit_store@ntow t
select s.*
  from analysis_invest_profit_store s
 where not exists (select *
          from analysis_invest_profit_store@ntow t
         where s.marketingid = t.marketingid
           and s.coupontypeid = t.coupontypeid
           and s.storeid = t.storeid);
    

 

 

 

 

整理一下Oracle在项目中常用的基本语句,算是一个小小的总结概要。

1、获得当前时间

select sysdate from dual
2、获得char类型的时间

select to_char(sysdate,'yyyy-MM-dd') as mydate from dual

3、获得精确到秒的时间

select to_char(sysdate,'yyyy-MM-dd HH24:MI:SS') as mydate from dual

 


4、Oracle中用sql语句更改字段类型。
alter table trans_buycoupon_record  rename column buy_num to buy_num1;

alter table trans_buycoupon_record add buy_num number;

update trans_buycoupon_record set buy_num=cast(buy_num1 as number);

alter table trans_buycoupon_record drop column buy_num1;

 

----------------------------------------------------------------------------------------------------------------------------------

(4月21日补充)

 5、A表与B表有关联,A和B都是col这个字段,但是A表中维护有记录B表中则没有,现在要将B表中的col字段的值更新为与A表中的col值一样update prepay_send_record ps set end_date =(select m.end_date from member_prepay_account m where m.merchant_id=ps.merchant_id and ps.customer_id=m.customer_id )  这里要注意的是要找到将要修改的表与数据来源的表的关系。

 

4月12号,先总结到这里,以后持续更新中。

 

8月15号更新:

 

向Oracle表中加新索引:

CREATE   INDEX   IND_CARBNOW_FRAMEID   ON     sms_send_record(marketing_id,biz_type);

 

语法是:

create INDEX 索引名  on  表名(字段名1,字段名2);

 

 

 

Oracle中复制一张表的结构,用sql语句复制一张表结构

同一个用户复制的话可以用

Sql代码 复制代码 

create table 新表 as select * from 旧表     数据一起复制   

 

Sql代码 复制代码 

create table 新表 as select * from 旧表 where 1=2     空表  

 

不同用户复制的话可以用

 

Sql代码 复制代码 

create table B.新表 as select * from A.旧表    

  

copy   from   a/a   to   b/b   create   table   aaa   using   select   *   from   aaa;  

 

 

 6、将一张表的数据,插入到另一张表(Oracle中将数字类型以字符串形式拼接)

insert into yd_person(person_id,person_name,mobile,store_id)
select CONCAT('25010100006',t.mobile), t.name ,t.mobile ,'250101000060001' from yd_person_1209 t 

 

分享到:
评论
2 楼 远去的渡口 2011-09-28  
courage207 写道
   

如此表情为哪般??
1 楼 courage207 2011-09-28  
   

相关推荐

Global site tag (gtag.js) - Google Analytics