How to use CHECK constraint to restrict non-numeric varchar2 (only alphabets)? - sql

I have a column NAME
It must contain only characters and not numbers
How do I use CHECK condition:
CHECK(NAME NOT LIKE '%[0-9]%')
or any other method...
edit: Oracle database is used.

You didn't state your DBMS so I'm assuming PostgreSQL
CHECK(name ~ '^[^0-9]*$')

Double negative Should be standard (not MySQL though) because it uses LIKE:
CHECK(NAME NOT LIKE '%[^a-zA-Z]%')

Related

When creating a table in Oracle SQL, how can I create a column that will contain specific values?

I am trying to create a table and one of the columns will contain file names like 2009-00014 or 2013-55045.
Should it be 'number' or 'varchar'?
The hyphen requires that the column be a string. In Oracle, the recommended type would be varchar2(), with a specified length.
In addition, the names of files might one day include other characters -- who knows how an application will change in the future.
I think, it should be varchar2.
in my opinion, you can use 'varchar2' but If you're going to do mathematical operations on that column you should 'number'

INITCAP in MariaDB

I am currently trying to check that every values that are inserted into my MariaDB table the 1st letter of every word in uppercase, so I tried to use CONSTRAINT nombreok CHECK (INITCAP(nombre_director) = nombre_director)); but it doesn't works because it gives me an error saying that function INITCAP() cannot be used in a CHECK... what can I do to solve it?
I think initcap() is not deterministic because the results may depend on the default collation of the database (under some circumstances).
You can ensure that the first characters are capitalized using:
CONSTRAINT nombreok CHECK (not (nombre_director collate latin1_general_cs) REGEXP '(^|[[:space:]])[[:lower:]]')
Here is a db<>fiddle.
If you want to check the lower case as well:
CONSTRAINT nombreok CHECK ((nombre_director collate latin1_general_cs) REGEXP '^([[:upper:]][[:lower:]]+([[:space:]]+|$))+$')
However, you might want to extend this beyond just alphabetical characters.
Here is a db<>fiddle for this.

Constraint with regexp

I am currently trying to create a regular expresion to use in my Oracle database constraints' that is able to check if all the words of a string starts with an uppercase. I already tried to use functions like initcap, but it really doesn't work as expected, because it gives an error when I try to insert values that contains a letter like "ñ" or an acent. Could you please help me?
I think the check constraint that you want is more like this:
check (not regexp_like(mycol, '(^|\W)[[:lower:]]'))
You could do this with a check constraint and a regex, like so:
alter table mytable
add constraint myconstraint
check (not regexp_like(mycol, '(^|\W)[a-z]'))
Regexp (^|\W)[A-Z] means: the beginning of the string or a non-word character followed by an lower case value. not regexp_like(...) forbids such pattern.
Demo on DB Fiddle
As commented by #kfinity, you can also use regex (^|\W)[[:lower:]], that should properly trap special upper characters.

how to restrict special character except space in a table in oracle

i have a requirement to insert data into a varchar2 column.can anyone please help me how to restrict special characters except space in a table field in oracle and also the field should not accept two consecutive space
You are looking for a check constraint, which seems to need regular expressions. Something like this:
alter table t add constraint chk_t_col
check (regexp_like(col, '^[a-zA-Z0-9 ]+$') and col not like '% %');
I'm not sure what the set of characters is, but the above is for alphanumeric and space.
Note: You can create the regular expression to preclude the double spaces. I find the above easier to follow.

HSQL Query constraint for alphabets and numbers

How I have a column Firstname which should be only Alphabets
and phone number which should be only numbers in HSQL
i wrote something like
ALTER TABLE USER
ADD CONSTRAINT CHECK_USER_FIRST_NAME CHECK (FIRST_NAME NOT LIKE '%[^A-Z ]%' )
But it allows Tp1 which i don't want.
Can someone help me with constraint in HSQL
LIKE doesn't support regular expressions. The only wildcards it supports are % for multiple characters and _ for a single character. To match against a regular expression you need regexp_matches()
ALTER TABLE user
ADD CONSTRAINT check_user_first_name
CHECK (regexp_matches(first_name, '[A-Z]+'))
This will allow only uppercase letters, so you probably want to use '[A-Za-z]+' instead. It also requires at least one character in the name. If you want to allow empty strings, change the + to *. This will still allow null values though.
Note that USER is a reserved keyword in SQL. You shouldn't create a table with that name. If you try this e.g. on Oracle or PostgreSQL it will fail. The name requires to be quoted - which is not a good idea. You should find a different name.