pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedAlter table column constraint
10+ messages / 6 participants
[nested] [flat]
* Alter table column constraint
@ 2018-12-17 19:58 Rich Shepard <rshepard@appl-ecosys.com>
0 siblings, 1 reply; 10+ messages in thread
From: Rich Shepard @ 2018-12-17 19:58 UTC (permalink / raw)
To: pgsql-general
I want to alter a term in a column's constraint to allow only specified
strings as attributes and have not found how to do this in the docs (using
version 10 docs now). There is an alter table command that allows renaming a
constraint but I've not seen how to modify the constraint itself.
Pointer to a reference needed.
TIA,
Rich
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: Alter table column constraint
@ 2018-12-17 20:01 Rich Shepard <rshepard@appl-ecosys.com>
parent: Rich Shepard <rshepard@appl-ecosys.com>
0 siblings, 1 reply; 10+ messages in thread
From: Rich Shepard @ 2018-12-17 20:01 UTC (permalink / raw)
To: pgsql-general
On Mon, 17 Dec 2018, Rich Shepard wrote:
> I want to alter a term in a column's constraint to allow only specified
> strings as attributes and have not found how to do this in the docs (using
> version 10 docs now). There is an alter table command that allows renaming
> a constraint but I've not seen how to modify the constraint itself.
Is the procedure to drop the current check constraint then add the revised
one?
Rich
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: Alter table column constraint
@ 2018-12-17 20:06 Joshua D. Drake <jd@commandprompt.com>
parent: Rich Shepard <rshepard@appl-ecosys.com>
0 siblings, 1 reply; 10+ messages in thread
From: Joshua D. Drake @ 2018-12-17 20:06 UTC (permalink / raw)
To: Rich Shepard <rshepard@appl-ecosys.com>; pgsql-general
On 12/17/18 12:01 PM, Rich Shepard wrote:
> On Mon, 17 Dec 2018, Rich Shepard wrote:
>
>> I want to alter a term in a column's constraint to allow only specified
>> strings as attributes and have not found how to do this in the docs
>> (using
>> version 10 docs now). There is an alter table command that allows
>> renaming
>> a constraint but I've not seen how to modify the constraint itself.
>
> Is the procedure to drop the current check constraint then add the
> revised
> one?
Or the other way around but yes.
JD
>
> Rich
>
--
Command Prompt, Inc. || http://the.postgres.company/ || @cmdpromptinc
*** A fault and talent of mine is to tell it exactly how it is. ***
PostgreSQL centered full stack support, consulting and development.
Advocate: @amplifypostgres || Learn: https://postgresconf.org
***** Unless otherwise stated, opinions are my own. *****
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: Alter table column constraint
@ 2018-12-17 20:13 Melvin Davidson <melvin6925@gmail.com>
parent: Joshua D. Drake <jd@commandprompt.com>
0 siblings, 1 reply; 10+ messages in thread
From: Melvin Davidson @ 2018-12-17 20:13 UTC (permalink / raw)
To: Joshua D. Drake <jd@commandprompt.com>; +Cc: Rich Shepard <rshepard@appl-ecosys.com>; pgsql-general
>I want to alter a term in a column's constraint to allow only specified
> strings as attributes
Yes, you must drop then add the revised constraint. However, from your
statement above, it sounds to
me as if you would be better off using A FOREIGN kEY CONSTRAINT. It makes
things a lot simpler.
On Mon, Dec 17, 2018 at 3:07 PM Joshua D. Drake <jd@commandprompt.com>
wrote:
> On 12/17/18 12:01 PM, Rich Shepard wrote:
> > On Mon, 17 Dec 2018, Rich Shepard wrote:
> >
> >> I want to alter a term in a column's constraint to allow only specified
> >> strings as attributes and have not found how to do this in the docs
> >> (using
> >> version 10 docs now). There is an alter table command that allows
> >> renaming
> >> a constraint but I've not seen how to modify the constraint itself.
> >
> > Is the procedure to drop the current check constraint then add the
> > revised
> > one?
>
> Or the other way around but yes.
>
> JD
>
>
> >
> > Rich
> >
>
> --
> Command Prompt, Inc. || http://the.postgres.company/ || @cmdpromptinc
> *** A fault and talent of mine is to tell it exactly how it is. ***
> PostgreSQL centered full stack support, consulting and development.
> Advocate: @amplifypostgres || Learn: https://postgresconf.org
> ***** Unless otherwise stated, opinions are my own. *****
>
>
>
--
*Melvin Davidson*
*Maj. Database & Exploration Specialist*
*Universe Exploration Command – UXC*
Employment by invitation only!
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: Alter table column constraint
@ 2018-12-17 20:20 Rich Shepard <rshepard@appl-ecosys.com>
parent: Melvin Davidson <melvin6925@gmail.com>
0 siblings, 4 replies; 10+ messages in thread
From: Rich Shepard @ 2018-12-17 20:20 UTC (permalink / raw)
To: pgsql-general
On Mon, 17 Dec 2018, Melvin Davidson wrote:
> Yes, you must drop then add the revised constraint. However, from your
> statement above, it sounds to me as if you would be better off using A
> FOREIGN kEY CONSTRAINT. It makes things a lot simpler.
Melvin,
I don't follow. Here's the DDL for that column:
industry varchar(24) NOT NULL
CONSTRAINT invalid_industry
CHECK (industry in ('Agriculture', 'Business, other', 'Chemicals',
'Energy', 'Law', 'Manufacturing', 'Mining', 'Municipalities',
'Ports/Marine Services', 'Transportation')),
and I want to remove Municipalities for the more general Government.
Regards,
Rich
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: Alter table column constraint
@ 2018-12-17 20:32 Ron <ronljohnsonjr@gmail.com>
parent: Rich Shepard <rshepard@appl-ecosys.com>
3 siblings, 0 replies; 10+ messages in thread
From: Ron @ 2018-12-17 20:32 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
On 12/17/2018 02:20 PM, Rich Shepard wrote:
> On Mon, 17 Dec 2018, Melvin Davidson wrote:
>
>> Yes, you must drop then add the revised constraint. However, from your
>> statement above, it sounds to me as if you would be better off using A
>> FOREIGN kEY CONSTRAINT. It makes things a lot simpler.
>
> Melvin,
>
> I don't follow. Here's the DDL for that column:
>
> industry varchar(24) NOT NULL
> CONSTRAINT invalid_industry
> CHECK (industry in ('Agriculture', 'Business, other', 'Chemicals',
> 'Energy', 'Law', 'Manufacturing', 'Mining', 'Municipalities',
> 'Ports/Marine Services', 'Transportation')),
>
> and I want to remove Municipalities for the more general Government.
Melvin is saying to:
1. create a table named valid_industry,
2. populate it with the valid industries,
3. create an FK constraint on your main table's industry column to
valid_industry.industry, and then
4. drop the constraint invalid_industry.
--
Angular momentum makes the world go 'round.
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: Alter table column constraint
@ 2018-12-17 20:32 David G. Johnston <david.g.johnston@gmail.com>
parent: Rich Shepard <rshepard@appl-ecosys.com>
3 siblings, 1 reply; 10+ messages in thread
From: David G. Johnston @ 2018-12-17 20:32 UTC (permalink / raw)
To: Rich Shepard <rshepard@appl-ecosys.com>; +Cc: pgsql-general
On Mon, Dec 17, 2018 at 1:20 PM Rich Shepard <rshepard@appl-ecosys.com> wrote:
>
> On Mon, 17 Dec 2018, Melvin Davidson wrote:
>
> > Yes, you must drop then add the revised constraint. However, from your
> > statement above, it sounds to me as if you would be better off using A
> > FOREIGN kEY CONSTRAINT. It makes things a lot simpler.
>
> Melvin,
>
> I don't follow. Here's the DDL for that column:
>
> industry varchar(24) NOT NULL
> CONSTRAINT invalid_industry
> CHECK (industry in ('Agriculture', 'Business, other', 'Chemicals',
> 'Energy', 'Law', 'Manufacturing', 'Mining', 'Municipalities',
> 'Ports/Marine Services', 'Transportation')),
>
> and I want to remove Municipalities for the more general Government.
--not tested
CREATE TABLE industry (
industry_name text PRIMARY KEY
);
CREATE TABLE company (
company_id serial PRIMARY KEY,
industry_name text REFERENCES industry (industry_name)
ON UPDATE CASCADE
ON DELETE RESTRICT
);
UPDATE industries SET industry_name = 'Government' WHERE industry_name
= 'Municipalities';
-- All records in company have changed now too thanks to the ON UPDATE CASCADE
To avoid the effective table rewrite use surrogate keys and turn the
text into a simple label. It should still have a UNIQUE index on it
though as it is your real key.
David J.
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: Alter table column constraint
@ 2018-12-17 20:33 Melvin Davidson <melvin6925@gmail.com>
parent: Rich Shepard <rshepard@appl-ecosys.com>
3 siblings, 0 replies; 10+ messages in thread
From: Melvin Davidson @ 2018-12-17 20:33 UTC (permalink / raw)
To: Rich Shepard <rshepard@appl-ecosys.com>; +Cc: pgsql-general
So CREATE a table eg:
CREATE TABLE fks_for_tables
( fks_id serial
fks_values varchar(20),
CONSTRAINT fks_pkey PRIMARY KEY (fks_id),
CONSTRAINT fks-unique UNIQUE fks_values
)
Then
INSERT INTO fks_for_tables
(fks_values)
VALUES
( 'Agriculture'),
('Business'),
('other))',
'Chemicals')
...
...
('Transportation');
Then you can
ALTER TABLE your_table
ADD CONSTRAINT FOREIGN KEY (industry)
REFERENCES fks_for_tables(fks_valies);
On Mon, Dec 17, 2018 at 3:20 PM Rich Shepard <rshepard@appl-ecosys.com>
wrote:
> On Mon, 17 Dec 2018, Melvin Davidson wrote:
>
> > Yes, you must drop then add the revised constraint. However, from your
> > statement above, it sounds to me as if you would be better off using A
> > FOREIGN kEY CONSTRAINT. It makes things a lot simpler.
>
> Melvin,
>
> I don't follow. Here's the DDL for that column:
>
> industry varchar(24) NOT NULL
> CONSTRAINT invalid_industry
> CHECK (industry in ('Agriculture', 'Business, other', 'Chemicals',
> 'Energy', 'Law', 'Manufacturing', 'Mining', 'Municipalities',
> 'Ports/Marine Services', 'Transportation')),
>
> and I want to remove Municipalities for the more general Government.
>
> Regards,
>
> Rich
>
>
--
*Melvin Davidson*
*Maj. Database & Exploration Specialist*
*Universe Exploration Command – UXC*
Employment by invitation only!
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: Alter table column constraint
@ 2018-12-17 20:35 Melvin Davidson <melvin6925@gmail.com>
parent: David G. Johnston <david.g.johnston@gmail.com>
0 siblings, 0 replies; 10+ messages in thread
From: Melvin Davidson @ 2018-12-17 20:35 UTC (permalink / raw)
To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: Rich Shepard <rshepard@appl-ecosys.com>; pgsql-general
See https://www.postgresql.org/docs/current/tutorial-fk.html
On Mon, Dec 17, 2018 at 3:32 PM David G. Johnston <
david.g.johnston@gmail.com> wrote:
> On Mon, Dec 17, 2018 at 1:20 PM Rich Shepard <rshepard@appl-ecosys.com>
> wrote:
> >
> > On Mon, 17 Dec 2018, Melvin Davidson wrote:
> >
> > > Yes, you must drop then add the revised constraint. However, from your
> > > statement above, it sounds to me as if you would be better off using A
> > > FOREIGN kEY CONSTRAINT. It makes things a lot simpler.
> >
> > Melvin,
> >
> > I don't follow. Here's the DDL for that column:
> >
> > industry varchar(24) NOT NULL
> > CONSTRAINT invalid_industry
> > CHECK (industry in ('Agriculture', 'Business, other', 'Chemicals',
> > 'Energy', 'Law', 'Manufacturing', 'Mining', 'Municipalities',
> > 'Ports/Marine Services', 'Transportation')),
> >
> > and I want to remove Municipalities for the more general Government.
>
> --not tested
>
> CREATE TABLE industry (
> industry_name text PRIMARY KEY
> );
>
> CREATE TABLE company (
> company_id serial PRIMARY KEY,
> industry_name text REFERENCES industry (industry_name)
> ON UPDATE CASCADE
> ON DELETE RESTRICT
> );
>
> UPDATE industries SET industry_name = 'Government' WHERE industry_name
> = 'Municipalities';
> -- All records in company have changed now too thanks to the ON UPDATE
> CASCADE
>
> To avoid the effective table rewrite use surrogate keys and turn the
> text into a simple label. It should still have a UNIQUE index on it
> though as it is your real key.
>
> David J.
>
>
--
*Melvin Davidson*
*Maj. Database & Exploration Specialist*
*Universe Exploration Command – UXC*
Employment by invitation only!
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: Alter table column constraint
@ 2018-12-17 20:36 Adrian Klaver <adrian.klaver@aklaver.com>
parent: Rich Shepard <rshepard@appl-ecosys.com>
3 siblings, 0 replies; 10+ messages in thread
From: Adrian Klaver @ 2018-12-17 20:36 UTC (permalink / raw)
To: Rich Shepard <rshepard@appl-ecosys.com>; pgsql-general
On 12/17/18 12:20 PM, Rich Shepard wrote:
> On Mon, 17 Dec 2018, Melvin Davidson wrote:
>
>> Yes, you must drop then add the revised constraint. However, from your
>> statement above, it sounds to me as if you would be better off using A
>> FOREIGN kEY CONSTRAINT. It makes things a lot simpler.
>
> Melvin,
>
> I don't follow. Here's the DDL for that column:
>
> industry varchar(24) NOT NULL
> CONSTRAINT invalid_industry
> CHECK (industry in ('Agriculture', 'Business, other', 'Chemicals',
> 'Energy', 'Law', 'Manufacturing', 'Mining', 'Municipalities',
> 'Ports/Marine Services', 'Transportation')),
>
> and I want to remove Municipalities for the more general Government.
What Melvin suggested was to:
1) Move this ('Agriculture', 'Business, other', 'Chemicals', ..)
into its own table say something like:
CREATE TABLE industry(industry_code varchar PRIMARY KEY, industry_desc
varchar)
2) Change the industry field in your existing table to:
industry varchar(24) NOT NULL REFERENCES industry(industry_code) ON
UPDATE CASCADE.
Where this helps is that in the situation you describe in your original
post you just change 'Municipalities' to 'Government' in the industry
table and the referring table automatically gets the change via the ON
UPDATE CASCADE.
>
> Regards,
>
> Rich
>
--
Adrian Klaver
adrian.klaver@aklaver.com
^ permalink raw reply [nested|flat] 10+ messages in thread
end of thread, other threads:[~2018-12-17 20:36 UTC | newest]
Thread overview: 10+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-12-17 19:58 Alter table column constraint Rich Shepard <rshepard@appl-ecosys.com>
2018-12-17 20:01 ` Rich Shepard <rshepard@appl-ecosys.com>
2018-12-17 20:06 ` Joshua D. Drake <jd@commandprompt.com>
2018-12-17 20:13 ` Melvin Davidson <melvin6925@gmail.com>
2018-12-17 20:20 ` Rich Shepard <rshepard@appl-ecosys.com>
2018-12-17 20:32 ` Ron <ronljohnsonjr@gmail.com>
2018-12-17 20:32 ` David G. Johnston <david.g.johnston@gmail.com>
2018-12-17 20:35 ` Melvin Davidson <melvin6925@gmail.com>
2018-12-17 20:33 ` Melvin Davidson <melvin6925@gmail.com>
2018-12-17 20:36 ` Adrian Klaver <adrian.klaver@aklaver.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