4 ms·
Just define a column constraint (syntax might be wrong): CREATE TABLE t (col TEXT CHECK length(col) < 50); What is nice is that you just need to add/drop a co
by rpedela 13y ago
Just define a column constraint (syntax might be wrong):
CREATE TABLE t (col TEXT CHECK length(col) < 50);
What is nice is that you just need to add/drop a constraint if you want to change the max value check rather than changing the data type which can result in a table rewrite depending on the scenario. You can also add a min length check as well using this method or regex check, etc.
I think the author should have made this point rather than just glossing over it with "constraints, triggers are more flexible".
- Jonanin 13y agoThanks for the suggestion, I edited my post to provide an example of using domains with TEXT fields.
- srisa 13y agoDoesn't the use of check constraints add an overhead and possibly negate the slight performance advantage that was gained by using text instead of varchar/char?
- ddebernardy 13y agoYeah. Technically, `foo varchar(n)` amounts to syntactic sugar for `foo varchar check(length(foo) <= n)`. What the benchmarks show, if anything, is that while the `check` involves negligible overhead per row, the overhead can eventually be enough to make a measurable difference. Not to mention, the Postgres actually manual says as much as the above, in its description of text types.
- epo 13y agoThe check constraint should only be applied when the column is updated so depends on your usage.