0

Customers と Records という 2 つのテーブルを作成しました。レコードには、customerID に対する外部キー制約があります。既に存在する顧客にレコードを挿入しようとすると、次のエラーが表示されます。

  Message (The INSERT statement conflicted with the FOREIGN KEY constraint "FK_REC_cstmr_int_id". The conflict occurred in database "Omitted", table "dbo.CST_NEW_CUSTOMER", column 'cstmr_int_id'.)

挿入コードは次のとおりです。

INSERT INTO [Omitted].[dbo].[REC_NEW_RECORDS]
       ([cstmr_int_id]
       ,[xml_tx]
VALUES
       (10
       ,'<test>test</test>'
GO

ここで見つけた関連する質問のほとんどは、間違った順序で挿入することについて話しましたが、ID 10 の顧客を選択できます。

編集1:これは1人の顧客を返します

SELECT [cstmr_int_id]
   FROM [Omitted].[dbo].[CST_NEW_CUSTOMER] WHERE cstmr_int_id =10

編集 2: レコード テーブルの作成スクリプトは次のとおりです。

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[REC_NEW_RECORDS](
[rec_int_id] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
[cstmr_int_id] [int] NOT NULL,
[xml_tx] [varchar](max) NULL,
CONSTRAINT [REC_PK_rec_int_id] PRIMARY KEY CLUSTERED 
(
[rec_int_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF,ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[REC_NEW_RECORDS]  WITH CHECK ADD  CONSTRAINT [FK_REC_cstmr_int_id] FOREIGN KEY([cstmr_int_id])
REFERENCES [dbo].[CST_NEW_CUSTOMER] ([cstmr_int_id])
GO
4

3 に答える 3

5

その値は、参照されたテーブルに存在しません。10参照されたテーブルにないシナリオは次のとおりです。

設定

create table cst_new_customer (
  cstmr_int_id int not null primary key);

insert into cst_new_customer (cstmr_int_id) values (9), (11);

create table rec_new_records (
  cstmr_int_id int not null primary key,
  xml_tx varchar(50));

alter table rec_new_records add constraint fk_rec_cstmr_int_id
foreign key (cstmr_int_id) references cst_new_customer (cstmr_int_id);

テスト 1

insert into rec_new_records values (10, 'test');

結果

INSERT ステートメントが FOREIGN KEY 制約 "fk_rec_cstmr_int_id" と競合しました。データベース「db_3_055da」、テーブル「dbo.cst_new_customer」、列「cstmr_int_id」で競合が発生しました。:

テスト 2

insert into rec_new_records values (11, 'test');

結果

| | CSTMR_INT_ID | XML_TX |
|--------------|--------|
| | 11 | テスト |

デモを見る

于 2013-10-29T17:15:59.613 に答える
1

上記のすべてのクエリ/ステートメントに同じ接続を使用していますか?

問題は、コミットされていないトランザクションに関連している可能性があります。

于 2013-10-29T17:11:33.707 に答える
0

One possible scenario for this problem that I experienced today was that I had two foreign key constraints. I only inserted one of them in the referenced table but I was unaware of the second foreign key constraint that was not inserted.

于 2018-01-05T13:40:56.433 に答える