A different appoach: create a DOMAIN and use that as a data type for qc_isotope.
This will come in handy if the data type uccurs in more than one place: the constraint won't have to be repeated.
CREATE DOMAIN QC_ISO VARCHAR(2) CHECK (value IN ('TC', 'TL' ))
;
CREATE TABLE sensitivity
( id SERIAL NOT NULL PRIMARY KEY
);
ALTER TABLE sensitivity
ADD COLUMN qc_isotope QC_ISO NOT NULL DEFAULT 'TC'
;
INSERT INTO sensitivity(qc_isotope) VALUES ('AA') ;
INSERT INTO sensitivity(qc_isotope) VALUES ('TC') ;
SELECT * FROM sensitivity;
Result:
CREATE DOMAIN
NOTICE: CREATE TABLE will create implicit sequence "sensitivity_id_seq" for serial column "sensitivity.id"
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "sensitivity_pkey" for table "sensitivity"
CREATE TABLE
ALTER TABLE
ERROR: value for domain qc_iso violates check constraint "qc_iso_check"
INSERT 0 1
id | qc_isotope
----+------------
2 | TC
(1 row)
UPDATE: it does appear that DOMAINs can be changed once they are used (this works on PG-9.1):
ALTER DOMAIN QC_ISO
DROP CONSTRAINT QC_ISO_check -- I don't think the name is important
;
ALTER DOMAIN QC_ISO
ADD CONSTRAINT QC_ISO_check CHECK (value IN ('TC', 'TL', 'AA' ))
;
INSERT INTO sensitivity(qc_isotope) VALUES ('AA') ;
INSERT INTO sensitivity(qc_isotope) VALUES ('BB') ;
SELECT * FROM sensitivity;
New result:
ALTER DOMAIN
ALTER DOMAIN
ERROR: value for domain qc_iso violates check constraint "qc_iso_check"
INSERT 0 1
id | qc_isotope
----+------------
2 | TC
4 | AA
(2 rows)