Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

i have an assignment that i have to do in SQL Server Manager.

I have a database, and a table named User. Under user there are a column named e-mail.

The assignment is to create a new constraint to that table that affect the column e-mail.

There has to be only one '@' and minimum one '.'

It is not allowed to be any special characters such as ( !, ", #, ¤, %, etc.) <- this do not include the '@' and '.'

I've tried some different things but cant seem to make it work. Also it should be noticed that I am a beginner.

Thanks for your help

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
185 views
Welcome To Ask or Share your Answers For Others

1 Answer

Alternatively you can create your table with constraint, like this which will check if all the given conditions satisfies. Please change table data with your's

If table not Exist

CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Email  varchar(255),
CHECK (len(Email) - len(replace(Email,'@',''))=1 AND len(Email) - 
len(replace(Email,'.',''))=1 AND CHARINDEX('!',Email)!>0 AND 
CHARINDEX('#',Email)!>0 AND CHARINDEX('%',Email)!>0 AND 
CHARINDEX('¤',Email)!>0 AND CHARINDEX('"',Email)!>0)
);

Some Rejected Inputs

some@some.!%#¤com

some@some.!%#com

some@some.%#com

some@some.#com

some@some@com

!some@some@com

etc...

Some Excepted Inputs

some@some.com

some222@some.com

harry_porter@some.com

332@some.com

etc....

If you have already a table then

 GO

 ALTER TABLE [dbo].[Yourtablename]  WITH CHECK ADD Constraint EmailConstraint CHECK  (((len([Email])-
 len(replace([Email],'@','')))=(1) AND (len([Email])-
 len(replace([Email],'.','')))=(1) AND charindex('!',[Email])<=(0) AND 
 charindex('#',[Email])<=(0) AND charindex('%',[Email])<=(0) AND 
 charindex('¤',[Email])<=(0) AND charindex('"',[Email])<=(0)))
 GO

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...