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?
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.
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).
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.
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With