 | 
March 24th, 2008, 08:19 AM
| | Member | | Join Date: Mar 2007
Posts: 106
| | Inheritance problem
hi ,
i have one parent table with 3 fields, id, name and date with no primary key.
i have inherited 6 child tables from the parent table with the same table structure of the parent.
i have created one trigger too to insert data in the child table whenever inserted in the parent table checking the date of one year. if it's between jan and feb of that year then insert into 1st child table, if it's between march and april then insert into 2nd child table and so on.and all the child table has the primary key id.
when ever i insert the same id with the same date, then it will not allow to insert in any of the child table. but when i insert same id with different date, it allows to insert into different child table and in the parent table too.
what i wanted is whenever i insert the id and the date, if the id exists in any of the child table, it should not insert into either the parent table or any child table.
is it possible to make all the child tables behave as a single table to check whether the id already exists or not???
is it possible to do so?
even if i use the primary key in the parent table i.e. id, it still inserts the data in the child tables according to the date.
plz help
with best regards
| 
March 24th, 2008, 06:40 PM
| | Moderator | | Join Date: Dec 2006
Posts: 250
| | Quote: |
Originally Posted by coolminded hi ,
i have one parent table with 3 fields, id, name and date with no primary key.
i have inherited 6 child tables from the parent table with the same table structure of the parent.
i have created one trigger too to insert data in the child table whenever inserted in the parent table checking the date of one year. if it's between jan and feb of that year then insert into 1st child table, if it's between march and april then insert into 2nd child table and so on.and all the child table has the primary key id.
when ever i insert the same id with the same date, then it will not allow to insert in any of the child table. but when i insert same id with different date, it allows to insert into different child table and in the parent table too.
what i wanted is whenever i insert the id and the date, if the id exists in any of the child table, it should not insert into either the parent table or any child table.
is it possible to make all the child tables behave as a single table to check whether the id already exists or not???
is it possible to do so?
even if i use the primary key in the parent table i.e. id, it still inserts the data in the child tables according to the date.
plz help
with best regards | How did you write a trigger?
You can try write a trigger that checks if id exists in tables and if not found any rows in child tables with containing current inserting id and performing the insert wheen non rows found. Can you show the trigger function code?
| 
March 25th, 2008, 11:52 AM
| | Member | | Join Date: Mar 2007
Posts: 106
| | Quote: |
Originally Posted by rski How did you write a trigger?
You can try write a trigger that checks if id exists in tables and if not found any rows in child tables with containing current inserting id and performing the insert when non rows found. Can you show the trigger function code? | here's my code
---the master table -- - create table tbl_mst(
-
id varchar(20) not null,
-
name varchar(50),
-
entry_date date);
-
-
----the child tables ------
-
create tbl_child1(
-
check(entry_date >= DATE '2008-01-01' AND entry_date < '2008-03-01')
-
) INHERITS (tbl_mst);
-
-
create tbl_child2(
-
check(entry_date >= DATE '2008-03-01' AND entry_date < '2008-05-01')
-
) INHERITS (tbl_mst);
-
and so on tbl_child3 to tbl_child6;
-
-
----assigning primary key to child tables------
-
alter table tbl_child1 add constraints pk_child1(id);
-
alter table tbl_child1 add constraints pk_child2(id);
-
and so on for child3 to child6;
-
-
--- creating a function----
-
create or replace function fn_tbl_trigger()
-
returns trigger as '
-
begin
-
if tg_op = 'insert' then
-
if (new.entry_date > = DATE '2008-01-01' and new.entry_date < DATE '2008-03-01' )
-
-
THEN
-
INSERT INTO tbl_child1(new.*);
-
elseif (new.entry_date > = DATE '2008-03-01' and new.entry_date < DATE
-
-
'2008-05-01' ) THEN
-
INSERT INTO tbl_child2(new.*);
-
elsif and so on for child3 to child6;
-
end if;
-
end if;
-
return null;
-
end;
-
'
-
language plpgsql;
-
-
---creating trigger---
-
create trigger tbl_trig
-
before insert on tbl_mst
-
for each row
-
execute procedure fn_tbl_trigger();
i think this will clear u.
Last edited by eWish; March 28th, 2008 at 01:35 AM.
Reason: Please use code tags
| 
March 25th, 2008, 07:17 PM
| | Moderator | | Join Date: Dec 2006
Posts: 250
| | Quote: |
Originally Posted by coolminded here's my code
---the master table --
create table tbl_mst(
id varchar(20) not null,
name varchar(50),
entry_date date);
----the child tables ------
create tbl_child1(
check(entry_date >= DATE '2008-01-01' AND entry_date < '2008-03-01')
) INHERITS (tbl_mst);
create tbl_child2(
check(entry_date >= DATE '2008-03-01' AND entry_date < '2008-05-01')
) INHERITS (tbl_mst);
and so on tbl_child3 to tbl_child6;
----assigning primary key to child tables------
alter table tbl_child1 add constraints pk_child1(id);
alter table tbl_child1 add constraints pk_child2(id);
and so on for child3 to child6;
--- creating a function----
create or replace function fn_tbl_trigger()
returns trigger as '
begin
if tg_op = 'insert' then
if (new.entry_date > = DATE '2008-01-01' and new.entry_date < DATE '2008-03-01' )
THEN
INSERT INTO tbl_child1(new.*);
elseif (new.entry_date > = DATE '2008-03-01' and new.entry_date < DATE
'2008-05-01' ) THEN
INSERT INTO tbl_child2(new.*);
elsif and so on for child3 to child6;
end if;
end if;
return null;
end;
'
language plpgsql;
---creating trigger---
create trigger tbl_trig
before insert on tbl_mst
for each row
execute procedure fn_tbl_trigger();
i think this will clear u. | Maybe try into trigger something like that (you just need to extend the following example for all six child tables) -
create or replace function ....
-
declare
-
r record;
-
...
-
begin
-
...
-
select into r * from tbl_child1 where NEW.id =id
-
if found then
-
return NEW;
-
else
-
return NULL;
-
end if;
-
...
-
end;
-
it's late so i'm not sure if it is clear. Is it?
|  |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|