Re: add foreign key constraint after table creation?

From: Dani Oderbolz <oderbolz(at)ecologic(dot)de>
To: pgsql-novice(at)postgresql(dot)org
Subject: Re: add foreign key constraint after table creation?
Date: 2003-05-27 08:09:19
Message-ID: 3ED31D2F.70909@ecologic.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

>
>
>CREATE TABLE people (
> n_id SERIAL PRIMARY KEY,
> n_object_id INTEGER DEFAULT NULL
> references objects
> ON UPDATE CASCADE
> ON DELETE SET NULL,
> n_objects_counter INTEGER DEFAULT 0,
> b_hidden BOOLEAN DEFAULT false,
> dt_created TIMESTAMP DEFAULT current_timestamp,
> dt_modified TIMESTAMP DEFAULT current_timestamp,
> t_nickname VARCHAR(20) UNIQUE NOT NULL,
> t_firstname VARCHAR(20) NOT NULL,
> t_surname VARCHAR(25) NOT NULL,
> t_mobile VARCHAR(15) UNIQUE,
> t_email VARCHAR(30) UNIQUE,
> b_registered BOOLEAN DEFAULT false,
> n_email_status INT2 DEFAULT 0,
> n_txt_status INT2 DEFAULT 0,
> b_work_hours BOOLEAN DEFAULT false
>);
>
>CREATE TABLE objects (
> n_id SERIAL PRIMARY KEY,
> t_text_id VARCHAR(25) UNIQUE NOT NULL,
> b_hidden BOOLEAN DEFAULT false,
> dt_created TIMESTAMP DEFAULT current_timestamp,
> dt_modified TIMESTAMP DEFAULT current_timestamp,
> n_creator INTEGER NOT NULL
> references people
> ON UPDATE CASCADE
> ON DELETE CASCADE,
> n_type INT2 NOT NULL DEFAULT 0,
> t_name VARCHAR(30) NOT NULL,
> t_description VARCHAR(200),
> t_location VARCHAR(100) DEFAULT NULL,
> t_postcode VARCHAR(10) DEFAULT NULL,
> n_id_photo INTEGER DEFAULT NULL
> references photo
> ON UPDATE CASCADE
> ON DELETE SET NULL
>
>);
>...
>
>
Hi Rory,
maybe you should review your design.
I think you are recording a redundancy:
As soon as a given obect_id is recorded in the person,
you know that this person is the creator (ok,
maybe I am nor fully understanding your schema?).
If you need your design, it might help to treat the
relationship as if it was a many to many,
and putting a third table in between with just
(person_id, object_id, is_creator).

Just my 2 cents.

Cheers, Dani

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Rory Campbell-Lange 2003-05-27 09:36:01 Re: Functions or Rules and Views?
Previous Message Israel Calderon 2003-05-27 02:31:38 Double quoted column within CREATE TABLE statement