0

I have a Database DB with a table name population but with no Primary Key. Means it can have duplication of data. For example: I have 5 families (f1,f2,f3,f4,f5) with different members inside it (and members may have same name). So I can have exactly same type of record in more than 1 row. Now I want to edit just 1 member of the family, but it is editing all the duplicate records. What I want to do is, I just want to Update my Database once and only once. In other words, I want to use UPDATE command to execute just once. How to do it? I am using Sql Server Express, VS2010, C# 4.0 (if this info matters). I am pretty sure my problem may sound stupid to some people (probably many). But it is just a dummy of my problem. Any help or suggestion will be greatly appreciated. Thanks

5
  • 2
    If the names are duplicate and you don't have a primary key, how would identify the "one" row you want updated? Commented Sep 27, 2011 at 18:24
  • hmmm, by checking different column names, and no matter how many rows it encountered to be matching, it has to update just one and exit. but to your query, one upvote Commented Sep 27, 2011 at 18:26
  • You still have to answer the question of which row to update. Commented Sep 27, 2011 at 18:28
  • it has to update the first matching column (based on where clause), and then exit Commented Sep 27, 2011 at 18:31
  • "First" is not identified with a WHERE clause. It would require the use of an ORDER BY. So, how do you order the many encountered rows to determine which is "first?" Commented Sep 27, 2011 at 18:34

4 Answers 4

2

I know it's not exactly what you're asking but seriously, the easiest option is to alter the database to have a primary key and use that. Perhaps an Identity key....

Without that, you could update just one record, but you have no guarantee of which record. This is why primary keys are such as fundamental concept. I suppose this doesn't really matter if they are all the same, so....

If you really want to proceed without a primary key, you need to use the TOP keyword as shown here: How do I update n rows in a table?

Set it to

UPDATE TOP 1 ....
Sign up to request clarification or add additional context in comments.

Comments

1

Add an identity column, ID int with auto increment on. Then update using the id's.

CREATE TABLE dbo.Family
(
    Id int NOT NULL IDENTITY (1, 1),
    FamilyName varchar(50) NULL
)

update dbo.Family set FamilyName = 'xxx' where Id = y

1 Comment

surely will consider adding a identity column
1

In case you can't add an identity column for some reason:

UPDATE TOP ( 1 ) Families
SET    Whatever = 'Your Value', ...
WHERE  <Your where clause>

6 Comments

Define "correct." If the rows are all identical, the idea is to change any one of them. If the rows are not identical, don't do it this way -- use a WHERE clause that gets the correct row.
But updating random records is so much easier than finding the right record! Like I always say, It's SO much easier to speculate wildly than to actually do research and find the right answer.
@Jay Why of course... :) I can't think of a scenario when I would ever have completely duplicate records in a table on purpose, but that's just me.
@Andrew: Yeah, I'm trying to think of a scenario. Maybe a log file or audit trail could have exact duplicates if the same event happened twice -- but even then you'd likely have timestamps that would differ. Can anybody suggest a scenario? It almost sounds like an interesting puzzle.
@Jay Even in an audit trail or log file you can throw an identity column on there. For all the things my predecessors at my current job got wrong in the database (no unique contraint on username? wtf? it was fun when that blew up), at least they had the sense to use something for a primary key in the audit tables.
|
0

The real answer is: Fix your database design. Every record should have some unique identifier. Add an auto-increment field or something.

To directly answer your question, you can say "update top (1) ..." to only update one record. But without some unique identifier, how do you know which record to update? The program will essentially update a random record. Which takes me back to point 1.

Edit: Whoops, my original answer was for a different engine. Corrected above.

1 Comment

i will definitely review my database design and will consider adding a id column. Thanks for your help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.