Violation of PRIMARY KEY constraint 'PK_Address'. etc…what am I doing wrong?

柔情痞子 提交于 2019-12-05 13:47:16

In this case I think it's safe to let the database choose the primary key by not specifying it. There's probably already data in the table.

INSERT INTO [dbo].[Address]
   (Street,City,State,ZipCode)
VALUES
   ('2400 Broadway','New York','NY',11201),
   ('320 21st Street','Atlanta','GA',303),
   ('439 Skyline Blvd','Seattle','WA',98101),
   ('56 Park Avenue','Dallas','TX',75201);
GO

From the error message it is very clear that already AddressID = 1 exist in the table since you have a primary key on AddressID you cannot insert duplicate values.

Try this query to insert into the table

INSERT INTO [dbo].[Address]
   (Street,City,State,ZipCode)
SELECT Street,City,State,ZipCode 
FROM  ( VALUES (1,'2400 Broadway','New York','NY',11201), 
               (2,'320 21st Street','Atlanta','GA',303), 
               (3,'439 Skyline Blvd','Seattle','WA',98101), 
               (4,'56 Park Avenue','Dallas','TX',75201)) 
v(addressid, street, city, state, zipcode) 
WHERE  NOT EXISTS (SELECT 1 
                   FROM   [dbo].[address] A 
                   WHERE  a.addressid = v.addressid) 

If addressid columns has identity property then remove the addressid column from insert column list and select list.

What you doing wrong is:

  • You already have AddressId (1) in your table Address table. Primary key should be unique. You cannot insert duplicate values in the primary key column.

What you can probably do is:

  • update the existing value and insert the new values. That means update the row with AddressId 1 and insert new rows with new AddressIds.

It's better you let the Sql Server decide on your primary key value by creating `identity column'. You could create identity column while creating a table as follows:

CREATE TABLE [dbo].[Address]
(
 [AddressId] [int] IDENTITY(1,1) NOT NULL,
 [OtherColumns] [int] NULL,
 CONSTRAINT [PK_Address] PRIMARY KEY CLUSTERED  (AddressId ASC)
 )

Please note that, if you have created a table with Idenity column, you do not need to supply any values during insert operation because Sql Server implicitly creates and inserts that values for you.

Primary Keys have to be unique. If AddressID is your primary key you cannot have two rows in your table that have the same AddressID number. Primary Keys are usually set automatically so you don't have to set them yourself.

Think of the Primary Key as a unique identifier for each row. The duplicate key error is telling you that you already have a row in your address table that has an AddressID value of 1. To avoid these errors you should set your AddressID column to IDENTITY and not worry about setting the value. Try inserting your records without setting the AddressID. Something like so:

INSERT INTO [dbo].[Address]
       (Street,City,State,ZipCode)
VALUES
       ('2400 Broadway','New York','NY',11201),
       ('320 21st Street','Atlanta','GA',303),
       ('439 Skyline Blvd','Seattle','WA',98101),
       ('56 Park Avenue','Dallas','TX',75201)
       ('56 Park Avenue','Dallas','TX',75201);
 GO
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!