From: Ron <ronljohnsonjr@gmail.com>
To: pgsql-admin@lists.postgresql.org
Subject: Re: How to restrict
Date: Tue, 26 Feb 2019 09:24:22 -0600
Message-ID: <27032520-e411-6506-a0ca-eaf258524ffe@gmail.com> (raw)
In-Reply-To: <6C004315-7728-4909-BCCD-2DE8F5DA3920@illinois.edu>
References: <6C004315-7728-4909-BCCD-2DE8F5DA3920@illinois.edu>
On 2/26/19 9:11 AM, Campbell, Lance wrote:
>
> PostgreSQL 10.x
>
> What is the best way to restrict the values on a text field to make sure
> they only contain particular values: Example: The field “type” can only
> contain the values of “X”, “Y” and “Z”. Would a trigger be the best
> strategy? Or is there a special SQL type I should use in this particular
> case?
>
Use either a CHECK constraint or FOREIGN KEY.
For just a few items*, *understanding that you must drop and recreate the
constraint to modify it.
http://www.postgresqltutorial.com/postgresql-check-constraint/
CREATE TABLE foo
(
bar VARCHAR(20) CHECK (bar IN ('X', 'Y', 'Z'))
);
For more items, and simpler additions:
http://www.postgresqltutorial.com/postgresql-foreign-key/
--
Angular momentum makes the world go 'round.
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 using the --to and --cc options:
reply via email
To: pgsql-admin@postgresql.org
Cc: ronljohnsonjr@gmail.com, pgsql-admin@lists.postgresql.org
Subject: Re: How to restrict
In-Reply-To: <27032520-e411-6506-a0ca-eaf258524ffe@gmail.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