473,387 Members | 1,588 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Inheritance problem

137 100+
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
Mar 24 '08 #1
3 2895
rski
700 Expert 512MB
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?
Mar 24 '08 #2
coolminded
137 100+
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 --

Expand|Select|Wrap|Line Numbers
  1. create table tbl_mst(
  2. id varchar(20) not null,
  3. name varchar(50),
  4. entry_date date);
  5.  
  6. ----the child tables ------
  7. create tbl_child1(
  8. check(entry_date >= DATE '2008-01-01' AND entry_date < '2008-03-01')
  9. ) INHERITS (tbl_mst);
  10.  
  11. create tbl_child2(
  12. check(entry_date >= DATE '2008-03-01' AND entry_date < '2008-05-01')
  13. ) INHERITS (tbl_mst);
  14. and so on tbl_child3 to tbl_child6;
  15.  
  16. ----assigning primary key to child tables------
  17. alter table tbl_child1 add constraints pk_child1(id);
  18. alter table tbl_child1 add constraints pk_child2(id);
  19. and so on for child3 to child6;
  20.  
  21. --- creating a function----
  22. create or replace function fn_tbl_trigger()
  23.  returns trigger as '
  24. begin
  25. if tg_op = 'insert' then
  26.     if (new.entry_date > = DATE '2008-01-01' and new.entry_date < DATE '2008-03-01' )     
  27.  
  28.     THEN
  29.         INSERT INTO tbl_child1(new.*);
  30.     elseif (new.entry_date > = DATE '2008-03-01' and new.entry_date < DATE 
  31.  
  32. '2008-05-01' )         THEN
  33.         INSERT INTO tbl_child2(new.*);
  34.     elsif and so on for child3 to child6;
  35.     end if;
  36. end if;
  37. return null;
  38. end;
  39. '
  40. language plpgsql;
  41.  
  42. ---creating trigger---
  43. create trigger tbl_trig
  44. before insert on tbl_mst
  45. for each row
  46. execute procedure fn_tbl_trigger();
i think this will clear u.
Mar 25 '08 #3
rski
700 Expert 512MB
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)

Expand|Select|Wrap|Line Numbers
  1. create or replace function ....
  2. declare
  3. r record;
  4. ...
  5. begin
  6. ...
  7.         select into r * from tbl_child1 where NEW.id =id
  8.         if found then              
  9.                 return NEW;
  10.         else
  11.                 return NULL;
  12.         end if;
  13. ...
  14. end;
  15.  
it's late so i'm not sure if it is clear. Is it?
Mar 25 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: AIM | last post by:
Error in msvc in building inheritance.obj to build hello.pyd Hello, I am trying to build the boost 1.31.0 sample extension hello.cpp. I can not compile the file inheritance.cpp because the two...
3
by: Morten Aune Lyrstad | last post by:
Hi again! I'm having problems with inheritance. I have a base interface class called IObject. Next I have two other interfaces classes, IControl and ICommandMaster, which derives from IObject. ...
14
by: Steve Jorgensen | last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling a particular case of implementation inheritance that would be easy and obvious in a fully OOP language, but is not at...
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
8
by: Gaetan | last post by:
hi i have 2 classes A1 and A2 implementing a problem with 2 different ways i also have 2 other classes X1 and X2 implementing an other problem i need classes that provide A1+X1 methods,...
6
by: VR | last post by:
Hi, I read about Master Pages in ASP.Net 2.0 and after implementing some WinForms Visual Inheritance I tryed it with WebForms (let's say .aspx pages, my MasterPage does not have a form tag itself...
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
5
by: colint | last post by:
Hi I'm fairly new to c++ and I have a question regarding inheritance. I'm trying to create a class based on 2 inherited classes, e.g. class A { ... } class B: public A
5
by: a | last post by:
Hi, I have an oop inheritance graph problem. What is the difference betweent the following 2 inheritance graph? How does the C++ solve the naming conflict problem for multiple inheritance...
3
by: Leo Seccia | last post by:
Hello everyone, I have a c# project with a sql server database. I have a number of lookup tables in my database which I successfully managed to import into my LINQ dataclasses. eg. Table:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.