Обсуждение: How to restrict

Поиск
Список
Период
Сортировка

How to restrict

От
"Campbell, Lance"
Дата:

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? 

 

Thanks,

 

LANCE CAMPBELL

Software Architect

 

Web Services

Public Affairs

Contact the Webtools Team

217.333.0382

lance@illinois.edu

 

 

/var/folders/wp/1f6l7hw95y718z976kgnl5f9kr5rtc/T/com.microsoft.Outlook/WebArchiveCopyPasteTempFiles/signature_logo.png

 

Under the Illinois Freedom of Information Act any written communication to or from university employees regarding university business is a public record and may be subject to public disclosure.

 

Вложения

Re: How to restrict

От
Achilleas Mantzios
Дата:
On 26/2/19 5:11 μ.μ., 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? 

Easy with a CHECK CONSTRAINT, like :
create table baba (type varchar(10) CHECK (type in ('X','Y','Z')));

 

Thanks,

 

LANCE CAMPBELL

Software Architect

 

Web Services

Public Affairs

Contact the Webtools Team

217.333.0382

lance@illinois.edu

 

 

/var/folders/wp/1f6l7hw95y718z976kgnl5f9kr5rtc/T/com.microsoft.Outlook/WebArchiveCopyPasteTempFiles/signature_logo.png

 

Under the Illinois Freedom of Information Act any written communication to or from university employees regarding university business is a public record and may be subject to public disclosure.

 



-- 
Achilleas Mantzios
IT DEV Lead
IT DEPT
Dynacom Tankers Mgmt
Вложения

Re: How to restrict

От
Ron
Дата:
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.

Re: How to restrict

От
Shreeyansh Dba
Дата:
Yes, @Ron reply makes sense you can use a CHECK constraint or FOREIGN KEY.

Thanks & Regards,
Shreeyansh DBA Team
www.shreeyansh.com


On Tue, Feb 26, 2019 at 8:54 PM Ron <ronljohnsonjr@gmail.com> wrote:
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.