pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Comparing dates in DDL
21+ messages / 6 participants
[nested] [flat]

* Comparing dates in DDL
@ 2019-01-04 16:53  Rich Shepard <rshepard@appl-ecosys.com>
  0 siblings, 2 replies; 21+ messages in thread

From: Rich Shepard @ 2019-01-04 16:53 UTC (permalink / raw)
  To: pgsql-general

   I have a projects table that includes these two columns:

start_date date DEFAULT CURRENT_DATE,
end_date date
 	CONSTRAINT valid_start_date
 	CHECK (start_date <= end_date),

   1. Do I need a DEFAULT value for the end_date?
   2. If so, please suggest a value for it.

TIA,

Rich




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

* Re: Comparing dates in DDL
@ 2019-01-04 17:12  Igor Korot <ikorot01@gmail.com>
  parent: Rich Shepard <rshepard@appl-ecosys.com>
  1 sibling, 2 replies; 21+ messages in thread

From: Igor Korot @ 2019-01-04 17:12 UTC (permalink / raw)
  To: Rich Shepard <rshepard@appl-ecosys.com>; +Cc: pgsql-general

Hi, Rich,

On Fri, Jan 4, 2019 at 10:53 AM Rich Shepard <rshepard@appl-ecosys.com> wrote:
>
>    I have a projects table that includes these two columns:
>
> start_date date DEFAULT CURRENT_DATE,
> end_date date
>         CONSTRAINT valid_start_date
>         CHECK (start_date <= end_date),
>
>    1. Do I need a DEFAULT value for the end_date?
>    2. If so, please suggest a value for it.

start_date.day() + 1?

Thank you.

>
> TIA,
>
> Rich
>




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

* Re: Comparing dates in DDL
@ 2019-01-04 17:16  Andreas Kretschmer <andreas@a-kretschmer.de>
  parent: Rich Shepard <rshepard@appl-ecosys.com>
  1 sibling, 1 reply; 21+ messages in thread

From: Andreas Kretschmer @ 2019-01-04 17:16 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org



Am 04.01.19 um 17:53 schrieb Rich Shepard:
> I have a projects table that includes these two columns:
>
> start_date date DEFAULT CURRENT_DATE,
> end_date date
>     CONSTRAINT valid_start_date
>     CHECK (start_date <= end_date),
>
>   1. Do I need a DEFAULT value for the end_date?

no, you can use NULL, for instance. You don't need an explicit value.
But maybe you want to set the start_date to NOT NULL.

> 2. If so, please suggest a value for it.

other solution for such 2 fields: you can use DATERANGE, only one field.


Regards, Andreas

-- 
2ndQuadrant - The PostgreSQL Support Company.
www.2ndQuadrant.com





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

* Re: Comparing dates in DDL
@ 2019-01-04 17:18  Rob Sargent <robjsargent@gmail.com>
  parent: Igor Korot <ikorot01@gmail.com>
  1 sibling, 2 replies; 21+ messages in thread

From: Rob Sargent @ 2019-01-04 17:18 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org


On 1/4/19 10:12 AM, Igor Korot wrote:
> Hi, Rich,
>
> On Fri, Jan 4, 2019 at 10:53 AM Rich Shepard <rshepard@appl-ecosys.com> wrote:
>>     I have a projects table that includes these two columns:
>>
>> start_date date DEFAULT CURRENT_DATE,
>> end_date date
>>          CONSTRAINT valid_start_date
>>          CHECK (start_date <= end_date),
>>
>>     1. Do I need a DEFAULT value for the end_date?
>>     2. If so, please suggest a value for it.
> start_date.day() + 1?
>
> Thank you.
>
>> TIA,
>>
>> Rich
>>

Is the end_date always knowable at record insert?

CHECK(end_date is null or start_date <= end_date)





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

* Re: Comparing dates in DDL
@ 2019-01-04 17:21  Rich Shepard <rshepard@appl-ecosys.com>
  parent: Igor Korot <ikorot01@gmail.com>
  1 sibling, 0 replies; 21+ messages in thread

From: Rich Shepard @ 2019-01-04 17:21 UTC (permalink / raw)
  To: pgsql-general

On Fri, 4 Jan 2019, Igor Korot wrote:

>>    1. Do I need a DEFAULT value for the end_date?
>>    2. If so, please suggest a value for it.
>
> start_date.day() + 1?

   Thanks, Igor. I did not pick up this syntax when I looked at data types
and their DDL usage.

Regards,

Rich




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

* Re: Comparing dates in DDL
@ 2019-01-04 17:24  David G. Johnston <david.g.johnston@gmail.com>
  parent: Rob Sargent <robjsargent@gmail.com>
  1 sibling, 1 reply; 21+ messages in thread

From: David G. Johnston @ 2019-01-04 17:24 UTC (permalink / raw)
  To: Rob Sargent <robjsargent@gmail.com>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

On Friday, January 4, 2019, Rob Sargent <robjsargent@gmail.com> wrote:
>
> CHECK(end_date is null or start_date <= end_date)
>

The is null expression is redundant since check constraints pass when the
result is unknown.

David J.

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

* Re: Comparing dates in DDL
@ 2019-01-04 17:26  Rich Shepard <rshepard@appl-ecosys.com>
  parent: Rob Sargent <robjsargent@gmail.com>
  1 sibling, 1 reply; 21+ messages in thread

From: Rich Shepard @ 2019-01-04 17:26 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

On Fri, 4 Jan 2019, Rob Sargent wrote:

> Is the end_date always knowable at record insert?

Rob,

   Not always. Sometimes projects have known end dates, other times the end
is interminate until it happens.

> CHECK(end_date is null or start_date <= end_date)

   So a default of NULL should be applied, or just allowed to happen?

Thanks,

Rich




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

* Re: Comparing dates in DDL
@ 2019-01-04 17:30  Rich Shepard <rshepard@appl-ecosys.com>
  parent: David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 1 reply; 21+ messages in thread

From: Rich Shepard @ 2019-01-04 17:30 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

On Fri, 4 Jan 2019, David G. Johnston wrote:

> The is null expression is redundant since check constraints pass when the
> result is unknown.

David,

   I wondered about this since NULL can be missing, unknown, or otherwise
defined. Are there benefits to allowing an empty value in that column when
checking that it's later than the start date rather than explicitly setting
a default date after the start date?

Regards,

Rich




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

* Re: Comparing dates in DDL
@ 2019-01-04 17:32  Rich Shepard <rshepard@appl-ecosys.com>
  parent: Andreas Kretschmer <andreas@a-kretschmer.de>
  0 siblings, 1 reply; 21+ messages in thread

From: Rich Shepard @ 2019-01-04 17:32 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

On Fri, 4 Jan 2019, Andreas Kretschmer wrote:

> no, you can use NULL, for instance. You don't need an explicit value.
> But maybe you want to set the start_date to NOT NULL.

Andreas,

   Yes, I added NOT NULL to the start_date column.

>> 2. If so, please suggest a value for it.
>
> other solution for such 2 fields: you can use DATERANGE, only one field.

   Only if all projects have a known end_date; some don't.

Thanks,

Rich




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

* Re: Comparing dates in DDL
@ 2019-01-04 17:33  David G. Johnston <david.g.johnston@gmail.com>
  parent: Rich Shepard <rshepard@appl-ecosys.com>
  0 siblings, 1 reply; 21+ messages in thread

From: David G. Johnston @ 2019-01-04 17:33 UTC (permalink / raw)
  To: Rich Shepard <rshepard@appl-ecosys.com>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

On Friday, January 4, 2019, Rich Shepard <rshepard@appl-ecosys.com> wrote:

> On Fri, 4 Jan 2019, David G. Johnston wrote:
>
> The is null expression is redundant since check constraints pass when the
>> result is unknown.
>>
>
> David,
>
>   I wondered about this since NULL can be missing, unknown, or otherwise
> defined. Are there benefits to allowing an empty value in that column when
> checking that it's later than the start date rather than explicitly setting
> a default date after the start date?
>
>
I don’t understand the question...

David J.

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

* Re: Comparing dates in DDL
@ 2019-01-04 17:36  Rich Shepard <rshepard@appl-ecosys.com>
  parent: David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 1 reply; 21+ messages in thread

From: Rich Shepard @ 2019-01-04 17:36 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

On Fri, 4 Jan 2019, David G. Johnston wrote:

>>   I wondered about this since NULL can be missing, unknown, or otherwise
>> defined. Are there benefits to allowing an empty value in that column when
>> checking that it's later than the start date rather than explicitly setting
>> a default date after the start date?
>>
>>
> I don’t understand the question...

David,

   Understandable. :-)

   Perhaps this is more clear: is there a difference between not specifying a
default end_date value and specifying NULL as the default end_date value?

Regards,

Rich




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

* Re: Comparing dates in DDL
@ 2019-01-04 17:48  David G. Johnston <david.g.johnston@gmail.com>
  parent: Rich Shepard <rshepard@appl-ecosys.com>
  0 siblings, 1 reply; 21+ messages in thread

From: David G. Johnston @ 2019-01-04 17:48 UTC (permalink / raw)
  To: Rich Shepard <rshepard@appl-ecosys.com>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

On Friday, January 4, 2019, Rich Shepard <rshepard@appl-ecosys.com> wrote:

> On Fri, 4 Jan 2019, David G. Johnston wrote:
>
>   I wondered about this since NULL can be missing, unknown, or otherwise
>>> defined. Are there benefits to allowing an empty value in that column
>>> when
>>> checking that it's later than the start date rather than explicitly
>>> setting
>>> a default date after the start date?
>>>
>>>
>>> I don’t understand the question...
>>
>
> David,
>
>   Understandable. :-)
>
>   Perhaps this is more clear: is there a difference between not specifying
> a
> default end_date value and specifying NULL as the default end_date value?
>
>
No.  If no default is available and a value for the field is not provided
the stored value will be null; a default of null is thus also redundant
specification.

David J.

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

* Re: Comparing dates in DDL
@ 2019-01-04 17:53  Rob Sargent <robjsargent@gmail.com>
  parent: Rich Shepard <rshepard@appl-ecosys.com>
  0 siblings, 0 replies; 21+ messages in thread

From: Rob Sargent @ 2019-01-04 17:53 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org


On 1/4/19 10:26 AM, Rich Shepard wrote:
> On Fri, 4 Jan 2019, Rob Sargent wrote:
>
>> Is the end_date always knowable at record insert?
>
> Rob,
>
>   Not always. Sometimes projects have known end dates, other times the 
> end
> is interminate until it happens.
>
>> CHECK(end_date is null or start_date <= end_date)
>
>   So a default of NULL should be applied, or just allowed to happen?
>
> Thanks,
>
> Rich
>
This is exactly what null is for.  Much preferable to some arbitrary 
value which will confuse analyses.




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

* Re: Comparing dates in DDL
@ 2019-01-04 17:59  Rich Shepard <rshepard@appl-ecosys.com>
  parent: David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 0 replies; 21+ messages in thread

From: Rich Shepard @ 2019-01-04 17:59 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

On Fri, 4 Jan 2019, David G. Johnston wrote:

> No. If no default is available and a value for the field is not provided
> the stored value will be null; a default of null is thus also redundant
> specification.

David,

   Thanks for clarifying.

Regards,

Rich




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

* Re: Comparing dates in DDL
@ 2019-01-04 20:30  Andreas Kretschmer <andreas@a-kretschmer.de>
  parent: Rich Shepard <rshepard@appl-ecosys.com>
  0 siblings, 1 reply; 21+ messages in thread

From: Andreas Kretschmer @ 2019-01-04 20:30 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org



Am 04.01.19 um 18:32 schrieb Rich Shepard:
>> other solution for such 2 fields: you can use DATERANGE, only one field.
>
>   Only if all projects have a known end_date; some don't.

that's not a problem:

test=*# create table projects(duration daterange default 
daterange(current_date,null) check(lower(duration) is not null));
CREATE TABLE


Regards, Andreas

-- 
2ndQuadrant - The PostgreSQL Support Company.
www.2ndQuadrant.com





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

* Re: Comparing dates in DDL
@ 2019-01-04 21:12  Rich Shepard <rshepard@appl-ecosys.com>
  parent: Andreas Kretschmer <andreas@a-kretschmer.de>
  0 siblings, 1 reply; 21+ messages in thread

From: Rich Shepard @ 2019-01-04 21:12 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

On Fri, 4 Jan 2019, Andreas Kretschmer wrote:

>>   Only if all projects have a known end_date; some don't.
>
> that's not a problem:
>
> test=*# create table projects(duration daterange default 
> daterange(current_date,null) check(lower(duration) is not null));

Andreas,

   Thank you. That's a data type I've not before used.

Regards,

Rich




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

* Re: Comparing dates in DDL
@ 2019-01-04 21:21  Rich Shepard <rshepard@appl-ecosys.com>
  parent: Rich Shepard <rshepard@appl-ecosys.com>
  0 siblings, 1 reply; 21+ messages in thread

From: Rich Shepard @ 2019-01-04 21:21 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

On Fri, 4 Jan 2019, Rich Shepard wrote:

>  Thank you. That's a data type I've not before used.

Andreas,

   Thinking more about duration perhaps I'm seeing a problem that really does
not exist: it's a single column for both dates in the table while the UI
needs separate date data entry widgets. Unless I use middleware code when a
project row is first entered I am not seeing how two discrete dates are
combined with sqlalchemy inserts them into the table.

Regards,

Rich





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

* Re: Comparing dates in DDL
@ 2019-01-04 21:52  David G. Johnston <david.g.johnston@gmail.com>
  parent: Rich Shepard <rshepard@appl-ecosys.com>
  0 siblings, 1 reply; 21+ messages in thread

From: David G. Johnston @ 2019-01-04 21:52 UTC (permalink / raw)
  To: Rich Shepard <rshepard@appl-ecosys.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

On Fri, Jan 4, 2019 at 2:21 PM Rich Shepard <rshepard@appl-ecosys.com> wrote:
>    Thinking more about duration perhaps I'm seeing a problem that really does
> not exist: it's a single column for both dates in the table while the UI
> needs separate date data entry widgets. Unless I use middleware code when a
> project row is first entered I am not seeing how two discrete dates are
> combined with sqlalchemy inserts them into the table.

That would be the decision to make - does your toolkit support (or can
be made to support) the type and are you willing to choose a
sub-optimal database model because one or more applications happen to
do things differently?

IMO the daterange datatype is the best type you can choose for the
model; now you have to figure out and decide where any tradeoffs are
and if they are worth it given your specific circumstances.

David J.




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

* Re: Comparing dates in DDL
@ 2019-01-04 22:19  Rich Shepard <rshepard@appl-ecosys.com>
  parent: David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 1 reply; 21+ messages in thread

From: Rich Shepard @ 2019-01-04 22:19 UTC (permalink / raw)
  To: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

On Fri, 4 Jan 2019, David G. Johnston wrote:

> That would be the decision to make - does your toolkit support (or can be
> made to support) the type and are you willing to choose a sub-optimal
> database model because one or more applications happen to do things
> differently?
>
> IMO the daterange datatype is the best type you can choose for the model;
> now you have to figure out and decide where any tradeoffs are and if they
> are worth it given your specific circumstances.

David,

   Thanks for the insights.

Regards,

Rich




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

* Re: Comparing dates in DDL
@ 2019-01-04 23:49  Jeremy Finzel <finzelj@gmail.com>
  parent: Rich Shepard <rshepard@appl-ecosys.com>
  0 siblings, 1 reply; 21+ messages in thread

From: Jeremy Finzel @ 2019-01-04 23:49 UTC (permalink / raw)
  To: Rich Shepard <rshepard@appl-ecosys.com>; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

On Fri, Jan 4, 2019 at 4:19 PM Rich Shepard <rshepard@appl-ecosys.com>
wrote:

> On Fri, 4 Jan 2019, David G. Johnston wrote:
>
> > That would be the decision to make - does your toolkit support (or can be
> > made to support) the type and are you willing to choose a sub-optimal
> > database model because one or more applications happen to do things
> > differently?
> >
> > IMO the daterange datatype is the best type you can choose for the model;
> > now you have to figure out and decide where any tradeoffs are and if they
> > are worth it given your specific circumstances.
>
> David,
>
>    Thanks for the insights.
>
> Regards,
>
> Rich



Another suggestion which hasn’t been mentioned is using ‘infinity’ as the
end date. I like this because it IMO indicates that the record is clearly
the current valid record more than null.

But I’m not sure exactly what you are trying to do either. If you are
creating a new record with changes and closing (ending) the range of the
original record, then a GIST exclusion index would ensure you have no
overlapping date ranges for all historical records put together.

Thanks,
Jeremy

>

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

* Re: Comparing dates in DDL
@ 2019-01-04 23:59  Rich Shepard <rshepard@appl-ecosys.com>
  parent: Jeremy Finzel <finzelj@gmail.com>
  0 siblings, 0 replies; 21+ messages in thread

From: Rich Shepard @ 2019-01-04 23:59 UTC (permalink / raw)
  To: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

On Fri, 4 Jan 2019, Jeremy Finzel wrote:

> Another suggestion which hasn’t been mentioned is using ‘infinity’ as the
> end date. I like this because it IMO indicates that the record is clearly
> the current valid record more than null.

Jeremy,

   I believe that infinity was mentioned in this thread.

> But I’m not sure exactly what you are trying to do either. If you are
> creating a new record with changes and closing (ending) the range of the
> original record, then a GIST exclusion index would ensure you have no
> overlapping date ranges for all historical records put together.

   Having overlapping date ranges in a projects table is a Good Thing(TM)
because it means I have multiple, concurrent revenue streams. :-)

Carpe weekend,

Rich

--
I dream of an America where a chicken can cross the road without having its
motives questioned.




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


end of thread, other threads:[~2019-01-04 23:59 UTC | newest]

Thread overview: 21+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-01-04 16:53 Comparing dates in DDL Rich Shepard <rshepard@appl-ecosys.com>
2019-01-04 17:12 ` Igor Korot <ikorot01@gmail.com>
2019-01-04 17:18   ` Rob Sargent <robjsargent@gmail.com>
2019-01-04 17:24     ` David G. Johnston <david.g.johnston@gmail.com>
2019-01-04 17:30       ` Rich Shepard <rshepard@appl-ecosys.com>
2019-01-04 17:33         ` David G. Johnston <david.g.johnston@gmail.com>
2019-01-04 17:36           ` Rich Shepard <rshepard@appl-ecosys.com>
2019-01-04 17:48             ` David G. Johnston <david.g.johnston@gmail.com>
2019-01-04 17:59               ` Rich Shepard <rshepard@appl-ecosys.com>
2019-01-04 17:26     ` Rich Shepard <rshepard@appl-ecosys.com>
2019-01-04 17:53       ` Rob Sargent <robjsargent@gmail.com>
2019-01-04 17:21   ` Rich Shepard <rshepard@appl-ecosys.com>
2019-01-04 17:16 ` Andreas Kretschmer <andreas@a-kretschmer.de>
2019-01-04 17:32   ` Rich Shepard <rshepard@appl-ecosys.com>
2019-01-04 20:30     ` Andreas Kretschmer <andreas@a-kretschmer.de>
2019-01-04 21:12       ` Rich Shepard <rshepard@appl-ecosys.com>
2019-01-04 21:21         ` Rich Shepard <rshepard@appl-ecosys.com>
2019-01-04 21:52           ` David G. Johnston <david.g.johnston@gmail.com>
2019-01-04 22:19             ` Rich Shepard <rshepard@appl-ecosys.com>
2019-01-04 23:49               ` Jeremy Finzel <finzelj@gmail.com>
2019-01-04 23:59                 ` Rich Shepard <rshepard@appl-ecosys.com>

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