您好,登錄后才能下訂單哦!
一、Oracle數(shù)據(jù)庫操作
1、創(chuàng)建數(shù)據(jù)庫
create database databasename;
create table table_new as select * from table_old;復(fù)制結(jié)構(gòu)和數(shù)據(jù)
create table table_new as select * from table_old where 1=0;只復(fù)制結(jié)構(gòu)
2、刪除數(shù)據(jù)庫
drop database dbname
3、備份數(shù)據(jù)庫
完全備份
exp demo/demo@orcl buffer=1024 file=d:\back.dmp full=y
demo:用戶名、密碼
buffer: 緩存大小
file: 具體的備份文件地址
full: 是否導(dǎo)出全部文件
ignore: 忽略錯誤,如果表已經(jīng)存在,則也是覆蓋
將數(shù)據(jù)庫中system用戶與sys用戶的表導(dǎo)出
exp demo/demo@orcl file=d:\backup\1.dmp owner=(system,sys)
導(dǎo)出指定的表
exp demo/demo@orcl file=d:\backup2.dmp tables=(teachers,students)
按過濾條件,導(dǎo)出
exp demo/demo@orcl file=d:\back.dmp tables=(table1) query=\" where filed1 like 'fg%'\"
導(dǎo)出時可以進(jìn)行壓縮;命令后面 加上 compress=y ;如果需要日志,后面: log=d:\log.txt
備份遠(yuǎn)程服務(wù)器的數(shù)據(jù)庫
exp 用戶名/密碼@遠(yuǎn)程的IP:端口/實(shí)例 file=存放的位置:\文件名稱.dmp full=y
4、數(shù)據(jù)庫還原
打開cmd直接執(zhí)行如下命令,不用再登陸sqlplus。
完整還原
imp demo/demo@orcl file=d:\back.dmp full=y ignore=y log=D:\implog.txt
指定log很重要,便于分析錯誤進(jìn)行補(bǔ)救。
導(dǎo)入指定表
imp demo/demo@orcl file=d:\backup2.dmp tables=(teachers,students)
還原到遠(yuǎn)程服務(wù)器
imp 用戶名/密碼@遠(yuǎn)程的IP:端口/實(shí)例 file=存放的位置:\文件名稱.dmp full=y
二、Oracle表操作
1、創(chuàng)建表
create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)
根據(jù)已有的表創(chuàng)建新表:
A:select * into table_new from table_old (使用舊表創(chuàng)建新表)
B:create table tab_new as select col1,col2… from tab_old definition only<僅適用于Oracle>
總結(jié):
A.只復(fù)制表結(jié)構(gòu) 加入了一個永遠(yuǎn)不可能成立的條件1=2,則此時表示的是只復(fù)制表結(jié)構(gòu),但是不復(fù)制表內(nèi)容
create table 用戶名.表名 as select * from 用戶名.表名 where 1=2
如create table zdsy.bs_log2 as select * from zdsy.bs_log where 1=2
B.完全復(fù)制表(包括創(chuàng)建表和復(fù)制表中的記錄)
create table test as select * from bs_log --bs_log是被復(fù)制表
2、刪除表
drop table tabname
3、重命名表
說明:alter table 表名 rename to 新表名
eg:alter table tablename rename to newtablename
4、增加字段
說明:alter table 表名 add (字段名 字段類型 默認(rèn)值 是否為空);
例:alter table tablename add (ID int);
alter table tablename add (ID varchar2(30) default '空' not null);
5、修改字段
說明:alter table 表名 modify (字段名 字段類型 默認(rèn)值 是否為空);
eg:alter table tablename modify (ID number(4));
6、重名字段
說明:alter table 表名 rename column 列名 to 新列名 (其中:column是關(guān)鍵字)
eg:alter table tablename rename column ID to newID;
7、刪除字段
說明:alter table 表名 drop column 字段名;
eg:alter table tablename drop column ID;
8、添加主鍵
alter table tabname add primary key(col)
9、刪除主鍵
alter table tabname drop primary key(col)
10、創(chuàng)建索引
create [unique] index idxname on tabname(col….)
11、刪除索引
drop index idxname
注:索引是不可更改的,想更改必須刪除重新建。
12、創(chuàng)建視圖
create view viewname as select statement
13、刪除視圖
drop view viewname
14.重建索引
select 'alter index '||index_name||' rebuild tablespace SHINEYUE45 online;' from user_indexes where table_name='cw_kmbh'
alter index CW_KMBH_KMBH rebuild tablespace SHINEYUE45 online;
alter index CW_KMBH_ZJM rebuild tablespace SHINEYUE45 online;
alter index PK_CW_KMBH rebuild tablespace SHINEYUE45 online;
三、Oracle操作數(shù)據(jù)
1、數(shù)據(jù)查詢
select <列名> from <表名> [where <查詢條件表達(dá)試>] [order by <排序的列名>[asc或desc]]
2、插入數(shù)據(jù)
insert into 表名 values(所有列的值);
insert into test values(1,'zhangsan',20);
insert into 表名(列) values(對應(yīng)的值);
insert into test(id,name) values(2,'lisi');
3、更新數(shù)據(jù)
update 表 set 列=新的值 [where 條件] -->更新滿足條件的記錄
update test set name='zhangsan2' where name='zhangsan'
update 表 set 列=新的值 -->更新所有的數(shù)據(jù)
update test set age =20;
4、刪除數(shù)據(jù)
delete from 表名 where 條件 -->刪除滿足條件的記錄
delete from test where id = 1;
delete from test -->刪除所有
commit; -->提交數(shù)據(jù)
rollback; -->回滾數(shù)據(jù)
delete方式可以恢復(fù)刪除的數(shù)據(jù),但是提交了,就沒辦法了 delete刪除的時候,會記錄日志 -->刪除會很慢很慢
truncate table 表名
刪除所有數(shù)據(jù),不會影響表結(jié)構(gòu),不會記錄日志,數(shù)據(jù)不能恢復(fù) -->刪除很快
drop table 表名
刪除所有數(shù)據(jù),包括表結(jié)構(gòu)一并刪除,不會記錄日志,數(shù)據(jù)不能恢復(fù)-->刪除很快
5、數(shù)據(jù)復(fù)制
表數(shù)據(jù)復(fù)制
insert into table1 (select * from table2);
復(fù)制表結(jié)構(gòu)
create table table1 select * from table2 where 1>1;
復(fù)制表結(jié)構(gòu)和數(shù)據(jù)
create table table1 select * from table2;
復(fù)制指定字段
create table table1 as select id, name from table2 where 1>1;
6. 創(chuàng)建用戶倒庫
select * from dba_directories;
select * from dba_users;
drop user shineyue45_hhgjjcs cascade;----刪除
create user shineyue45_hhgjjcs identified by atwasoft2017 default tablespace SHINEYUE45;--賦給用戶默認(rèn)表空間
grant connect,resource to shineyue45_hhgjjcs;
grant select any dictionary to shineyue45_hhgjjcs;--查詢字典
grant create any view to shineyue45_hhgjjcs;--視圖
grant select_catalog_role to shineyue45_hhgjjcs;
grant execute any procedure to shineyue45_hhgjjcs;
grant debug any procedure to shineyue45_hhgjjcs;--存儲
grant debug connect session to shineyue45_hhgjjcs;--會話
grant select any table to shineyue45_hhgjjcs;--查詢表
GRANT "EXP_FULL_DATABASE" TO shineyue45_hhgjjcs WITH ADMIN OPTION;-- 導(dǎo)出
GRANT "IMP_FULL_DATABASE" TO shineyue45_hhgjjcs WITH ADMIN OPTION;-- 導(dǎo)入
scp -r /hhgjj_backup/backup/shineyue4020180928.dmp root@172.18.111.55:/oracle/backup/
impdp SHINEYUE45_HHGJJCS/atwasoft2017@172.18.111.55:1521/orcl directory=HHGJJ_BACKUP dumpfile=shineyue4020181211.dmp logfile=shineyue4020181211.log remap_schema=HHGJJ_SYS40:SHINEYUE45_HHGJJCS remap_tablespace=HHGJJ_SYS40:SHINEYUE45 transform=OID:N
注意:1>如果有分區(qū)表需要在這個導(dǎo)入語句中加入transform=segment_attributes:n參數(shù)。該參數(shù)可與忽略expdp導(dǎo)出時附帶的相關(guān)表空間和存儲子句約束。
expdp HHGJJ_SYS40/atwasoft2018@172.18.111.77:1521/orcl directory=BACKUP_DATA dumpfile=shineyue4020180918.dmp schemas=HHGJJ_SYS40
注意:不要在impdp和empdp語句最后不能有分號
7.insert和select一起使用
insert into im_gryhzh
select f_newid,a.grbh,a.jgbm,jkrxm,d.hkzh,' ','01',jkrzjh,sysdate,d.swtyhbm,c.mc,c.bm,a.cllrczy, a.lrczyxm,' ',' ',' ','00',' ',' ',b.yhbm
from grdk_sq_htxx a,bm_yhbm b,bm_im_yhjg c,grdk_dk_zz d where d.jkhtbh in(select jkhtbh from grdk_dk_zz where dkzt='02')
and d.hkzh not in(select yhzh from im_gryhzh)
and d.swtyhbm=b.bm and b.yhbm=c.yhbm and d.dkffrq>=to_date('20180501','yyyymmdd') and a.jkhtbh=d.jkhtbh;
7.creat和select一起使用----產(chǎn)用于備份指定數(shù)據(jù)
create table grdk_tx_hk_yh_jh_0920 as
select * from grdk_tx_hk_yh yh where exists
(select ywlsh from im_tx_zjcljg_zjb where ywlsh=yh.id and pcid in
(select pcid from im_tx_zjjsjy_zjb a where a.batchno in ('VS201809180059015334','VS201809180059015443')) and fse<>0)
and jkhtbh not in (select jkhtbh from grdk_dk_hkmx
where jzrq=to_date('20180918','yyyymmdd') and hkzt in ('02','03') and hktj='02' and gjjzqe=0);
8.update和select一起使用
update grdk_tx_hk_yh set sfpbcg=0
where id in (select ywlsh from im_tx_zjcljg_zjb where pcid in
(select pcid from im_tx_zjjsjy_zjb a where a.batchno in '20180918100077703223') and fse<>0)
and jkhtbh not in (
select jkhtbh from grdk_dk_hkmx
where jzrq=to_date('20180918','yyyymmdd') and hkzt='02' and hktj='02' and gjjzqe=0)
update tmp_cw_dwfzhs a set a.xmbm1mc=(select nvl(max(hsxmmc),' ') from cw_fzhs_mx where hsxmbm=a.xmbm1 and hslxbm='001' and ztbh=v_ztbh and nd=v_cwnd)
where exists(select max(hsxmmc) from cw_fzhs_mx where hsxmbm=a.xmbm1 and ztbh=v_ztbh and nd=v_cwnd and hslxbm='001');
9.delete和update一起使用
delete from grdk_tx_hk where jkhtbh in (select jkhtbh from grdk_tx_hk_yh
where id in (select ywlsh from im_tx_zjcljg_zjb where pcid in
(select pcid from im_tx_zjjsjy_zjb a where a.batchno in '20180918100077703223') and fse<>0)
and jkhtbh not in (
select jkhtbh from grdk_dk_hkmx
where jzrq=to_date('20180918','yyyymmdd') and hkzt='02' and hktj='02' and gjjzqe=0)) and txrq>to_date('20180918','yyyymmdd')
10.游標(biāo)使用------相當(dāng)于java中數(shù)據(jù)類型集合,特別適用于處理批量數(shù)據(jù)(比如批量未入賬的)
DECLARE
V_GRZH VARCHAR2(20):=' ';
V_JXRQ DATE:=TO_DATE('20190701','YYYYMMDD');
V_DQJS NUMBER(20,2):=0;
V_HQJS NUMBER(20,2):=0;
V_DQLX NUMBER(20,2):=0;
V_HQLX NUMBER(20,2):=0;
V_LXHJ NUMBER(20,2):=0;
CURSOR CUR_GRZH IS SELECT GRZH FROM GJZF_GR_ZZ WHERE grzh='000000151018';
BEGIN
OPEN CUR_GRZH;
LOOP
FETCH CUR_GRZH INTO V_GRZH;
EXIT WHEN CUR_GRZH%NOTFOUND;
V_DQJS:=0;
V_HQJS:=0;
P_HFB_JCR_NDJX_JSCALC(V_GRZH,V_JXRQ,V_DQJS,V_HQJS,V_DQLX,V_HQLX,V_LXHJ);
UPDATE GJZF_GR_ZZ SET NDJXDQJS=V_DQJS,NDJXHQJS=V_HQJS WHERE GRZH=V_GRZH;
COMMIT;
END LOOP;
CLOSE CUR_GRZH;
END;
四>Oracle定時器操作
1>查詢數(shù)據(jù)庫下面有哪些定時器
select * from dba_jobs;
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。