违反 PRIMARY KEY 约束“PK_Address".等等......我做错了什么? [英] Violation of PRIMARY KEY constraint 'PK_Address'. etc...what am I doing wrong?

查看:66
本文介绍了违反 PRIMARY KEY 约束“PK_Address".等等......我做错了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

晚上.我对 SQL 还很陌生,但我已经做了很多鬼混.我正在按照我在网上找到的指南在 21 天内学习 SQL,但我在弄清楚我收到的错误是什么原因时遇到了一些麻烦.

Evening all. I am fairly new to SQL but have been doing quite a bit of fooling around. I am following a guide I found online to learn SQL in 21 days and I am having a bit of trouble figuring out what the error I am receiving is causing.

我正在尝试将数据INSERT 插入现有表中.此表的主键是 AddressID.我试图输入的数据在下面的代码中:

I am trying to INSERT data into an existing table. The Primary Key for this table is AddressID. The data I am trying to enter is in the code below:

INSERT INTO [dbo].[Address]
       (AddressID,Street,City,State,ZipCode)
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);
GO

我不断收到此错误:

Msg 2627, Level 14, State 1, Line 2 违反 PRIMARY KEY约束'PK_Address'.无法在对象中插入重复键'dbo.Address'.重复的键值为 (1).

Msg 2627, Level 14, State 1, Line 2 Violation of PRIMARY KEY constraint 'PK_Address'. Cannot insert duplicate key in object 'dbo.Address'. The duplicate key value is (1).

我已经尝试了几乎所有我能想到的方法,但我无法更新我的表格.我在 INSERT INTO 部分下的列名是正确的,我的 VALUES 也是正确的.我确实发现我可以使用单个 INSERT 语句来获取表中的数据.最初我使用 4 个单独的语句.

I have tried just about everything I can think of but I am unable to get my table updated. My column names at under the INSERT INTO portion are correct and my VALUES are also correct. I did find that I can use a single INSERT statement to get my data in the table. Originally I was using 4 separate statements.

我做错了什么?

推荐答案

在这种情况下,我认为让数据库通过不指定主键来选择主键是安全的.表中可能已经有数据了.

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

这篇关于违反 PRIMARY KEY 约束“PK_Address".等等......我做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆