pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: stan <stanb@panix.com>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: Subject: Re: constrain with MATCH full and NULL values in referenced table
Date: Mon, 12 Aug 2019 10:40:20 -0700
Message-ID: <402afcca-08f0-0b10-0ffb-b6d824c6b45e@aklaver.com> (raw)
In-Reply-To: <20190812173006.GA7923@panix.com>
References: <20190812170621.GA7359@panix.com>
	<3d326318-0b75-f499-1865-5cbbcc15dae3@aklaver.com>
	<20190812173006.GA7923@panix.com>

On 8/12/19 10:30 AM, stan wrote:
>>> The task_instance table is the one the original question was in reference
>>> to.
>>>
>>> CREATE TABLE employee (
>>>       employee_key            integer DEFAULT nextval('employee_key_serial')
>>>       PRIMARY KEY ,
>>>       id                     varchar(5)  NOT NULL UNIQUE ,
>>>       first_name             varchar  NOT NULL,
>>> );
>>>
>>> CREATE TABLE work_type (
>>>       work_type_key      integer DEFAULT nextval('work_type_key_serial')
>>>       PRIMARY KEY ,
>>>       type               smallint UNIQUE ,
>>>       descrip            varchar UNIQUE ,
>>>       modtime            timestamptz DEFAULT current_timestamp
>>> );
>>>
>>> CREATE TABLE rate (
>>>       employee_key       integer NOT NULL,
>>>       work_type_key      integer NOT NULL,
>>>       rate 	       numeric (5, 2) NOT NULL,
>>>       descrip            varchar ,
>>>       modtime            timestamptz DEFAULT current_timestamp ,
>>>       FOREIGN KEY (employee_key) references employee(employee_key) ,
>>>       FOREIGN KEY (work_type_key) references work_type(work_type_key) ,
>>>       CONSTRAINT rate_constraint UNIQUE (employee_key , work_type_key)
>>> );
>>>
>>>
>>> CREATE TABLE task_instance (
>>>       task_instance      integer DEFAULT nextval('task_instance_key_serial')
>>>       PRIMARY KEY ,
>>>       project_key        integer NOT NULL ,
>>>       employee_key       integer NOT NULL ,
>>>       work_type_key      integer NOT NULL ,
>>>       hours 	       numeric (5, 2) NOT NULL ,
>>>       work_start         timestamptz ,
>>>       work_end           timestamptz ,
>>>       modtime            timestamptz DEFAULT current_timestamp ,
>>>       descrip            varchar ,
>>
>> Aren't the marked ones below redundant?:
>>
>>>       FOREIGN KEY (employee_key) references employee(employee_key) ,
>>         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>>>       FOREIGN KEY (project_key) references project(project_key) ,
>>>       FOREIGN KEY (work_type_key) references work_type(work_type_key) ,
>>         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>>>       FOREIGN KEY (work_type_key , employee_key) REFERENCES rate (work_type_key , employee_key) MATCH FULL
>>
>> They are covered above.
>>
>>> );
> 
> OK, looks like learning time for me, again. Which is alwasy a good thing. My
> thought here was that I needed to specify these on the dreivative table
> (task_instnce). Are you teaching me that, since these constraints exist on
> the tables that rate is derived from, I do not need to specify thmm for the
> rate table?

If I'm following what you are trying to do then:

1) task_instance is dependent on the information in rate being present 
for a given combination of (work_type_key , employee_key).

2) If 1) is correct then you cannot create a record in task_instance 
until a record exists in rate.

3) 2) means you have already established a relationship to employee and 
work_type via rate.

> 
> The purpose of those is to verify that the key being inserted already exists
> in the parent (eg employee) table.
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com





view thread (5+ messages)  latest in thread

Message-ID: <402afcca-08f0-0b10-0ffb-b6d824c6b45e@aklaver.com>
Permalink:  ../402afcca-08f0-0b10-0ffb-b6d824c6b45e@aklaver.com/
Also on:    postgresql.org/message-id/402afcca-08f0-0b10-0ffb-b6d824c6b45e@aklaver.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: adrian.klaver@aklaver.com, stanb@panix.com
  Subject: Re: Subject: Re: constrain with MATCH full and NULL values in referenced table
  In-Reply-To: <402afcca-08f0-0b10-0ffb-b6d824c6b45e@aklaver.com>
  References: <20190812170621.GA7359@panix.com> <3d326318-0b75-f499-1865-5cbbcc15dae3@aklaver.com> <20190812173006.GA7923@panix.com> <402afcca-08f0-0b10-0ffb-b6d824c6b45e@aklaver.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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