Re: How to restrict

From: Ron <ronljohnsonjr(at)gmail(dot)com>
To: pgsql-admin(at)lists(dot)postgresql(dot)org
Subject: Re: How to restrict
Date: 2019-02-26 15:24:22
Message-ID: 27032520-e411-6506-a0ca-eaf258524ffe@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

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.

In response to

Responses

Browse pgsql-admin by date

  From Date Subject
Next Message Pepe TD Vo 2019-02-26 15:36:08 Re: [MASSMAIL]Re: pg_dumpall: could not connect to database: FATAL
Previous Message gilberto.castillo 2019-02-26 15:19:53 Re: [MASSMAIL]Re: pg_dumpall: could not connect to database: FATAL