Reputation: 59
I want to add user address to a SQL Server database but after entering one record it shows error on primary key. I am using uniqueidentifier
as a primary key column. Kindly suggest how to resolve this error. I am using SQL Server 2012 and the table's name is tbladdress
.
Error is
Violation of PRIMARY KEY constraint 'PK_Address_1'. Cannot insert duplicate key in object tblAddress. The duplicate key value is (00000000-0000-0000-0000-000000000000).
Table structure:
CREATE TABLE [dbo].[tblAddress]
(
[AddressID] UNIQUEIDENTIFIER NOT NULL,
[Username] VARCHAR(50) NOT NULL,
[UserId] BIGINT NOT NULL,
[mobile] VARCHAR(20) NOT NULL,
[Country] VARCHAR(50) NULL,
[State] VARCHAR(200) NULL,
[City] VARCHAR(200) NULL,
[pincode] VARCHAR(10) NULL,
[FullAddress] VARCHAR(300) NULL,
[landmark] VARCHAR(150) NULL,
[Isactive] BIT NULL,
[cdate] DATE NULL,
CONSTRAINT [PK_Address_1]
PRIMARY KEY CLUSTERED ([AddressID] ASC)
);
Upvotes: 1
Views: 355
Reputation: 25152
Judging from the error, it seems like you are trying to manually insert the record. Use NEWID()
instead.
insert into tblAddress (AddressID, Username, UserId, mobile)
values
(NEWID(),'John Cole',123456,'555555')
,(NEWID(),'John Anderson',65465465,'555444')
Upvotes: 2