SQL INSERT 违反主键

发布于 2024-12-11 03:03:46 字数 611 浏览 1 评论 0原文

我有一个表,其主键由三列组成:CODE_TARIF、UNITE、MODE_LIV。在我的表中,有 3 条记录的 MODE_LIV = 2。然后我使用 MODE_LIV = 4 插入一条记录。

INSERT INTO T_TARIF (
    CODE_TARIF, ZONE, UNITE, MODE_LIV, LIBELLE, TR_DEBUT, TR_FIN, MONTANT
) 
SELECT 'Livr_A_50_99', '2', '1', '4', 'sdg', '50', '99', '90' 
UNION ALL 
SELECT 'Livr_A_50_99', '2', '1', '4', 'sdg', '50', '99', '90' 
UNION ALL 
SELECT 'Livr_A_50_99', '2', '1', '4', 'sdg', '50', '99', '90'

它返回一个错误(违反主键),这没有任何意义,因为 MODE_LIV = 2 不等于 MODE_LIV = 4

我知道我可以添加代理键作为自动递增标识列,但在我的情况下这不是一个选项。

I have a table with a primary key composed of three columns: CODE_TARIF, UNITE, MODE_LIV. In my table there are three records with MODE_LIV = 2. Then I insert a record with MODE_LIV = 4.

INSERT INTO T_TARIF (
    CODE_TARIF, ZONE, UNITE, MODE_LIV, LIBELLE, TR_DEBUT, TR_FIN, MONTANT
) 
SELECT 'Livr_A_50_99', '2', '1', '4', 'sdg', '50', '99', '90' 
UNION ALL 
SELECT 'Livr_A_50_99', '2', '1', '4', 'sdg', '50', '99', '90' 
UNION ALL 
SELECT 'Livr_A_50_99', '2', '1', '4', 'sdg', '50', '99', '90'

It returns an error (violation of primary key), which doesn't make any sense because MODE_LIV = 2 is not equal to MODE_LIV = 4.

I know I could add a surrogate key as an auto-incrementing identity column, but in my situation that isn't an option.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

南城旧梦 2024-12-18 03:03:46

您正在插入主键字段中具有相同数据的 3 行。
当然,你会遇到PK违规的情况。

将代码更改为:

INSERT INTO T_TARIF (CODE_TARIF, ZONE, UNITE, MODE_LIV, LIBELLE, TR_DEBUT, TR_FIN, MONTANT) 
SELECT 'Livr_A_50_99', '2', '1', '3', 'sdg', '50', '99', '90' 
UNION ALL 
SELECT 'Livr_A_50_99', '2', '1', '4', 'sdg', '50', '99', '90' 
UNION ALL 
SELECT 'Livr_A_50_99', '2', '1', '5', 'sdg', '50', '99', '90'

//Different data for every row    ^

You are inserting 3 rows with identical data in the primary key fields.
Of course you're going to get PK violations.

Change the code to:

INSERT INTO T_TARIF (CODE_TARIF, ZONE, UNITE, MODE_LIV, LIBELLE, TR_DEBUT, TR_FIN, MONTANT) 
SELECT 'Livr_A_50_99', '2', '1', '3', 'sdg', '50', '99', '90' 
UNION ALL 
SELECT 'Livr_A_50_99', '2', '1', '4', 'sdg', '50', '99', '90' 
UNION ALL 
SELECT 'Livr_A_50_99', '2', '1', '5', 'sdg', '50', '99', '90'

//Different data for every row    ^
身边 2024-12-18 03:03:46

观察您的查询,我看到您尝试在具有主键的表中插入三个完全相等的记录(这是违反的,因为构建 PK 的三个字段是相同的 'Livr_A_50_99', '1', '4')!!

Watching your query I see three perfectly equal records you're trying to insert in a table having a primary key (which is violated because three fields building PK are the same 'Livr_A_50_99', '1', '4')!!

一个人练习一个人 2024-12-18 03:03:46

我猜“三个主键”是指复合键。在您的情况下,您的主键不能有重复数据,并且 CODE_TARIFUNITEMODE_LIV 的组成是三个重复记录。

I'm guessing by "three primary keys" you mean a composite key. In your case, your primary key cannot have duplicate data, and the composition of CODE_TARIF, UNITE, and MODE_LIV are three duplicate records.

青衫负雪 2024-12-18 03:03:46

如果我正确地阅读了您的帖子,您有一个由(CODE_TARIF、UNITE 和 MODE_LIV)组成的复合 PK

在这种情况下,您的插入尝试使用 PK 插入所有 3 行

'Livr_A_50_99','1', '4'

如果您不打算插入完全相同的数据 3 次,然后将您的 UNION ALL 替换为 UNION ,这样只会插入该行一次。

如果您打算使用代理自动递增 PK,则需要将表 DDL 更改为

CREATE TABLE T_TARIF
(
    T_TARIF_Id INT identity(1,1) NOT NULL,
... Other fields here

)
GO

ALTER TABLE T_TARIF ADD CONSTRAINT PK_Tariff PRIMARY KEY(T_TARIF_Id)
GO

If I read your post correctly, you have a composite PK consisting of (CODE_TARIF, UNITE, and MODE_LIV)

In this case, your insert tries to insert all 3 rows with the PK

'Livr_A_50_99','1', '4'

If you don't intend inserting the exact same data 3 times, then replace your UNION ALL with UNION which will just insert the row once.

If you intend using a surrogate auto incrementing PK instead you will need to change your table DDL to

CREATE TABLE T_TARIF
(
    T_TARIF_Id INT identity(1,1) NOT NULL,
... Other fields here

)
GO

ALTER TABLE T_TARIF ADD CONSTRAINT PK_Tariff PRIMARY KEY(T_TARIF_Id)
GO
水染的天色ゝ 2024-12-18 03:03:46

主键用于标识单行数据。

在您的示例中,您将插入 3 行相同的数据。这意味着主键将识别所有三行,而不仅仅是一行。根据定义,这是主键违规。

您的选择是:
- 添加新字段(例如自动递增 id)以区分行
- 仅插入重复行之一
- 修改您的数据,使其只需要插入一次(例如添加“计数”字段)
- 修改您的数据,使 PK 字段与任何其他行不相同

A primary key is used to identify a single row of data.

In your example you are inserting 3 identical rows of data. This means that the primary key would identify all three rows, not just one. That's a Primary Key violation by definition.

Your options are:
- Add a new field (such as an autoincrementing id) to differentiate between the rows
- Insert just one of the duplicate rows
- Modify your data so that it only requires inserting once (Such as adding a 'count' field)
- Modify your data so that the PK fields are not identical to any other row

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文