_ | 覦覈襦 | 豕蠏手 | 殊螳 | 譯殊碁
FrontPage › PostgreSQLPartitioning

data type 襷讌 朱, trigger 蠍磯 觧.

drop table test_child1;
drop table test_child2;
drop table test_parent;

--覿覈 企
create table test_parent(
	id int
,	logs json
,	src text
);

--企: 一 豢螳
create table test_child1(check(id = 1)) inherits (test_parent);
create table test_child2(check(id = 2)) inherits (test_parent);

--碁Μ蟇 
create or replace function test_insert_trigger()
returns trigger as
$$
declare logs json;
declare	ins_sql text;
begin
	logs := new.src;
	ins_sql='insert into test_child' ||new.id||'(id, logs) values('||new.id||', '''||new.src||''')';
	execute ins_sql;
	return null;
exception
	when others then
		ins_sql='insert into test_child' ||new.id||'(id, src) values('||new.id||', '''||new.src||''')';
		execute ins_sql;
		return null;
end;
$$ language plpgsql;

--碁Μ蟇
create trigger insert_test_trigger
    before insert on test_parent
    for each row execute procedure test_insert_trigger();

--ろ
insert into test_parent(id, src) values(1, '{"kr":"覩手記", "en":"KOREA"}');
insert into test_parent(id, src) values(1, '{"kr":"覩手記", "en":"KOREA"');

select * from test_parent;


--一  ?
select * from test_parent
where id = 1;

/*
"Append  (cost=0.00..20.38 rows=5 width=68)"
"  ->  Seq Scan on test_parent  (cost=0.00..0.00 rows=1 width=68)"
"        Filter: (id = 1)"
"  ->  Seq Scan on test_child1  (cost=0.00..20.38 rows=4 width=68)"
"        Filter: (id = 1)"
*/

--一 
alter table test_child1 no inherit test_parent;

select * from test_parent
where id = 1;
--0 rows

--drop table test_child1
蠍 蠍郁鍵..
企: : るジ讓曙 襦螻豺 企Ν 譯殊語. 襦螻豺
EditText : Print : Mobile : FindPage : DeletePage : LikePages : Powered by MoniWiki : Last modified 2018-04-13 23:12:53

願 蟆 譬 譯殊 語 螳覃 旧願 蟆企. (S.襴曙)