我想更新表格上的电子邮件地址字段。我的问题:
UPDATE TableName
SET emailaddress = 'someone@somewhere.com'
WHERE Per_ID = '1234'
我收到了这个回复:
Violation of PRIMARY KEY constraint 'PK_TableName'.
Cannot insert duplicate key in object 'TableName'.
问题(S):
提前致谢!
这是创建表脚本:
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
答案 0 :(得分:-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