Oracle SQL Developer regex expression error - sql

In the script file, I inserted the following code.
drop table Test;
create table Test(
name char(2) unique not null,
constraint name_c check(
regexp_like(name, '^[A-Z]{1,2}$', 'c')
)
);
insert into Test values ('B');
The developer never budge. It keeps on saying violating the name_c constraint and I don't understand why. The regular expression looks fine for me.
Some variants, however, succeeded, for example, dropping the dollar sign
drop table Test;
create table Test(
name char(2) unique not null,
constraint name_c check(
regexp_like(name, '^[A-Z]{1,2}', 'c')
)
);
insert into Test values ('B');
And I don't understand either.
Why?
Edit: This is the problem: logically the regex is right. But somehow Oracle SQL Developer doesn't budge.

if you went it to start with 1 or 2 capital letter and doesn't matter what coming next you should do it like this :
^[A-Z]{1,2}.*
.* mean 1 or more character exept \n (nex line)

Related

Check constraint not reading [A-Z]?

I am having an issue with a check constraint reading [A-Z].
here is the code:
drop table test CASCADE CONSTRAINTS;
Create table test (
post Varchar2(6),
CHECK (post like '[A-Z]')
);
insert into test values ('A');
And I receive a "check constraint violated" error after trying to insert A. Any feedback would be appreciated. Even if it does work for you because everything is telling me it should work.
Use REGEXP_LIKE:
CREATE TABLE test (
post Varchar2(6),
CHECK (REGEXP_LIKE(post, '^[A-Z]+$'))
);
The pattern ^[A-Z]+$ would match posts which only contain capital letters. If instead you want to flag posts which begin with a capital letter, then use ^[A-Z].

How to name NOT NULL constraint in CREATE TABLE query

For a test tomorrow we're told to name our constraints
I know it's possible to create a constraint when you use ALTER TABLE
but can you add a name to a not null constraint when you CREATE TABLE?
f.e.
CREATE TABLE test (
test1 VARCHAR
CONSTRAINT nn_test1 NOT NULL (test1)
)
I get an error when trying to run this query. Am I writing it wrong?
The error I get is
ERROR: syntax error at or near "NOT"
LINE 3: CONSTRAINT nn_test1 NOT NULL (test1))
^
SQL state: 42601
Character: 56
You have two options to define a named not null constraint:
Inline with the column:
CREATE TABLE test
(
test1 VARCHAR CONSTRAINT nn_test1 NOT NULL,
test2 integer --<< no comma because it's the last column
);
Or at the end of columns as an out-of-line constraint. But then you need a check constraint:
CREATE TABLE test
(
test1 VARCHAR,
test2 integer, --<< comma required after the last column
constraint nn_test1 check (test1 is not null)
);
This has become irrelevant, since you're not using SQL Server
First of all, you should always specify a length for a VARCHAR. Not doing so (in SQL Server variables, or parameters) may result in a string of just exactly ONE character in length - typically NOT what you want.
Then, you need to just specify the NOT NULL - there's no need to repeat the column name (actually this is the error) - if you're specifying the CONSTRAINT "inline" with the column definition (which is a perfectly legal and in my opinion the preferred way of doing this).
Try this code:
CREATE TABLE test
(
test1 VARCHAR(50)
CONSTRAINT nn_test1 NOT NULL
)
At least this is the CREATE TABLE statement that works in T-SQL / SQL Server - not sure about PostgreSQL (don't know it well enough, don't have it at hand to test right now).
I, a_horse_with_no_name, the two syntax:
constraint nn_test1 check (test1 is not null)
and
test1 VARCHAR CONSTRAINT nn_test1 NOT NULL
are equivalent ? performance correctly ecc.
Because in first case the SQL server exception return the name nn_test so the system know exactly error.

PostgreSQL CHECK Constraint with 'LIKE' fails but succeeds with 'SIMILAR TO' and/or POSIX '!~*'

I am using PostgreSQL 10.1.
I create the following table:
CREATE TABLE country
(
id smallint NOT NULL,
alpha2 character varying(2) NOT NULL,
alpha3 character varying(3) NOT NULL,
name character varying(38) NOT NULL,
CONSTRAINT country_pkey PRIMARY KEY (id),
CONSTRAINT country_alpha2_key UNIQUE (alpha2),
CONSTRAINT country_alpha3_key UNIQUE (alpha3),
CONSTRAINT country_name_key UNIQUE (name),
CONSTRAINT country_alpha2_check
CHECK ((char_length(alpha2::text)) = 2 AND
(alpha2 NOT LIKE '%[^a-zA-Z]%')),
CONSTRAINT country_alpha3_check
CHECK ((char_length(alpha3::text)) = 3 AND
(alpha3 NOT LIKE '%[^a-zA-Z]%')),
CONSTRAINT country_name_check CHECK (char_length(name::text) > 0)
);
Unfortunately, the following statement succeeds although it should not:
INSERT INTO country (id, alpha2, alpha3, name)
VALUES (1, '11', '111', 'Haiti');
If I substitute LIKE with SIMILAR TO then the above statement fails as it should.
If I substitute NOT LIKE '%[^a-zA-Z]%' with POSIX Regex !~* '[^a-zA-Z]' then the above statement does fail, too, as it should.
Is there any explanation why LIKE fails? Most of the examples I have seen use LIKE! It seems that LIKE doesn't like to work!
Tia
The explanation is obvious and is hidden in a mere technicality:
LIKE in PostgreSQL uses only two characters to form a pattern: underscore _ and percent sign %.

Why does the zipcode fail the check constraint?

I feel like I am probably missing something really simple, but I really can't figure out what I'm doing wrong. I'm trying to use a check constraint to make sure zipcodes are 5 digit numbers, but the check restraint keeps failing. Here is the table creating with the constraint:
Create Table Students (
StudentID Int Primary Key Identity(1,1)
StudentNumber nVarchar(100) Unique Not Null,
...
StudentZipCode nChar(10) Not Null
)
Go
Alter Table Students Add Constraint chZipCode
CHECK (StudentZipCode LIKE '[0-9][0-9][0-9][0-9][0-9]' OR StudentZipCode
Like '[0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]')
Go
Codes like 12345-6789 work, but when I try to insert the values like '12345' or '01234' it gives me this error:
The INSERT statement conflicted with the CHECK constraint "chZipCode". The conflict occurred in database ..., table "dbo.Students", column 'StudentZipCode'.
It fails because you defined the zip code as a char() instead of a varchar(). Hence, it has a bunch of spaces padding it out.
So, define it as:
Create Table Students (
StudentID Int Primary Key Identity(1,1),
StudentNumber nVarchar(100) Unique Not Null,
StudentZipCode nVarChar(10) Not Null,
CHECK (StudentZipCode LIKE '[0-9][0-9][0-9][0-9][0-9]' OR
StudentZipCode LIKE '[0-9][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]')
);
Then '12345' works, because it matches the first of the LIKE patterns.
'012344' does not work, because no pattern has six digits in a row.
Here is a SQL Fiddle.

Exclude certain characters using a sql constraint (with regexp)?

I'm trying to create an Oracle SQL*Plus table, but I'm having trouble with a constraint using regular expressions.
The constraint I'm trying to implement is that the field is 4 characters long and cannot contain the following characters: ,.!£$*<>"=
In reality I would be enough to just only allow alphanumeric characters, so I tried the following regular expression constraint:
CONSTRAINT CHK_Foo CHECK (Foo LIKE '[[:alnum:]]'),
However when I attempted to enter AAAAfield it said I had violated the constraint.
I'm a complete newcomer to Regexp, and a relative newcomer to SQL, so any explanations as to why it doesn't work as well as just an alternative would be immensely appreciated!
Two options:
CHECK (REGEXP_LIKE( foo, '[^,.!£$*<>"=]{4}' ) ) will accept all 4-character strings that do not contain ,.!£$*<>"=
CHECK (REGEXP_LIKE( bar, '[a-zA-Z0-9]{4}' ) ) will accept all 4-character strings that only contain a-zA-Z0-9
(If you want less than strings which are less than or equal to 4 characters in length then you can replace {4} with {0,4})
SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE TABLE test (
foo CHAR(4) NOT NULL,
bar CHAR(4) NOT NULL,
CONSTRAINT chk_foo CHECK (REGEXP_LIKE( foo, '[^,.!£$*<>"=]{4}' ) ),
CONSTRAINT chk_bar CHECK (REGEXP_LIKE( bar, '[a-zA-Z0-9]{4}' ) )
);
Query 1:
INSERT INTO test VALUES ( 'AAAA', '"AAA' );
Result 1:
ORA-02290: check constraint (USER_4_850C2.CHK_BAR) violated : INSERT INTO test VALUES ( 'AAAA', '"AAA' )
Query 2:
INSERT INTO test VALUES ( '$AAA', 'AAAA' );
Result 2:
ORA-02290: check constraint (USER_4_850C2.CHK_FOO) violated : INSERT INTO test VALUES ( '$AAA', 'AAAA' )
Query 3:
INSERT INTO test VALUES ( 'AAAA', 'AAAA' )
Result 3:
CONSTRAINT CHK_Foo CHECK (regexp_LIKE(Foo, '^\w{4}$'))
You should construct a correct Oracle regexp and then use REGEXP_LIKE and not just LIKE.
See here:
http://docs.oracle.com/cd/B19306_01/appdev.102/b14251/adfns_regexp.htm
http://docs.oracle.com/cd/B19306_01/appdev.102/b14251/adfns_regexp.htm#CHDIDJJC