pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedImplementing standard SQL's DOMAIN constraint
9+ messages / 3 participants
[nested] [flat]
* Implementing standard SQL's DOMAIN constraint
@ 2019-01-02 17:12 Rich Shepard <rshepard@appl-ecosys.com>
2019-01-02 17:34 ` Re: Implementing standard SQL's DOMAIN constraint David G. Johnston <david.g.johnston@gmail.com>
0 siblings, 1 reply; 9+ messages in thread
From: Rich Shepard @ 2019-01-02 17:12 UTC (permalink / raw)
To: pgsql-general
Happy New Year all,
My readings taught me that standard SQL has a domain constraint that checks
for the same valid characters in a column common to multiple tables.
Example:
CREATE DOMAIN state_code AS char(2)
DEFAULT '??'
CONSTRAINT valid_state_code
CHECK (value IN ('AL', 'AK', 'AZ', ...));
This applies to all tables each having a column named state_code.
I see the value of this feature when multiple tables have start_date and
end_date columns with a constraint that ensures the start_date is <= to the
end date.
Reading the release 10 manual I find many constraints and I want to learn
which one will implement this feature. A pointer is needed.
Rich
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: Implementing standard SQL's DOMAIN constraint
2019-01-02 17:12 Implementing standard SQL's DOMAIN constraint Rich Shepard <rshepard@appl-ecosys.com>
@ 2019-01-02 17:34 ` David G. Johnston <david.g.johnston@gmail.com>
2019-01-02 17:47 ` Re: Implementing standard SQL's DOMAIN constraint Rich Shepard <rshepard@appl-ecosys.com>
0 siblings, 1 reply; 9+ messages in thread
From: David G. Johnston @ 2019-01-02 17:34 UTC (permalink / raw)
To: Rich Shepard <rshepard@appl-ecosys.com>; +Cc: pgsql-general
On Wednesday, January 2, 2019, Rich Shepard <rshepard@appl-ecosys.com>
wrote:
>
> CREATE DOMAIN state_code AS char(2)
> DEFAULT '??'
> CONSTRAINT valid_state_code
> CHECK (value IN ('AL', 'AK', 'AZ', ...));
>
> This applies to all tables each having a column named state_code.
>
There is no magic name logic involved. A domain is just a type with
inherent constraints that are user definable. You make use of it like any
other type.
Create table tbl (
column_name state_code not null
)
Values stored in column_name are now of type state_code and constrained to
be one of the check constraint values.
David J.
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: Implementing standard SQL's DOMAIN constraint
2019-01-02 17:12 Implementing standard SQL's DOMAIN constraint Rich Shepard <rshepard@appl-ecosys.com>
2019-01-02 17:34 ` Re: Implementing standard SQL's DOMAIN constraint David G. Johnston <david.g.johnston@gmail.com>
@ 2019-01-02 17:47 ` Rich Shepard <rshepard@appl-ecosys.com>
2019-01-02 17:52 ` Re: Implementing standard SQL's DOMAIN constraint David G. Johnston <david.g.johnston@gmail.com>
0 siblings, 1 reply; 9+ messages in thread
From: Rich Shepard @ 2019-01-02 17:47 UTC (permalink / raw)
To: pgsql-general
On Wed, 2 Jan 2019, David G. Johnston wrote:
> There is no magic name logic involved. A domain is just a type with
> inherent constraints that are user definable. You make use of it like any
> other type.
>
> Create table tbl (
> column_name state_code not null
> )
>
> Values stored in column_name are now of type state_code and constrained to
> be one of the check constraint values.
David,
I'm not following you. I have two tables each with a column,
state_code char(2) NOT NULL.
Do you mean that I need to write the column constraint for each table? If
not, I don't see from your response how to implement the multi-table
constraint for this column.
Regards,
Rich
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: Implementing standard SQL's DOMAIN constraint
2019-01-02 17:12 Implementing standard SQL's DOMAIN constraint Rich Shepard <rshepard@appl-ecosys.com>
2019-01-02 17:34 ` Re: Implementing standard SQL's DOMAIN constraint David G. Johnston <david.g.johnston@gmail.com>
2019-01-02 17:47 ` Re: Implementing standard SQL's DOMAIN constraint Rich Shepard <rshepard@appl-ecosys.com>
@ 2019-01-02 17:52 ` David G. Johnston <david.g.johnston@gmail.com>
2019-01-02 17:58 ` Re: Implementing standard SQL's DOMAIN constraint Rich Shepard <rshepard@appl-ecosys.com>
0 siblings, 1 reply; 9+ messages in thread
From: David G. Johnston @ 2019-01-02 17:52 UTC (permalink / raw)
To: Rich Shepard <rshepard@appl-ecosys.com>; +Cc: pgsql-general
On Wednesday, January 2, 2019, Rich Shepard <rshepard@appl-ecosys.com>
wrote:
> On Wed, 2 Jan 2019, David G. Johnston wrote:
>
> There is no magic name logic involved. A domain is just a type with
>> inherent constraints that are user definable. You make use of it like any
>> other type.
>>
>> Create table tbl (
>> column_name state_code not null
>> )
>>
>> Values stored in column_name are now of type state_code and constrained to
>> be one of the check constraint values.
>>
>
> David,
>
> I'm not following you. I have two tables each with a column,
> state_code char(2) NOT NULL.
>
> Do you mean that I need to write the column constraint for each table? If
> not, I don't see from your response how to implement the multi-table
> constraint for this column.
>
>
That is a char(2) column for which ‘??’ is a valid value. The fact that it
is named state_code is immaterial; the domain that you created doesn’t get
used. There is no magic linking just by virtue of using the same name.
Change char(2) to state_code if you wish to apply the domain on the column.
David J.
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: Implementing standard SQL's DOMAIN constraint
2019-01-02 17:12 Implementing standard SQL's DOMAIN constraint Rich Shepard <rshepard@appl-ecosys.com>
2019-01-02 17:34 ` Re: Implementing standard SQL's DOMAIN constraint David G. Johnston <david.g.johnston@gmail.com>
2019-01-02 17:47 ` Re: Implementing standard SQL's DOMAIN constraint Rich Shepard <rshepard@appl-ecosys.com>
2019-01-02 17:52 ` Re: Implementing standard SQL's DOMAIN constraint David G. Johnston <david.g.johnston@gmail.com>
@ 2019-01-02 17:58 ` Rich Shepard <rshepard@appl-ecosys.com>
2019-01-02 18:02 ` Re: Implementing standard SQL's DOMAIN constraint David G. Johnston <david.g.johnston@gmail.com>
0 siblings, 1 reply; 9+ messages in thread
From: Rich Shepard @ 2019-01-02 17:58 UTC (permalink / raw)
To: pgsql-general
On Wed, 2 Jan 2019, David G. Johnston wrote:
>> I'm not following you. I have two tables each with a column,
>> state_code char(2) NOT NULL.
> That is a char(2) column for which ‘??’ is a valid value. The fact that it
> is named state_code is immaterial; the domain that you created doesn’t get
> used. There is no magic linking just by virtue of using the same name.
>
> Change char(2) to state_code if you wish to apply the domain on the column.
David,
I think I'm now on your page. In the schema I change the column data type
to state_code, then I add the SQL code creating the domain at the top of the
.sql file. Yes?
Thanks,
Rich
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: Implementing standard SQL's DOMAIN constraint
2019-01-02 17:12 Implementing standard SQL's DOMAIN constraint Rich Shepard <rshepard@appl-ecosys.com>
2019-01-02 17:34 ` Re: Implementing standard SQL's DOMAIN constraint David G. Johnston <david.g.johnston@gmail.com>
2019-01-02 17:47 ` Re: Implementing standard SQL's DOMAIN constraint Rich Shepard <rshepard@appl-ecosys.com>
2019-01-02 17:52 ` Re: Implementing standard SQL's DOMAIN constraint David G. Johnston <david.g.johnston@gmail.com>
2019-01-02 17:58 ` Re: Implementing standard SQL's DOMAIN constraint Rich Shepard <rshepard@appl-ecosys.com>
@ 2019-01-02 18:02 ` David G. Johnston <david.g.johnston@gmail.com>
2019-01-02 18:05 ` Re: Implementing standard SQL's DOMAIN constraint [RESOLVED] Rich Shepard <rshepard@appl-ecosys.com>
0 siblings, 1 reply; 9+ messages in thread
From: David G. Johnston @ 2019-01-02 18:02 UTC (permalink / raw)
To: Rich Shepard <rshepard@appl-ecosys.com>; +Cc: pgsql-general
On Wednesday, January 2, 2019, Rich Shepard <rshepard@appl-ecosys.com>
wrote:
> On Wed, 2 Jan 2019, David G. Johnston wrote:
>
> I'm not following you. I have two tables each with a column,
>>> state_code char(2) NOT NULL.
>>>
>>
> That is a char(2) column for which ‘??’ is a valid value. The fact that it
>> is named state_code is immaterial; the domain that you created doesn’t get
>> used. There is no magic linking just by virtue of using the same name.
>>
>> Change char(2) to state_code if you wish to apply the domain on the
>> column.
>>
>
> David,
>
> I think I'm now on your page. In the schema I change the column data type
> to state_code, then I add the SQL code creating the domain at the top of
> the
> .sql file. Yes?
>
>
You add the create domain command once before any objects that make use of
it. If you only have one .sql file then at the top of it works.
David J.
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: Implementing standard SQL's DOMAIN constraint [RESOLVED]
2019-01-02 17:12 Implementing standard SQL's DOMAIN constraint Rich Shepard <rshepard@appl-ecosys.com>
2019-01-02 17:34 ` Re: Implementing standard SQL's DOMAIN constraint David G. Johnston <david.g.johnston@gmail.com>
2019-01-02 17:47 ` Re: Implementing standard SQL's DOMAIN constraint Rich Shepard <rshepard@appl-ecosys.com>
2019-01-02 17:52 ` Re: Implementing standard SQL's DOMAIN constraint David G. Johnston <david.g.johnston@gmail.com>
2019-01-02 17:58 ` Re: Implementing standard SQL's DOMAIN constraint Rich Shepard <rshepard@appl-ecosys.com>
2019-01-02 18:02 ` Re: Implementing standard SQL's DOMAIN constraint David G. Johnston <david.g.johnston@gmail.com>
@ 2019-01-02 18:05 ` Rich Shepard <rshepard@appl-ecosys.com>
2019-01-02 18:13 ` Re: Implementing standard SQL's DOMAIN constraint [RESOLVED] Ron <ronljohnsonjr@gmail.com>
0 siblings, 1 reply; 9+ messages in thread
From: Rich Shepard @ 2019-01-02 18:05 UTC (permalink / raw)
To: pgsql-general
On Wed, 2 Jan 2019, David G. Johnston wrote:
> You add the create domain command once before any objects that make use of
> it.
David,
This is the answer I sought: postgres supports the create domain command.
I did not see this in your first response.
Thanks very much,
Rich
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: Implementing standard SQL's DOMAIN constraint [RESOLVED]
2019-01-02 17:12 Implementing standard SQL's DOMAIN constraint Rich Shepard <rshepard@appl-ecosys.com>
2019-01-02 17:34 ` Re: Implementing standard SQL's DOMAIN constraint David G. Johnston <david.g.johnston@gmail.com>
2019-01-02 17:47 ` Re: Implementing standard SQL's DOMAIN constraint Rich Shepard <rshepard@appl-ecosys.com>
2019-01-02 17:52 ` Re: Implementing standard SQL's DOMAIN constraint David G. Johnston <david.g.johnston@gmail.com>
2019-01-02 17:58 ` Re: Implementing standard SQL's DOMAIN constraint Rich Shepard <rshepard@appl-ecosys.com>
2019-01-02 18:02 ` Re: Implementing standard SQL's DOMAIN constraint David G. Johnston <david.g.johnston@gmail.com>
2019-01-02 18:05 ` Re: Implementing standard SQL's DOMAIN constraint [RESOLVED] Rich Shepard <rshepard@appl-ecosys.com>
@ 2019-01-02 18:13 ` Ron <ronljohnsonjr@gmail.com>
2019-01-02 18:28 ` Re: Implementing standard SQL's DOMAIN constraint [RESOLVED] Rich Shepard <rshepard@appl-ecosys.com>
0 siblings, 1 reply; 9+ messages in thread
From: Ron @ 2019-01-02 18:13 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
On 1/2/19 12:05 PM, Rich Shepard wrote:
> On Wed, 2 Jan 2019, David G. Johnston wrote:
>
>> You add the create domain command once before any objects that make use of
>> it.
>
> David,
>
> This is the answer I sought: postgres supports the create domain command.
> I did not see this in your first response.
Note that a CHECK constraint with 50 items is a *Very Bad Idea*, since
changing such a constraint is very painful. Use a FK constraint instead.
--
Angular momentum makes the world go 'round.
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: Implementing standard SQL's DOMAIN constraint [RESOLVED]
2019-01-02 17:12 Implementing standard SQL's DOMAIN constraint Rich Shepard <rshepard@appl-ecosys.com>
2019-01-02 17:34 ` Re: Implementing standard SQL's DOMAIN constraint David G. Johnston <david.g.johnston@gmail.com>
2019-01-02 17:47 ` Re: Implementing standard SQL's DOMAIN constraint Rich Shepard <rshepard@appl-ecosys.com>
2019-01-02 17:52 ` Re: Implementing standard SQL's DOMAIN constraint David G. Johnston <david.g.johnston@gmail.com>
2019-01-02 17:58 ` Re: Implementing standard SQL's DOMAIN constraint Rich Shepard <rshepard@appl-ecosys.com>
2019-01-02 18:02 ` Re: Implementing standard SQL's DOMAIN constraint David G. Johnston <david.g.johnston@gmail.com>
2019-01-02 18:05 ` Re: Implementing standard SQL's DOMAIN constraint [RESOLVED] Rich Shepard <rshepard@appl-ecosys.com>
2019-01-02 18:13 ` Re: Implementing standard SQL's DOMAIN constraint [RESOLVED] Ron <ronljohnsonjr@gmail.com>
@ 2019-01-02 18:28 ` Rich Shepard <rshepard@appl-ecosys.com>
0 siblings, 0 replies; 9+ messages in thread
From: Rich Shepard @ 2019-01-02 18:28 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
On Wed, 2 Jan 2019, Ron wrote:
> Note that a CHECK constraint with 50 items is a *Very Bad Idea*, since
> changing such a constraint is very painful. Use a FK constraint instead.
Ron,
It's even longer with Canadian provinces included. I gratefully accept
your advice and will use a table and foreign key instead.
Regards,
Rich
^ permalink raw reply [nested|flat] 9+ messages in thread
end of thread, other threads:[~2019-01-02 18:28 UTC | newest]
Thread overview: 9+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-01-02 17:12 Implementing standard SQL's DOMAIN constraint Rich Shepard <rshepard@appl-ecosys.com>
2019-01-02 17:34 ` David G. Johnston <david.g.johnston@gmail.com>
2019-01-02 17:47 ` Rich Shepard <rshepard@appl-ecosys.com>
2019-01-02 17:52 ` David G. Johnston <david.g.johnston@gmail.com>
2019-01-02 17:58 ` Rich Shepard <rshepard@appl-ecosys.com>
2019-01-02 18:02 ` David G. Johnston <david.g.johnston@gmail.com>
2019-01-02 18:05 ` Re: Implementing standard SQL's DOMAIN constraint [RESOLVED] Rich Shepard <rshepard@appl-ecosys.com>
2019-01-02 18:13 ` Re: Implementing standard SQL's DOMAIN constraint [RESOLVED] Ron <ronljohnsonjr@gmail.com>
2019-01-02 18:28 ` Re: Implementing standard SQL's DOMAIN constraint [RESOLVED] 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