Adding an email check constraint in APEX - oracle

I'm fairly new to Oracle and very new to APEX. I'm trying to add a constraint on a table to validate the email:
REGEXP_LIKE(CALLER_EMAIL, '[a-zA-Z0-9._%-]+#[a-zA-Z0-9._%-]+\.[a-zA-Z]{2,4}')
Now if I'm right this would work fine inside a CONSTRAINT <name> CHECK(REGEXP_LIKE(...)) however I get this (confusing) error when I attempt to save it:
ORA-00920: invalid relational operator
I think it is because the generated query contains "CALLER_EMAIL":
alter table "CALL" add constraint
"CALL_EMAILFORMAT_CHK" check ( "CALLER_EMAIL" REGEXP_LIKE(CALLER_EMAIL, '[a-zA-Z0-9._%-]+#[a-zA-Z0-9._%-]+\.[a-zA-Z]{2,4}'))
Any ideas?

Try this:
alter table "CALL" add constraint
"CALL_EMAILFORMAT_CHK" check
( REGEXP_LIKE(CALLER_EMAIL, '[a-zA-Z0-9._%-]+#[a-zA-Z0-9._%-]+\.[a-zA-Z]{2,4}'));

Related

Adding NOT NULL Constraint with ALTER TABLE statement (Oracle)

this is my PL-SQL statement
ALTER TABLE regions MODIFY (region_name VARCHAR(40) DEFAULT 'Euro') CONSTRAINT region_nn NOT NULL;
The column 'region_name' has NULL values I want to replace with 'Euro'. I get an error with this, and I'm wondering if I have the syntax wrong or if it's impossible to place a default value when adding the NOT NULL constraint and I have to do it as two separate SQL statements
Thank you for your help'
adding a constrain does not modify any existing data, it only modifies the definition of your table. Fix your data first, then add the constraint - or add the constraint with the defererred keyword and then fix the data. Either way, you'll manually have to update the data.

Add enum type column to table

im trying to add a new column to my existing table 'Results', and it seems to be very easy but I cant see the mistake.
Here is my code:
SQL> Alter table results add column CAL ENUM('A','B');
ERROR at line 1: ORA-00904: : invalid identifier
What am I missing?
I've read this from w3 and this from java2s but cant see the difference to mine.
Thanks, and sorry for the dumb question.
OK, with an ORA- error I am assuming that this is an oracle database, and not mysql. you have both tags and you are linking to MySQL example, but the error is not a MySQL error.
Assuming that this IS an Oracle DB, then there is no native ENUM data type. You have to do this as follows: First - you add the column with a correctly defined data type, and second you create a constrained list of permitted values on that column as a check constraint.
Alter table results add (cal varchar2(1));
Alter table results add constraint chk_cal CHECK (cal in ('A','B')) ENABLE;
(EDITED to fix brackets in constraint creation line)

Oracle drop constraint unique and re-add it

I've drop a constraint unique key with command:
ALTER TABLE table
DROP CONSTRAINT UNIQUE uk_nome;
it's removed because I don't see it, but when I try to re-add it with different parameter:
ALTER TABLE tale ADD CONSTRAINT UK_name UNIQUE (uk_1, uk_2);
I receive the error:
Errore SQL: ORA-00955: name is already used by an existing object.
Where is the problem?
You drop uk_nome and then add uk_name. I mean that there is a typo in uk_nome.
Resolved, the unique key is present, like index, into the system in the table dba_objects

Create constraint in alter table without checking existing data

I'm trying to create a constraint on the OE.PRODUCT_INFORMATION table which is delivered with Oracle 11g R2.
The constraint should make the PRODUCT_NAME unique.
I've tried it with the following statement:
ALTER TABLE PRODUCT_INFORMATION
ADD CONSTRAINT PRINF_NAME_UNIQUE UNIQUE (PRODUCT_NAME);
The problem is, that in the OE.PRODUCT_INFORMATION there are already product names which currently exist more than twice.
Executing the code above throws the following error:
an alter table validating constraint failed because the table has
duplicate key values.
Is there a possibility that a new created constraint won't be used on existing table data?
I've already tried the DISABLED keyword. But when I enable the constraint then I receive the same error message.
You can certainly create a constraint which will validate any newly inserted or updated records, but which will not be validated against old existing data, using the NOVALIDATE keyword, e.g.:
ALTER TABLE PRODUCT_INFORMATION
ADD CONSTRAINT PRINF_NAME_UNIQUE UNIQUE (PRODUCT_NAME)
NOVALIDATE;
If there is no index on the column, this command will create a non-unique index on the column.
If you are looking to enforce some sort of uniqueness for all future entries whilst keeping your current duplicates you cannot use a UNIQUE constraint.
You could use a trigger on the table to check the value to be inserted against the current table values and if it already exists, prevent the insert.
http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14251/adfns_triggers.htm
or you could just remove the duplicate values and then enfoce your UNIQUE constraint.
EDIT: After Jonearles and Jeffrey Kemp's comments, I'll add that you can actually enable a unique constraint on a table with duplicate values present using the NOVALIDATE clause but you'd not be able to have a unique index on that constrained column.
See Tom Kyte's explanation here.
However, I would still worry about how obvious the intent was to future people who have to support the database. From a support perspective, it'd be more obvious to either remove the duplicates or use the trigger to make your intent clear.
YMMV
You can use deferrable .
ALTER TABLE PRODUCT_INFORMATION
ADD CONSTRAINT PRINF_NAME_UNIQUE UNIQUE (PRODUCT_NAME)
deferrable initially deferred NOVALIDATE;

Oracle unique constraint error message

I am maintaining a legacy application and I recently got contacted that people are getting an error message when they try to fill one of our oracle tables. Now, those oracle tables are not in our care, but I still want to try out something to help find the problem.
Anyway, the error message is the following:
java.sql.SQLException: ORA-00001: unique constraint (REO0.PK_TableName) violated :
I know I can find a lot of information online through google and here about this error message. That is not what my question is about.
The question is: the tablename shown here (which I put in bold), is that
the name of the table, or is the
PK_ part added to represent 'primary key' ?
Reason why I ask is: I can't directly get to this database, but somehow I can see all tables in REO0 and I can find one with TableName but not one with *PK_TableName* as the name for a table. So if this PK_ would refer to something like 'primary key' (which the constraint of is violated) then it would make a bit more sense.
PK_tablename is the name of the constraint, and as Alex Poole states in a good comment, it has been specified in the DDL (CREATE TABLE ... (columns, CONSTRAINT PK_tablename PRIMARY KEY(columns...) ), or ALTER TABLE ... ADD CONSTRAINT PK_tablename PRIMARY KEY(columns...) or CREATE UNIQUE INDEX PK_tablename ON ... (columns) for example). When no name has been given, Oracle generates a name which begins with SYS.
Note that usually PK_x suggests a primary key for table x, but your constraint might also be a foreign key constraint or a not null constraint for example.
The following query will tell you all:
SELECT * FROM all_constraints WHERE constraint_name = 'PK_TABLENAME'

Resources