0

I want to update an email address field on a table. My query:

UPDATE TableName
    SET emailaddress = 'someone@somewhere.com'
WHERE Per_ID = '1234'

I get this response:

Violation of PRIMARY KEY constraint 'PK_TableName'. 
Cannot insert duplicate key in object 'TableName'.

Question(s):

  1. Is there a way to modify a field in a table that has PKs?
  2. Assuming I have to delete the row and insert a new row, should I worry about having a different PK?
  3. How would I do #2? :)

Thanks in advance!

Here's the create table script:

USE [Web_Production]
GO

/****** Object:  Table [dbo].[Supported_Users]    Script Date: 07/23/2013 10:48:37 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Supported_Users](
    [Supported_Users_ID] [int] IDENTITY(1,1) NOT NULL,
    [Per_ID] [nvarchar](32) NULL,
    [EmailAddress] [nvarchar](80) NOT NULL,
    [SerialNum] [nvarchar](20) NULL,
    [Password] [nvarchar](255) NULL,
    [OSC_LastLogonDate] [datetime] NULL,
    [OSC_TotalLogons] [int] NULL,
    [CP_LastLogonDate] [datetime] NULL,
    [CP_TotalLogons] [int] NULL,
    [IRC_LastLogonDate] [datetime] NULL,
    [IRC_TotalLogons] [int] NULL,
    [AddDate] [datetime] NOT NULL,
    [AddUser] [nvarchar](50) NOT NULL,
    [ChangeDate] [datetime] NULL,
    [ChangeUser] [nvarchar](50) NULL,
    [SupportAccess] [bit] NULL,
    [confirmationID] [nvarchar](10) NULL,
    [ForcedExpiryDate] [datetime] NULL,
    [ManualAddition] [bit] NULL,
    [Industry] [nvarchar](50) NULL,
    [Roles] [nvarchar](255) NULL,
    [OLL_Token] [nvarchar](40) NULL,
    [OLL_AddDate] [datetime] NULL,
    [ShowSupport] [bit] NULL,
    [ShowForum] [bit] NULL,
    [ShowKB] [bit] NULL,
    [ShowTraining] [bit] NULL,
    [ForceReset] [bit] NULL,
 CONSTRAINT [PK_Supported_Users] PRIMARY KEY CLUSTERED 
(
    [EmailAddress] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[Supported_Users] ADD  CONSTRAINT [DF_Supported_Users_TotalLogons]  DEFAULT (0) FOR [OSC_TotalLogons]
GO

ALTER TABLE [dbo].[Supported_Users] ADD  CONSTRAINT [DF_Supported_Users_CP_TotalLogons]  DEFAULT (0) FOR [CP_TotalLogons]
GO

ALTER TABLE [dbo].[Supported_Users] ADD  CONSTRAINT [DF_Supported_Users_IRC_TotalLogons]  DEFAULT (0) FOR [IRC_TotalLogons]
GO

ALTER TABLE [dbo].[Supported_Users] ADD  CONSTRAINT [DF_Supported_Users_ManualAddition]  DEFAULT (0) FOR [ManualAddition]
GO

ALTER TABLE [dbo].[Supported_Users] ADD  CONSTRAINT [DF_Supported_Users_ShowSupport]  DEFAULT (1) FOR [ShowSupport]
GO

ALTER TABLE [dbo].[Supported_Users] ADD  CONSTRAINT [DF_Supported_Users_ShowForum]  DEFAULT (1) FOR [ShowForum]
GO

ALTER TABLE [dbo].[Supported_Users] ADD  CONSTRAINT [DF_Supported_Users_ShowKB]  DEFAULT (1) FOR [ShowKB]
GO

ALTER TABLE [dbo].[Supported_Users] ADD  CONSTRAINT [DF_Supported_Users_ShowTraining]  DEFAULT (1) FOR [ShowTraining]
GO

ALTER TABLE [dbo].[Supported_Users] ADD  CONSTRAINT [DF_Supported_Users_ForceReset]  DEFAULT (0) FOR [ForceReset]
GO
4

1 回答 1

-1

PK 违规错误消息的原因是因为您尝试更新表中已经存在值的字段。

鉴于您知道要实现的目标,这就是 SQL

DELETE TableName
WHERE  emailaddress = 'someone@somewhere.com' AND Per_ID = '1234'

UPDATE TableName
    SET emailaddress = 'someone@somewhere.com'
WHERE Per_ID = '1234
于 2013-07-23T18:18:26.027 回答