开发者

MS-Access: TableAdapter UpdateCommand for table without primary key

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?"


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.


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 ...


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜