How to solve this please

Mary Sachet 41 Reputation points
2022-11-06T10:47:20.457+00:00

This is my SQL it keeps saying this

Failed to execute query. Error: Violation of PRIMARY KEY constraint 'PK__Public_H__B43318E961C9E79D'. Cannot insert duplicate key in object 'dbo.Public_Housing_Building'. The duplicate key value is (112).
The statement has been terminated.

INSERT INTO [dbo].Public_Housing_Building
VALUES(109,'APT1','16_Spook_Street','Glen Iness','NSW',2017,2022,30),
(110,'APT2','26_Sava_Avenue','Square','NSW',1070,2022,45),
(112,'APT4','86_Chisholm_Parade','Broad','NSW',2906,2019,10),
(111,'APT3','76_Sylvan_Road','DeerBush','NSW',2621,2020,20),
(112,'APT4','86_Chisholm_Parade','Broad','NSW',2906,2019,10),
(113,'APT5','55_Ireland_Road','Discovery','NSW',3017,2022,50),
(114,'APT6','22_Budget_Place','Grove', 'NSW',4018,2018,60),
(115,'APT7','9_Parklands_Street','Hill','NSW',2211,2017,10),
(116,'APT8','2_Wonga_Avenue','Bare', 'NSW',2421,2022,22),
(117,'APT9','19_Monic_Cresent','Kotlash','NSW',6021,2021,65),
(118,'APT10','25_Sunset_Close','Gabo','NSW',2554,2021,15);

Please help me

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,689 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,552 questions
0 comments No comments
{count} votes

Accepted answer
  1. CosmogHong-MSFT 22,781 Reputation points Microsoft Vendor
    2022-11-07T06:15:32.48+00:00

    Hi @Mary Sachet

    Error: Violation of PRIMARY KEY constraint 'PK_Public_H_B43318E961C9E79D'. Cannot insert duplicate key in object 'dbo.Public_Housing_Building'. The duplicate key value is (112).

    The PRIMARY KEY constraint uniquely identifies each record in a table. Primary keys must contain UNIQUE values, and cannot contain NULL values.

    The error message has told you that The duplicate key value is (112). Therefore, as pvirzi answered, you need to remove duplicate INSERT data whose Public_Housing_Number value is 112.
    257699-image.png

    Best regards,
    LiHong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    3 people found this answer helpful.

3 additional answers

Sort by: Most helpful
  1. Pio Virzi 21 Reputation points
    2022-11-06T12:01:45.983+00:00

    Cheers,
    you have there two identical rows in your insert statement:

    (112,'APT4','86_Chisholm_Parade','Broad','NSW',2906,2019,10),
    (111,'APT3','76_Sylvan_Road','DeerBush','NSW',2621,2020,20),
    (112,'APT4','86_Chisholm_Parade','Broad','NSW',2906,2019,10),

    just remove one of them and it should work, if there isn't already one dataset with key '112' in your table.
    Your keys need to be unique, you can't use them twice.

    Greetings

    4 people found this answer helpful.

  2. Olaf Helper 40,741 Reputation points
    2022-11-07T06:11:33.357+00:00

    Violation of PRIMARY KEY constraint ... The duplicate key value is (112).

    You got a pretty clear error message, so what's the problem? You have 112 twice in your insert statement.

    3 people found this answer helpful.

  3. Mary Sachet 41 Reputation points
    2022-11-08T01:47:40.937+00:00

    Hi thank you all, I also have an issue with this other Quary it keeps saying

    Failed to execute query. Error: There are fewer columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement.

    INSERT INTO Unit_Building
    VALUES
    (200,109,3,1,1,1, SQUARE (110)),
    (201,111,1,2,1,1, SQUARE (125)),
    (202,112,2,3,1,1, SQUARE (130)),
    (203,113,3,1,1,1, SQUARE (140)),
    (204,114,4,1,1,1, SQUARE (150)),
    (205,115,2,3,1,1, SQUARE (120)),
    (206,116,1,1,1,1, SQUARE (180)),
    (207,117,4,2,1,1, SQUARE (190)),
    (208,118,3,1,1,1, SQUARE (175)),
    (209,119,3,2,1,1, SQUARE (200));