1

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.

2 Answers 2

11

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.

Sign up to request clarification or add additional context in comments.

2 Comments

i assume you meant to say.. replace the '2' with your minimum size.. yes?
@ShoeLace yes you have to
-1

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.

Comments

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.