Oracle varchar2 最小长度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3354185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 21:08:25  来源:igfitidea点击:

Oracle varchar2 minimum length

oracle

提问by vrm

In Oracle db we have a table with varchar2 type of column (for example USERNAME). How can I set a exact (or at least minimum) length for this column? So that all usernames inserted into this table can be only 10 (or have to be at least 10) characters long.

在 Oracle db 中,我们有一个带有 varchar2 类型列的表(例如 USERNAME)。如何为此列设置精确(或至少是最小)长度?因此,插入该表的所有用户名只能是 10 个(或必须至少为 10 个)字符长。

回答by skaffman

You could use a check constraint:

您可以使用检查约束:

CREATE TABLE mytable (
  mycolumn varchar2(50),
  constraint strlen check (length(mycolumn) > 2)
)

Or something similar. I'm not sure how performant this is, though.

或者类似的东西。不过,我不确定它的性能如何。

回答by William Robertson

Or just for fun,

或者只是为了好玩,

CREATE TABLE testit
( mycolumn VARCHAR2(20) CONSTRAINT min_length_chk CHECK (mycolumn LIKE '__%') );

It's less explicit than the LENGTH() approach though so I'm not sure I'm recommending it except as an idea for related issues.

虽然它不如 LENGTH() 方法明确,所以我不确定我是否推荐它,除非作为相关问题的想法。