1

I'm making a database with PostgreSQL. In one of the attributes in a tables it should be possible to insert numbers: "+" and "-", but no other chars like "A", "B" or "!".

Is it possible to check the input when I'm using the INSERT INTO function? I don't know because I'm just a beginner in Postgre and didn't find a solution in the internet.

Thanks if anybody knows an answer!

1

1 Answer 1

1

Instead of a trigger, you could use a CHECK constraint on the column's value. (or even make it a domain or type)

CREATE TABLE meuk
        ( id serial NOT NULL PRIMARY KEY
        , thefield varchar CHECK (thefield SIMILAR TO e'[0-9\+\-]+' )
        );

INSERT INTO meuk(thefield) VALUES ('1234');
INSERT INTO meuk(thefield) VALUES ('+1234');
INSERT INTO meuk(thefield) VALUES ('-1234');
INSERT INTO meuk(thefield) VALUES ('-1234a'); -- this one should fail

SELECT * FROM meuk;
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. That helps a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.