设为首页 收藏本站
查看: 1699|回复: 0

[经验分享] 开发人员常用的Oracle技术

[复制链接]

尚未签到

发表于 2016-7-27 11:17:45 | 显示全部楼层 |阅读模式
数据导出导入
  参考:http://www.cnblogs.com/jason_lb/archive/2007/02/09/645586.html
  

导出数据exp
  
  1)将数据库TEST完全导出,用户名system 密码manager 导出到D:\daochu.dmp中
  exp system/manager@TEST file=d:\daochu.dmp full=y
  
  2)将数据库中system用户与sys用户的表导出
  exp system/manager@TEST file=d:\daochu.dmp owner=(system,sys)
  
  3)将数据库中的表inner_notify、notify_staff_relat导出
  exp aichannel/aichannel@TESTDB2 file= d:\datanewsmgnt.dmp tables=(inner_notify,notify_staff_relat) 
  
  4)将数据库中的表table1中的字段filed1以"00"打头的数据导出
  exp system/manager@TEST file=d:\daochu.dmp tables=(table1) query=" where filed1 like '00%'"
  

导入数据imp
  
  1)将D:\daochu.dmp 中的数据导入 TEST数据库中。
  imp system/manager@TEST  file=d:\daochu.dmp
  imp aichannel/aichannel@TEST  full=y  file=d:\datanewsmgnt.dmp ignore=y
  上面可能有点问题,因为有的表已经存在,然后它就报错,对该表就不进行导入。
  在后面加上ignore=y 就可以了
  
  2)将d:daochu.dmp中的表table1 导入
  imp system/manager@TEST  file=d:\daochu.dmp  tables=(table1)
  

创建表空间
  

create tablespace TBLSMS
datafile 'D:/oracle/product/10.2.0/oradata/shenhq/tblsms.dbf' size 5m reuse
AUTOEXTEND ON NEXT 10M MAXSIZE UNLIMITED
EXTENT MANAGEMENT LOCAL AUTOALLOCATE
/
  
  
  与操作系统有一些相关,因为涉及数据文件的指定:
  

create tablespace TBLSMS
datafile '/opt/app/oracle/oradata/stariboss/tblsms.dbf' size 5m reuse
AUTOEXTEND ON NEXT 10M MAXSIZE UNLIMITED
  EXTENT MANAGEMENT LOCAL AUTOALLOCATE
/
  
  

创建用户并配置权限
  
  

-- Create the user
create user [user_name]
identified by "[user_name]"
default tablespace [default_tablespace_name]
temporary tablespace [temp_tablespace_name]
profile DEFAULT;
-- Grant/Revoke role privileges
grant dba to [user_name];
-- Grant/Revoke system privileges
grant insert any table to [user_name];
grant select any table to [user_name];
grant update any table to [user_name];
grant unlimited tablespace to [user_name];
   
  
  

典型的存储过程编写
  不会写PL/SQL不叫会用Oracle
  

存储过程

spool .[log_name].log
prompt
prompt Creating procedure [procedure_name]
prompt
create or replace procedure [procedure_name]
is
sql_code varchar2(50);
sql_errm varchar2(100);
cursor [cur_name] is
{select sql};
v_cnt number;
begin
v_cnt := 0;
for v_instanceservice in [cur_name] loop
begin
{some update sql};
v_cnt := v_cnt + 1;
if mod(v_cnt, 10000) = 0 then
commit;
end if;
exception
when others then
sql_code := sqlcode;
sql_errm := sqlerrm;
dbms_output.put_line(sql_code || ' ' || sql_errm ||
' at servicestr ' ||
v_instanceservice.servicestr);
end;
end loop;
commit;
dbms_output.put_line('Total is:' || v_cnt);
end;
/

spool off


 
函数


create or replace function [function_name]([parameters_name] [parameters_type])
return varchar2 IS
v_result         varchar2(100);
{use parameters in sql as value}
begin
v_result         := null;
{process nothing to say}
return v_result;
end [function_name];
   

修改表

增加字段
  

create or replace procedure ALTER_TABLE is
v_count number;
begin
select count(*)
into v_count
from user_tab_columns u
where u.TABLE_NAME = [表名]
and (u.COLUMN_NAME = [字段名,如果多个用OR拼接条件]);
if v_count < 1 then
execute immediate '[增加字段的ALTER脚本]';
end if;
commit;
end;
/
exec ALTER_TABLE;
DROP PROCEDURE ALTER_TABLE;
其他杂项
  

调整一批sequence的值
  

create or replace procedure adjust_seq_value_prc
is
sql_code    varchar2(50);
sql_errm    varchar2(100);
cursor cur_get_seq_info is
select us.sequence_name, us.last_number, us.increment_by, us.cache_size
from user_sequences us
where us.sequence_name not in
('[sequence_name]', '[sequence_name]', ......);
begin
for v_get_seq_info in cur_get_seq_info loop
begin
execute immediate 'alter sequence ' || v_get_seq_info.sequence_name ||
' increment by [number_want_to_add] nocache';
execute immediate 'select ' || v_get_seq_info.sequence_name ||
'.nextval from dual'
execute immediate 'alter sequence ' || v_get_seq_info.sequence_name ||
' increment by ' || v_get_seq_info.increment_by ||
' nocache';
exception
when others then
sql_code := sqlcode;
sql_errm := sqlerrm;
dbms_output.put_line(sql_code || ' ' || sql_errm);
end;
end loop;
end;
/

   
  (有新知新得再补充,待续……)
  
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-250227-1-1.html 上篇帖子: Oracle数据库的应用结构 下篇帖子: Oracle中单行函数的分类
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表