For the 9-digit restriction, a domain over int
can work:
CREATE DOMAIN ein AS int CHECK (VALUE>0 AND VALUE<1000000000);
Then ein
can be used in declarations as a type, for instance:
=> CREATE TABLE test(id ein, t text);
CREATE TABLE
=> insert into test values(2*1e9::int);
ERROR: value for domain ein violates check constraint "ein_check"
=> insert into test values(100);
INSERT 0 1
The zerofill
bit is different, it's about presentation, not storage,
and that part cannot be specialized for a domain.
You may instead apply to_char
to the values, for example:
=> select to_char(id,'000000000') from test;
to_char
------------
000000100
and possibly access this through a stored view or a presentation
function that takes only the ein
as argument
if you prefer to abstract this from the client.
To go further, you could create a full type with CREATE TYPE
backed with C
code for the INPUT
and OUTPUT
function, and these functions could implement the 9-digit left-padded format as the input/output format, so that the user may never see anything else at the SQL level.