views:

448

answers:

3

What's the syntax for an Update query for a table without a primary key?

Disclaimer: Frustratingly, adding a primary key is not an option. My program is a small program in a much larger system with poor data management. My development time does not include rewriting the other software.

Note: The database is Microsoft Access.

Note: Similar to: Excel: TableAdapter UpdateCommand for table without primary key

UPDATE: Am I correct in saying, "If the table in the database has no explicit primary key, then there can be no valid TableAdapter UpdateCommand?"

A: 

It's no different than if you have a primary key. However, you will have to set some sort of where clause that will allow you to uniquely identify a row.

Jason Down
Yes, but the TableAdapter will fail if the number of rows affected doesn't match what is expected (i.e., if you are updating one row from the DataTable, and more than one row matches the condition)
Thomas Levesque
Is there a way to simply replace the contents of the DataTable with the contents in memory (in the DataSet)? The table only has 50 rows or so.
Steven
@Steven: No, you have to use an UPDATE statement.
Henk Holterman
A: 

Which DBMS are you using ? On Oracle (and probably others, but I have more experience with Oracle), there is a ROWID pseudo-column that you can use as a unique row identifier, even when there's no primary key

Thomas Levesque
MS Access (I just edited main post)
Steven
+1  A: 

If there isn't an explicit primary key, there should at least be an implicit primary key (even if it's every column). Without any sort of key, you won't be able to safely update the table.

If you go through the wizard when creating the dataset, you should get an update query that includes an update statement similar to this:

update TableA
set Column1 = @Column1, Column2 = @Column2 ...
where Column1 = @PreviousColumn1 and Column2 = @PreviousColumn2 ...

EDIT
You won't be able to use the wizard for update or delete commands without a PK on the table. You can, however, make a copy of the Access file put a PK on the table (if you can't derive a short implicit key, you may have to use every column) and use that to create the commands via the wizard.

If you don't want to go through that step, then you'll have to create a query similar to the one above. The @PreviousColumnX parameters would have their SourceVersion values set to Original.

update TableA
set Column1 = @Column1, Column2 = @Column2 ...
where (Column1 = @PreviousColumn1 or @PreviousColumn1 is null)
    and Column2 = @PreviousColumn2 ...
Austin Salonen
I think one is NOT generated for you if you don't have a primary key (and possibly if you have join clause you don't get one... I remember something about that... it's been a while). You'll have to manually create one, yes.
Jason Down
According to this post: http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataproviders/thread/4b5b3007-7a6e-4372-ae34-578d0b33725a adding a primary key must be also be done in the database.
Jason Down
Am I correct in saying, "If the table in the database has no explicit primary key, then there can be no valid TableAdapter UpdateCommand?"
Steven
No. The designer just won't generate one for you.
Austin Salonen
Will this process still work if some columns have NULL values?
Steven
You end up with slightly different syntax. I'll verify what it is and update my answer.
Austin Salonen
Are @Column and @PreviousColumn predefined macros that will automatically populate with the corresponding entries?
Steven
Not when you create them by hand. That's why I recommend the "copy and modify the database" approach so the wizard will generate it them for you.
Austin Salonen