检查两个表中一列的唯一值 [英] check for unique value for a column in two tables

查看:59
本文介绍了检查两个表中一列的唯一值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须处理一个程序,其中两个表都有一个电子邮件列,现在我必须继续工作,以使两个表的电子邮件值保持唯一,以便插入和更新.
请帮忙
预先感谢

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

推荐答案

您是说您有两个单独的表,每个表都有一个电子邮件地址,在另一个表中可能不会重复?
如果是这样,则只需检查另一个表中的条目数即可:
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:
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!


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




您可以使用插入和更新触发器来解决此问题,以下链接将为您提供帮助
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
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


这篇关于检查两个表中一列的唯一值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆