Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minimum length constraint on a column

I'm trying to implement a minimum length constraint in Oracle.

As I read in this answer and multiple other similar questions I tried:

ALTER TABLE my_table 
ADD CONSTRAINT MY_TABLE_PASSWORD_CK CHECK (DATALENGTH(password) >=4)

And I am getting "DATALENGTH": invalid identifier". I also tried:

( DATALENGTH([password]) >=4 )
( LEN([password]) >=4 )
( LEN(password) >=4 )

What is the current format for this check constraint in Oracle?

like image 638
Alexandru Severin Avatar asked Aug 22 '15 13:08

Alexandru Severin


People also ask

How do I find the minimum length of a column in SQL?

SQL Server databases use LEN or DATALENGTH to find field width. It also has its own function to find the maximum length of a column – COL_LENGTH. SELECT MIN(LENGTH(<column_name>)) AS MinColumnLength FROM Table; If we include any non-aggregate functions into our query then we need a GROUP BY clause.

What is the limit of check constraints on a column?

Microsoft recommends no more than 253 foreign key constraints per column, though you are only limited by the number of objects in a database (2,147,483,647).

How to restrict values in a column in sql?

The CHECK constraint is used to limit the value range that can be placed in a column. If you define a CHECK constraint on a column it will allow only certain values for this column. If you define a CHECK constraint on a table it can limit the values in certain columns based on values in other columns in the row.

How to check constraint on table?

The syntax for enabling a check constraint in SQL Server (Transact-SQL) is: ALTER TABLE table_name WITH CHECK CHECK CONSTRAINT constraint_name; table_name. The name of the table that you wish to enable the check constraint.


1 Answers

DATALENGTH() returns the length in bytes in SQL Server. The equivalent Oracle function is LENGTHB() (documented here):

ALTER TABLE my_table
    ADD CONSTRAINT MY_TABLE_PASSWORD_CK CHECK (LENGTHB(password) >= 4)

However, for your purposes, I think the string length would be appropriate in both databases, LENGTH() in Oracle (or LEN() in SQL Server).

like image 75
Gordon Linoff Avatar answered Oct 04 '22 20:10

Gordon Linoff