问题描述
嗨,团队,
你能帮我回答我的一个面试问题吗-
考虑一个具有1000行的日期列的非分区表x。
我们如何插入未来的行,即从1001 ..继续进入分区 (不修改表结构即非分区表)
是否真的有可能保持非分区表的状态,并且表上的新插入可以移动到分区?
你能帮我回答我的一个面试问题吗-
考虑一个具有1000行的日期列的非分区表x。
我们如何插入未来的行,即从1001 ..继续进入分区 (不修改表结构即非分区表)
是否真的有可能保持非分区表的状态,并且表上的新插入可以移动到分区?
专家解答
面试官可能意味着两件事中的一件
1) 现有表T成为新表中的分区
2) 将新行插入到 * new * table T_PAR (已分区) 中,并保留现有表T未分区,或
对于 (1),您可以创建一个新的分区表 (T_PAR),然后用表T发出 “alter table T_PAR交换分区P1”,然后表现在被分区,您没有移动任何现有数据。
对于 (2),您可以创建位于表格顶部的视图,并使用instead of trigger捕获插入,例如
1) 现有表T成为新表中的分区
2) 将新行插入到 * new * table T_PAR (已分区) 中,并保留现有表T未分区,或
对于 (1),您可以创建一个新的分区表 (T_PAR),然后用表T发出 “alter table T_PAR交换分区P1”,然后表现在被分区,您没有移动任何现有数据。
对于 (2),您可以创建位于表格顶部的视图,并使用instead of trigger捕获插入,例如
SQL> create table t ( x date ); Table created. SQL> SQL> insert into t select date '2017-05-01' - rownum 2 from dual connect by level <= 10; 10 rows created. SQL> SQL> commit; Commit complete. SQL> SQL> create table t_par ( x date ) 2 partition by range ( x ) 3 ( 4 partition p_empty values less than ( date '2017-05-01' ), 5 partition p1 values less than ( date '2020-01-01' ) 6 ); Table created. SQL> SQL> rename t to t_old_data; Table renamed. SQL> SQL> create view t as 2 select * from t_old_data 3 union all 4 select * from t_par; View created. SQL> SQL> create or replace 2 trigger trg 3 instead of insert on t 4 for each row 5 begin 6 if :new.x < date '2017-05-01' then 7 insert into t_old_data values (:new.x); 8 else 9 insert into t_par values (:new.x); 10 end if; 11 end; 12 / Trigger created. SQL> SQL> insert into t values ( sysdate ); 1 row created. SQL> SQL> select * from t; X --------- 30-APR-17 29-APR-17 28-APR-17 27-APR-17 26-APR-17 25-APR-17 24-APR-17 23-APR-17 22-APR-17 21-APR-17 06-MAY-17 11 rows selected. SQL> select * from t_par; X --------- 06-MAY-17 1 row selected.
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




