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

[经验分享] Oracle常用SQL集锦----持续更新中......

[复制链接]

尚未签到

发表于 2016-8-3 12:50:09 | 显示全部楼层 |阅读模式
--1)Primary table
create table T_FRANK
(
T_NO                          NUMBER not null,
T_NAME                        NUMBER
)
tablespace STAGING_TEST2_DATA
pctfree 10
pctused 40
initrans 1
maxtrans 255
storage
(
initial 1M
minextents 1
maxextents unlimited
);  
-- 2)add PK for table T_FRANK
alter table T_FRANK
add constraint PK_T_FRANK primary key (T_NO)
using index
tablespace STAGING_TEST2_DATA
pctfree 10
initrans 2
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);

-- update field's type
alter table T_FRANK modify T_NAME VARCHAR2(30);

--3)Foreign table
create table T_FRANK_TEST
(
F_ID                          NUMBER not null,
T_NO                          NUMBER not null,
IS_MANDATORY                  CHAR(1) default '1',
QTY                           NUMBER default '0',
PRICE                         NUMBER(16,4),
LAUNCH_DATE                   DATE,
MODIFIER                      VARCHAR2(50)
)
tablespace STAGING_TEST2_DATA
pctfree 10
pctused 40
initrans 1
maxtrans 255
storage
(
initial 1M
minextents 1
maxextents unlimited
);

-- 4)add PK for table T_FRANK_TEST
alter table T_FRANK_TEST
add constraint PK_T_FRANK_TEST primary key (F_ID, T_NO)
using index
tablespace STAGING_TEST2_DATA
pctfree 10
initrans 2
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);

--5)add FK for T_FRANK_TEST
alter table T_FRANK_TEST
add constraint FK_T_FRANK_TEST_T_FRANK foreign key (T_NO)
references T_FRANK (T_NO) on delete cascade;

--6)Create/Recreate indexes
create index T_FRANK_TEST_F_ID on T_FRANK_TEST (F_ID)
tablespace STAGING_TEST2_DATA
pctfree 10
initrans 2
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);

--7) insert data to T_FRANK
insert into T_FRANK values(NVL((select max(T_NO) from T_FRANK),0)+1,'Frank1');
insert into T_FRANK values(NVL((select max(T_NO) from T_FRANK),0)+1,'Frank2');
commit;

--8) insert data to T_FRANK_TEST
insert into T_FRANK_TEST values(NVL((select max(F_ID) from T_FRANK_TEST),0)+1,1,'1',1000,66.98,sysdate,'FrankWang');
insert into T_FRANK_TEST values(NVL((select max(F_ID) from T_FRANK_TEST),0)+1,2,'1',1000,66.98,sysdate,'FrankWang');
commit;

--9) update data
UPDATE T_FRANK_TEST t SET t.MODIFIER='FrankWang2' where t.f_id=2;
commit;

--10) delete T_FRANK(cascade delete T_FRANK_TEST)
delete from T_Frank f where f.t_no=1;
commit;

--11) delete the data and the table
--因为需要,Oracle有没有类似My SQL的drop table if exists这类的支持,只好自己写一个Declare了
declare
p_table varchar2(30):='T_FRANK_TEST';
v_count number;
begin
select count(1) into v_count from user_objects where object_name = upper('T_FRANK_TEST');
if v_count > 0 then
execute immediate 'drop table ' || p_table ||' cascade constraints';
end if;
end;
/

--12) usage for CURSOR , LOOP and WHILE in DECLARE
下面是实际工作中遇到的问题:
For this sap invoice error, there are two work need to do: please follow below order to execute the statement(1 -> 2), thanks!
1>Data patch:
Delete the dirty data from the table INVOICE_ARTICLE_SIZE, in order to delete it quickly(the dirty data is too large(about53473089)), below was my prepared statement:
According to preliminary estimates, below statement need about 13 hours in my local env. May be it need few time in Production.
DECLARE
--define batch delete number of invoices no
topnum constant int:=5;
----53473089.=5000*maxrecords=5000*10694, maxnum=10694
maxnum constant int:=2;
i int :=1;
new_invoice_no invoice_article_size.invoice_no%TYPE;
r_row invoice_article_size%rowtype;
--get top 500 record and stored to cursor
cursor my_cursor is
select * from invoice_article_size where invoice_no not in (select invoice_no from invoice) and rownum <= topnum;
TYPE t_invoice_no IS TABLE OF varchar2(20) INDEX BY BINARY_INTEGER;
j int :=1;
t_new_invoice_no t_invoice_no;   
BEGIN
WHILE i<=maxnum LOOP
open my_cursor;
loop
fetch my_cursor into r_row;
-- not found, exit from cursor
exit when my_cursor%notfound;
--get invoce_no
t_new_invoice_no(j) := r_row.invoice_no;
dbms_output.put_line(t_new_invoice_no(j));
--delete it one by one and commit
delete from invoice_article_size where invoice_no = t_new_invoice_no(j);
commit;
end loop;
close my_cursor;
dbms_output.put_line(i);
--execute another loop
i := i+1;
END LOOP;
END;

2>Hotfix:
add cascade delete for INVOICE_ARTICLE_SIZE, below is the SQL:(it also need long time to execute, it depends on number of the record in the size table.)
alter table INVOICE_ARTICLE_SIZE
add constraint FK_INOVICE_ARTICLE_SIZE foreign key (INVOICE_NO,ARTICLE_NO,ERP_ORDER_ID)
references INVOICE_ARTICLE (INVOICE_NO,ARTICLE_NO,ERP_ORDER_ID) on delete cascade;

BTW, may be this data patch can’t finish today,
But our system will continue to generate dirty data before you do Hotfix,
Hence I suggest, you’d better execute below sql to check whether there are dirty data exist:
select count(1) from invoice_article_size ins where (ins.invoice_no not in(select invoice_no from invoice));

If above select result>0, it means there are dirty data exist, you can use below delete statement to delete it directly.
delete from INVOICE_ARTICLE_SIZE i where (i.invoice_no not in(select invoice_no from invoice));
commit;


--13)待续。。。

运维网声明 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-252338-1-1.html 上篇帖子: oracle表的创建与删除 下篇帖子: 原创:oracle 存储过程<十三>
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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