Обсуждение: inheritance and table

Поиск
Список
Период
Сортировка

inheritance and table

От
nik600
Дата:
hi

i don't have understand how works inheritance of tables...

if table B inherits from table A

- A and B must share primary keys?

- if i isert record in B the record is replaced in A ?

can i avoid this?

i would like to have this scenario:

table A

table B inheridts from A
table C inheridts from A

if i insert a record in B it must be insered only in B!
if i insert a record in C it must be insered only in C!

is it possible?

thanks

Re: inheritance and table

От
Richard Broersma Jr
Дата:
> i don't have understand how works inheritance of tables...
> if table B inherits from table A
> - A and B must share primary keys?

No, currently there is no unique constraint that will force uniqueness across parent/child/sibling
tables.  Just think of them as being nothing more than seperate table that share simlar
data-definitions.

When you select * from parent;  you are essentially preforming a:

select * from parent
union
select * from childa
union
select * from childb ....;

if you want to only see the records in A then select * from only parent;

> - if i isert record in B the record is replaced in A ?
>
> can i avoid this?

This will not happen, you will end up with two records one A and one in B.


> i would like to have this scenario:
>
> table A
>
> table B inheridts from A
> table C inheridts from A
>
> if i insert a record in B it must be insered only in B!
> if i insert a record in C it must be insered only in C!

This is how is it will work.

http://www.postgresql.org/docs/8.1/interactive/ddl-inherit.html
Notice 5.8.1. Caveats

Regards,

Richard Broersma Jr.

Re: inheritance and table

От
Erik Jones
Дата:
nik600 wrote:
> hi
>
> i don't have understand how works inheritance of tables...
>
> if table B inherits from table A
>
> - A and B must share primary keys?
>
> - if i isert record in B the record is replaced in A ?
>
> can i avoid this?
>
> i would like to have this scenario:
>
> table A
>
> table B inheridts from A
> table C inheridts from A
>
> if i insert a record in B it must be insered only in B!
> if i insert a record in C it must be insered only in C!
>
> is it possible?
>
> thanks
Do you mean like this?  (Notice the use of LIKE instead of INHERITS):

CREATE TABLE table_1 (
    a int,
    b int
)

CREATE TABLE table_2 (
    LIKE table_1
)

(**Note:  CREATE TABLE with INHERITS uses different syntax!**)

INSERT INTO table_1 (a, b) VALUES (1, 2);
INSERT INTO table_2 (a, b,) VALUES (3,  4);

Now,

SELECT * FROM table_1;

yeilds,

_a | b_
1 | 2

and not,

_a | b
_1 | 2
3 | 4

as it would've if you'd used INHERITS instead of LIKE.

--
erik jones <erik@myemma.com>
software development
emma(r)


Re: inheritance and table

От
arie nugraha
Дата:
Inheritance in postgre means you will have same fields definition
like the inherited table plus its own fields. So if table B is inherit
table A,
table B will have same field  definition  like A plus  table  B own
unique field(s).

It wont share primary keys, table B just have primary key in the same
field(s) like A
and records in table A wont be replaced by record(s) inserted to table B
or vice versa.

table A and table B basically a different entity, they just have same
fields definition.

Hope that will help you

> hi
>
> i don't have understand how works inheritance of tables...
>
> if table B inherits from table A
>
> - A and B must share primary keys?
>
> - if i isert record in B the record is replaced in A ?
>
> can i avoid this?
>
> i would like to have this scenario:
>
> table A
>
> table B inheridts from A
> table C inheridts from A
>
> if i insert a record in B it must be insered only in B!
> if i insert a record in C it must be insered only in C!
>
> is it possible?
>
> thanks
>
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Have you searched our list archives?
>
>               http://archives.postgresql.org
>


Re: inheritance and table

От
nik600
Дата:
ok i have understand

many thanks to all!

On 6/27/06, Erik Jones <erik@myemma.com> wrote:
> nik600 wrote:
> > hi
> >
> > i don't have understand how works inheritance of tables...
> >
> > if table B inherits from table A
> >
> > - A and B must share primary keys?
> >
> > - if i isert record in B the record is replaced in A ?
> >
> > can i avoid this?
> >
> > i would like to have this scenario:
> >
> > table A
> >
> > table B inheridts from A
> > table C inheridts from A
> >
> > if i insert a record in B it must be insered only in B!
> > if i insert a record in C it must be insered only in C!
> >
> > is it possible?
> >
> > thanks
> Do you mean like this?  (Notice the use of LIKE instead of INHERITS):
>
> CREATE TABLE table_1 (
>     a int,
>     b int
> )
>
> CREATE TABLE table_2 (
>     LIKE table_1
> )
>
> (**Note:  CREATE TABLE with INHERITS uses different syntax!**)
>
> INSERT INTO table_1 (a, b) VALUES (1, 2);
> INSERT INTO table_2 (a, b,) VALUES (3,  4);
>
> Now,
>
> SELECT * FROM table_1;
>
> yeilds,
>
> _a | b_
> 1 | 2
>
> and not,
>
> _a | b
> _1 | 2
> 3 | 4
>
> as it would've if you'd used INHERITS instead of LIKE.
>
> --
> erik jones <erik@myemma.com>
> software development
> emma(r)
>
>