Home » SQL & PL/SQL » SQL & PL/SQL » How? - Create Check constraint - field must contain @ symbol (Oracle llg Apex)
icon4.gif  How? - Create Check constraint - field must contain @ symbol [message #610781] Mon, 24 March 2014 05:08 Go to next message
Oracleuser14
Messages: 1
Registered: March 2014
Location: Antarctica
Junior Member
I need to create a check constraint for an email column/field, where the field must contain an "@" symbol.
Does sql (oracle or SQL in general) let you do this or do I need to use a different programming language.

If it SQl does let you do this, am I on the right track?

1. Tried myself:
ALTER TABLE Q_Customer
ADD CONSTRAINT Q_chk_Cus_email CHECK (Cus_email LIKE '%@%');

...but
ORA-02293: cannot validate (C3267304.Q_CHK_CUS_EMAIL) - check constraint violated


2. Tried from a forum: alter table Q_CUSTOMER
add constraint Q_Chk_cus_email check (Cus_email like '%_@__%.__%')

...but
ORA-02293: cannot validate (C3267304.Q_CHK_CUS_EMAIL) - check constraint violated

I'm using Apex 4.0. I need an answer its for my final year project at university - which is due in this week Laughing .

[Updated on: Mon, 24 March 2014 05:09]

Report message to a moderator

Re: How? - Create Check constraint - field must contain @ symbol [message #610783 is a reply to message #610781] Mon, 24 March 2014 05:22 Go to previous messageGo to next message
Littlefoot
Messages: 21807
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
If ALTER TABLE failed, it means that there are some records in that table that violate the constraint. If that's OK, use NOVALIDATE option, such as
ALTER TABLE Q_Customer
ADD CONSTRAINT Q_chk_Cus_email CHECK (Cus_email LIKE '%@%') NOVALIDATE;

Otherwise, fix those errors first and then create the constraint.

However, the way you are checking e-mails isn't a perfect one; the first one is satisfied with @littlefoot as an e-mail address (which is wrong). The following constraint is most probably not the perfect one either, but is much better than the ones you use now. Give it a try:
ALTER TABLE q_customer ADD CONSTRAINT chk_cus_email
CHECK (REGEXP_SUBSTR (
                cus_email,
                '[a-zA-Z0-9._%-]+@[a-zA-Z0-9._%-]+\.[a-zA-Z]{2,4}')
                IS NOT NULL); 
Re: How? - Create Check constraint - field must contain @ symbol [message #610795 is a reply to message #610781] Mon, 24 March 2014 06:52 Go to previous message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

A complete check expression for email has already provided here.
You can search it.

Previous Topic: Error while executing the plsql block for mview refresh
Next Topic: comparing the result of multiple SELECT statements
Goto Forum:
  


Current Time: Fri Apr 19 22:43:47 CDT 2024