SQL INSERT 违反主键
我有一个表,其主键由三列组成: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您正在插入主键字段中具有相同数据的 3 行。
当然,你会遇到PK违规的情况。
将代码更改为:
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:
观察您的查询,我看到您尝试在具有主键的表中插入三个完全相等的记录(这是违反的,因为构建 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'
)!!我猜“三个主键”是指复合键。在您的情况下,您的主键不能有重复数据,并且
CODE_TARIF
、UNITE
和MODE_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
, andMODE_LIV
are three duplicate records.如果我正确地阅读了您的帖子,您有一个由(CODE_TARIF、UNITE 和 MODE_LIV)组成的复合 PK
在这种情况下,您的插入尝试使用 PK 插入所有 3 行
如果您不打算插入完全相同的数据 3 次,然后将您的
UNION ALL
替换为UNION
,这样只会插入该行一次。如果您打算使用代理自动递增 PK,则需要将表 DDL 更改为
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
If you don't intend inserting the exact same data 3 times, then replace your
UNION ALL
withUNION
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
主键用于标识单行数据。
在您的示例中,您将插入 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