Click here to Skip to main content
15,892,768 members
Please Sign up or sign in to vote.
1.73/5 (4 votes)
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
Posted
Comments
__TR__ 9-Jan-13 1:38am    
You can use triggers to achieve this.

HI,

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.
 
Share this answer
 
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.
 
Share this answer
 
v2
Hi,

Add validation before insert or Add Unique Index (Non Cluster) Or constrain
http://msdn.microsoft.com/en-us/library/ms188258.aspx
 
Share this answer
 
v2
Comments
Sandeep Mewara 9-Jan-13 1:39am    
For an insertion of data? Sorry, Nopes!
Create an unique index like below:
SQL
CREATE UNIQUE INDEX IX_FirstName_LastName_Age ON [TableName] (FirstName, LastName, Age)

But this is not the preferred approach, you need to have a primary key in this table that will like (let's assume you are working with patient) PatientId,FirstName, LastName and Age and PatiendId will be the primary key which will maintain uniqueness of the table.
 
Share this answer
 
v3

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