When creating a table, can you put a constraint check on a column to check that the string value has to be greater than 25 characters? I know it can be done on numeric, but having difficulty to do it for a string.
Here is my code:
CREATE TABLE TITLE (Title_ID VARCHAR(8), Title_Name VARCHAR(MAX) CHECK (Title_Name > 25));
I realize this only checks to see if the numeric value is greater than 25, but how can make it that it checks that the string value is greater than 25 characters
Advertisement
Answer
In Oracle, the maximum length of a string in a table is 4000 8-bit characters (as explained here). And you generally use varchar2()
for variable length strings. So you can do:
CREATE TABLE TITLE ( Title_ID VARCHAR2(8), Title_Name VARCHAR2(4000) CHECK (LENGTH(Title_Name) > 25) );
If you want an unlimited length string, you can use a CLOB
, but those are generally a bit more finicky to work with.
If you happen to be using SQL Server (which your syntax suggests), then this would be:
CREATE TABLE TITLE ( Title_ID VARCHAR(8), Title_Name VARCHAR(MAX) CHECK (LEN(Title_Name) > 25) );