Typ CHAR(1) boolean definieren 't', 'f'

Hallo,

statt des Types BOOLEAN möchte ich gerne einen Typen BOOLEAN definieren, der im Falle von true ein Char(1) mit t füllt und bei false ein f.

CREATE TYPE BOOLEAN AS (
newType VARCHAR(1) DEFAULT (‘f’) NOT NULL CHECK (newType IN (‘t’, ‘f’)),
);

So geht es nicht. Ich möchte nicht jedesmal so umständlich den typ als CHAR(1) defineren. Geht das?

Vielen Dank für Tipps,


Patrick

Das brauchst Du nicht. Das, was Du im SELECT zu sehen bekommst, kannst Du manipulieren.

test=# create table wahr (t bool);
CREATE TABLE
test=*# insert into wahr values (0::bool);
INSERT 0 1
test=*# insert into wahr values (1::bool);
INSERT 0 1
test=*# select * from wahr;
 t
---
 f
 t
(2 rows)

test=*# select case when t = 't'::bool then 1 else 0 end as t from wahr;
 t
---
 0
 1
(2 rows)

test=*# select case when t = 't'::bool then 'A' else 'X' end as t from wahr;
 t
---
 X
 A
(2 rows)

Andreas

Hallo,

danke für den Tipp. Kann man trotzdem den Typ boolean überschreiben, sodass immer nur t oder f verwendet wird und NOT NULL?

Du willst ihn kastrieren? Okay:

test=# create table wahr (t char(1) not null check (t ~ '[tf]'));
CREATE TABLE
test=*# insert into wahr values ('t');
INSERT 0 1
test=*# insert into wahr values ('0'::bool);
ERROR:  column "t" is of type character but expression is of type boolean
HINT:  You will need to rewrite or cast the expression.

Alternativ erzeugst Du eine neue Domain:

create domain wahr as  char(1) not null check  (value ~ '[tf]');

Andreas, keinen echten Sinn sehend…