Обсуждение: How to restrict
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,
Software Architect
217.333.0382
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.
Вложения
Easy with a CHECK CONSTRAINT, like :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?
create table baba (type varchar(10) CHECK (type in ('X','Y','Z')));
Thanks,
Software Architect
217.333.0382
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
Вложения
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.
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.