如何避免SQL中的重复记录 [英] How to avoid duplicate records in SQL

查看:122
本文介绍了如何避免SQL中的重复记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个包含FirstName,LastName和Age列的表。

我有一条记录详细介绍Tom,Cruise和各栏中的25个。

如果我尝试插入具有相同详细信息的另一条记录。它不应该被插入,因为它是一个重复的记录。

如何实现它?



谢谢

Hi,

I have a table with columns FirstName, LastName and Age.
And I''m having one record with details Tom, Cruise and 25 in respective columns.
If I try to insert another record with same details. It should not be inserted as it is a duplicate record.
How to achieve it?

Thank you

推荐答案

HI,



这可以用很多方式实现。



1)在数据库方面,你设置约束

2)在页面方面你打电话给SP存储在表中的任何你调用名字,名字将要存储在一个数据表中的信息和检查计数的年龄设为> 0表示无需插入== 0表示您插入查询。


This can achive in so many way''s.

1) In DataBase side you set the constraints.
2) In page wise u call the SP whatever you store in table u call the firstname,secondname,and age that information to be stored in one datatable and check the count is > 0 means no need to insert ==0 means you do inserting query.


读取 SQL检查约束



阅读:

MSDN:SQL CHECK约束 [ ^ ]

W3S:SQL CHECK约束 - 2 [ ^ ]



而不是在查询中有任何逻辑来处理它,你可以有适当的检查约束并顺利进行。其他方式是在操作之前在UI本身处理它。
Have a read on SQL Check Constraints.

Read:
MSDN: SQL CHECK Constraints[^]
W3S: SQL CHECK Constraint - 2[^]

Instead of having any logic in query to handle it, you can have check constraints in place and go ahead smoothly. Other way would be to handle it in UI itself before the operation.






在插入或添加唯一之前添加验证索引(非群集)或约束

http://msdn.microsoft.com/en-us/library/ms188258.aspx
Hi,

Add validation before insert or Add Unique Index (Non Cluster) Or constrain
http://msdn.microsoft.com/en-us/library/ms188258.aspx


这篇关于如何避免SQL中的重复记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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