Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have to work on a program where two table having an email column now i have to work on to keep email value unique for both tables for insert and for update also
please help
thank in advance
Posted
Updated 19-Jun-12 22:05pm
v2

Do you mean that you have two separate tables, each with an email address, which may not be repeated in the other table?
If so, then just check the count of entries in the other table:
SQL
SELECT COUNT(email) FROM OtherTable WHERE email=ToBeInsertedEmailAddress
If it's zero, you can do it.

If that isn't what you mean, please explain a bit more clearly!
 
Share this answer
 
SQL
IF (EXISTS(SELECT email FROM dbo.email WHERE email = 'user2@email.in'))
BEGIN
PRINT 'Email is already there';
END
ELSE
BEGIN
      INSERT INTO dbo.email(email,name)
      VALUES ('user2@email.in','user2');
END
 
Share this answer
 
hi ,

you can use insert and update trigger to fix this issue bellow links will help you out
1
1)http://www.sqlteam.com/article/an-introduction-to-triggers-part-i
2)http://msdn.microsoft.com/en-us/library/aa258254(v=sql.80).aspx
 
Share this answer
 
While creating the tables make the email column as Unique so that user can't insert same value to that column.
If you want to implement manually than before every insertion or update check that is that value is already present or not.
 
Share this answer
 
Comments
naren programmers 20-Jun-12 4:26am    
the application which i got there is a user table and employer table both have a email column the field should not be the same as this is an email , i can not convert these table to one table and then apply unique constraint as the application is at its final stage
bhagirathimfs 20-Jun-12 5:10am    
See Solution 4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900