Skip to Main Content
  • Questions
  • Migration from Postgres to Oracle (Parent Child Relationships)

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Abhishek.

Asked: April 26, 2011 - 12:43 am UTC

Last updated: April 28, 2011 - 7:57 am UTC

Version: 11

Viewed 1000+ times

You Asked

I am working on a migration task where I wanted to move PostGres tables to Oracle (Standard Edition).
In postgres my table structure is like this

Parent Table
Field 1
Field 2

And exactly similar structure is there for the Child Table

Child Table
Field 1
Field 2

We had similar structure because of some requirements, whenever parent table is queried depending on the where clause it used to query all the tables behnind the scenes and get us the data.

Now I would like to repeat this kind of stuff in Oracle 11g, how can I achieve this, please let me know.



and Tom said...

I'm not sure I understand the problem here. We support primary key and foreign key constraints in a rather straightforward manner.

create table dept (deptno number primary key, ... );
create table emp ( empno number primary key, ... deptno number references dept(deptno), ... );


The syntax should be identical to postgres. This example:

http://www.postgresql.org/docs/8.3/static/tutorial-fk.html

would run unchanged in Oracle.

Rating

  (8 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Oleksandr Alesinskyy, April 28, 2011 - 7:30 am UTC

See http://www.postgresql.org/docs/8.1/static/ddl-inherit.html as well as http://www.postgresql.org/docs/8.1/static/sql-createtable.html

This functionality is absent in Oracle. In case of a single parent it may be mimicked by resorting to object types, but for multiple parent - nope.

Anyhow, its implementation in current version of PostgreSQL has major drawbacks (see Caveats section in the first link).
Tom Kyte
April 28, 2011 - 7:57 am UTC

It would have been neat if the original poster had been a tad more clear in what they were saying/asking.


I don't see why you would use object types - it seems a simple view would do the trick? You already had to point the inserts to the correct base tables - you can just query a view to get what you need from them?

Oleksandr Alesinskyy, April 28, 2011 - 9:25 am UTC

Yes, a view can do the trick - almost...
As each time you add a new child table (or grand-child, or grand-grand-child) you need to modify your view (or views).

So it is doable - but require additional maintenance efforts.

Object views

A reader, April 28, 2011 - 2:24 pm UTC

object views allow you to add new child in this hierarchy without original view modification. Just use "under {parent view}" clause while creating object view for the child. Though you are not explicitly modifying the original parent view - it will be altered behind the scene to include child view in this case.

orace apps

A reader, April 28, 2011 - 2:48 pm UTC

what is responsibility,,,profile options in oracle apps?what are iys functionalities?

Object views @A reader

Oleksandr Alesinskyy, April 30, 2011 - 8:09 am UTC

It is a kind of a "resorting to object types" which I have mentioned in my post. But even this solution does not mimic PostgeSQL functionality completely:

- You cannot inherit from 2 or more parents (in PostgeSQL you can)
- You need to duplicate definition of all columns by a child creation
- changes to the parent table structure are not automatically reflected in its childs
- You need to use 2 commands (create table + create view) instead of single create table.

And so on.

Object views @Oleksandr

A reader, May 02, 2011 - 10:47 am UTC

I'd say i LOVE how object features are implemented in Oracle. They were able to clearly separate them from regular relational DB nature. I'd say - how it is handled in postgres is at least not mature (if not useless).
As for inheriting from more than one parent - I believe multiple inheritance is more evil than benefit. Not surprisingly that pretty much all modern languages got rid of it and switch to interface implementation.
In Oracle - if you want you can use even tables of object types, but object views are MUCH more powerful and convenient way of working with OOO features. To be honest, all I care for OOO - polymorphism. And Oracle allows us to have it in a very elegant way in DB. We are using these features VERY heavily and are pretty happy with them.

Object views

Duke Ganote, May 03, 2011 - 2:40 pm UTC

I suppose, if you really wanted to mimic postgreSQL functionality for this, you could write a proc that would modify the view when a child table was created. Then call it through a DDL trigger. Or at end-of-day, just rebuild it automatically.

@Object views @Oleksandr

A reader, May 08, 2011 - 4:20 pm UTC

1. You completely mixed up behavior inheritance and data structure inheritance. In case PostgerSQL we have later only.
2. As for multiply inheritance - what you are writing reflects quite outdated state (Java and alike), most really modern languages support mix-ins or similar constructs.