2017-07-25 134 views
-4

我正在更新數據庫中的Patient記錄,但是當我更新它時,它會覆蓋原始記錄。數據庫中的記錄更新

我想保留這兩個項目,但主鍵不會允許我爲同一個病人有多個記錄。

我該怎麼做?

+2

做一個'插入'而不是'更新'?如果您希望有人幫助您,您需要提供更多信息。 – Siyual

+0

是的,您可以,但您需要更改表格的主鍵定義。 –

+0

如上所述,最好總是顯示一些代碼以獲得更好的幫助 – Adrian

回答

-2

如果您進行更新,結果會覆蓋記錄。你必須做一個插入而不是更新,但是如果你對主鍵有限制,你會得到一個異常。嘗試刪除此主鍵(或唯一鍵),如果可能的話。

0

您可以使用此複合主鍵,對下面的例子來看看:

create table table1 
(
    ID int not null, 
    Number int not null, 
    Name nvarchar(30), 
    Note nvarchar(30), 
    DateCreated datetime 
) 
go 

alter table table1 add constraint PK_table1 primary key (ID, Number) 
go 

insert into table1 
values(1, 1, 'John', null, getdate()), 
    (2, 1, 'Maria', null, getdate()), 
    (3, 1, 'Simon', null, getdate()), 
    (4, 1, 'Alex', null, getdate()) 
go 

select * from table1 

-- insert 
insert into table1 
values (1, 2, 'John', 'changed address', getdate()) 
go 

select * from table1 

-- drop table table1 

或只使用查表其中主要的表的主鍵引用(更好的解決方案的大部分時間)。

相關問題