pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Inheritance and primary keys
2+ messages / 2 participants
[nested] [flat]

* Inheritance and primary keys
@ 2002-07-18 08:38 Raphael Bauduin <raphael.bauduin@be.easynet.net>
  0 siblings, 0 replies; 2+ messages in thread

From: Raphael Bauduin @ 2002-07-18 08:38 UTC (permalink / raw)
  To: pgsql-admin

Hi,

I'm developing a little project management tool in which you have contacts
related to a customer, and contacts related to the project.  Both contacts
contains the same data for now, but for my test, I made the asumption that I
would link the contacts directly with the tables customer and project (so
customer_contactshas a field cust_idwhich is a foreig key). This is somwhat
similar to the example given inthe tutorial with cities, some of which being
capitals.

I thus create a table contacts, from which the tables project_contacts
and cutomer_contacts wil inherit:

CREATE TABLE "contacts" (
        "contact_id" serial NOT NULL,
        "contact_first_name" text,
        "contact_name" character varying(100),
        "contact_phone" character varying(100),
        "contact_email" character varying(100),
        "contact_mobile" character varying(100),
        Constraint "contacts_pkey" Primary Key ("contact_id")
);

When creating the children table, the primary key is not inherited, so I
specified a constaint about it (and I removed the foreign key constraint from
the text below):

create table project_contacts ( project_id bigint,Constraint "project_contacts_pkey" Primary Key ("contact_id")) INHERITS (contacts);

create table customer_contacts ( cust_id bigint,Constraint "customer_contacts_pkey" Primary Key ("contact_id")) INHERITS (contacts);


Now the strange thing (for me :-)
When inserting rows in the different tables, I can get several rows with the same contact_id, though they use the same sequence to set the value of the field contact_id as shown below:

\d contacts
                                             Table "contacts"
       Column       |          Type          |                          Modifiers                          
------------------+------------------------+-------------------------------------------------------------
 contact_id         | integer                |not null default nextval('"contacts_contact_id_seq"'::text)


\d customer_contacts;
                                         Table "customer_contacts"
       Column       |          Type          |                          Modifiers                          
--------------------+------------------------+-----------------------------------------------------------
 contact_id         | integer                |not null default nextval('"contacts_contact_id_seq"'::text)

\d project_contacts     
                                         Table "project_contacts"
       Column       |          Type          |                          Modifiers                          
------------------+------------------------+-------------------------------------------------------------
 contact_id         | integer                |not null default nextval('"contacts_contact_id_seq"'::text)


But as shown below, you can have multiple rows with the same contact_id:

select contact_id from contacts;
 contact_id 
------------
          2
          3
          4
          5
          6
          7
          8
          9
         10
         11
          5
          6
          8
          9
         10
         11
         12
         13
         14
         15
         16
         17
         18
         19
         20
          1
(26 rows)


Am I missing something? Should I read a little more about OO databases?

Thanks in advance for your help.

Raph


PS: to those who read the mail to this point, thanks already :-))



^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Inheritance and primary keys
@ 2002-07-18 08:47 Raphael Bauduin <raphael@be.easynet.net>
  0 siblings, 0 replies; 2+ messages in thread

From: Raphael Bauduin @ 2002-07-18 08:47 UTC (permalink / raw)
  To: pgsql-admin

Hi,

I'm developing a little project management tool in which you have contacts
related to a customer, and contacts related to the project.  Both contacts
contains the same data for now, but for my test, I made the asumption that I
would link the contacts directly with the tables customer and project (so
customer_contactshas a field cust_idwhich is a foreig key). This is somwhat
similar to the example given inthe tutorial with cities, some of which being
capitals.

I thus create a table contacts, from which the tables project_contacts
and cutomer_contacts wil inherit:

CREATE TABLE "contacts" (
        "contact_id" serial NOT NULL,
        "contact_first_name" text,   
        "contact_name" character varying(100),
        "contact_phone" character varying(100),
        "contact_email" character varying(100),
        "contact_mobile" character varying(100),
        Constraint "contacts_pkey" Primary Key ("contact_id")
);

When creating the children table, the primary key is not inherited, so I
specified a constaint about it (and I removed the foreign key constraint from
the text below):

create table project_contacts ( project_id bigint,Constraint "project_contacts_pkey" Primary Key
+("contact_id")) INHERITS (contacts);

create table customer_contacts ( cust_id bigint,Constraint "customer_contacts_pkey" Primary Key
+("contact_id")) INHERITS (contacts);


Now the strange thing (for me :-)
When inserting rows in the different tables, I can get several rows with the same contact_id, though they use
+the same sequence to set the value of the field contact_id as shown below:

\d contacts
                                             Table "contacts"
       Column       |          Type          |                          Modifiers
------------------+------------------------+-------------------------------------------------------------
 contact_id         | integer                |not null default nextval('"contacts_contact_id_seq"'::text)


\d customer_contacts;
                                         Table "customer_contacts"
       Column       |          Type          |                          Modifiers
--------------------+------------------------+-----------------------------------------------------------
 contact_id         | integer                |not null default nextval('"contacts_contact_id_seq"'::text)

\d project_contacts
                                         Table "project_contacts"
       Column       |          Type          |                          Modifiers
------------------+------------------------+-------------------------------------------------------------
 contact_id         | integer                |not null default nextval('"contacts_contact_id_seq"'::text)


But as shown below, you can have multiple rows with the same contact_id:

select contact_id from contacts;
 contact_id
------------
          2
          3
          4
          5
          6
          7
          8
          9
         10
         11
          5
          6
          8
          9
         10
         11
         12
         13
         14
         15
         16
         17
         18
         19
         20
          1
(26 rows)


Am I missing something? Should I read a little more about OO databases?

Thanks in advance for your help.

Raph


PS: to those who read the mail to this point, thanks already :-))






^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2002-07-18 08:47 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-07-18 08:38 Inheritance and primary keys Raphael Bauduin <raphael.bauduin@be.easynet.net>
2002-07-18 08:47 Inheritance and primary keys Raphael Bauduin <raphael@be.easynet.net>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox