Postgresql - 如何禁止在某些字符串字段中使用空格

ken*_*der 2 database postgresql plperl

我想禁止在某些text/varchar字段中使用空格.

更重要的是,最好只有一组允许在那里使用的字符,例如:

[a-zA-Z0-9_\-]
Run Code Online (Sandbox Code Playgroud)

我想把它作为一个规则,作为其表VARCHAR中成员的所有字段primary key.

这应该在数据库级别完成,并且在尝试插入错误记录时可能会抛出异常,或者在将键字段更改为无效值时更新一个错误.

这可以在数据库级别内完成吗?我应该使用Pl/Perl它,还是有更简单的方法?

小智 6

您甚至不需要存储过程:

alter table xxx add constraint check_valid_chars check ( your_column ~ '^[a-zA-Z0-9_\-]+$' );
Run Code Online (Sandbox Code Playgroud)

应该管用.

  • 我只是用`your_column!〜''`。 (2认同)