Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 与外键约束冲突_Sql_Sql Server 2008 - Fatal编程技术网

Sql 与外键约束冲突

Sql 与外键约束冲突,sql,sql-server-2008,Sql,Sql Server 2008,我创建了一个凭证录入系统,比如会计软件 但我收到一条错误消息 Msg 547, Level 16, State 0, Line 1 The INSERT statement conflicted with the FOREIGN KEY constraint "Payment_Voucher_All_Voucher_List". The conflict occurred in database "Accounting12", table "dbo.Payment_Voucher". The

我创建了一个凭证录入系统,比如会计软件

但我收到一条错误消息

Msg 547, Level 16, State 0, Line 1
The INSERT statement conflicted with the FOREIGN KEY constraint "Payment_Voucher_All_Voucher_List". The conflict occurred in database "Accounting12", table "dbo.Payment_Voucher".

The statement has been terminated.
请告诉我我的数据库有什么问题
数据库模式在这里

CREATE TABLE [dbo].[Receipt_Voucher] (
    [Id] INTEGER IDENTITY(90000000,1) NOT NULL,
    [Voucher_No] VARCHAR(50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [Date] DATE,
    [Dr_Account] INTEGER,
    [Dr_Amount] MONEY,
    [Cr_Account] INTEGER,
    [Cr_Amount] MONEY,
    [voucher_type] VARCHAR(40) NOT NULL,
    CONSTRAINT [PK_Receipt_Voucher] PRIMARY KEY CLUSTERED ([Voucher_No], [voucher_type])
)
GO


/* ---------------------------------------------------------------------- */
/* Add table "Payment_Voucher"                                            */
/* ---------------------------------------------------------------------- */

CREATE TABLE [dbo].[Payment_Voucher] (
    [Id] INTEGER IDENTITY(90000000,1) NOT NULL,
    [Voucher_No] VARCHAR(50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [Date] DATE,
    [Dr_Account] INTEGER,
    [Dr_Amount] MONEY,
    [Cr_Account] INTEGER,
    [Cr_Amount] MONEY,
    [voucher_type] VARCHAR(40) NOT NULL,
    CONSTRAINT [PK_Payment_Voucher] PRIMARY KEY CLUSTERED ([Voucher_No], [voucher_type])
)
GO


/* ---------------------------------------------------------------------- */
/* Add table "All_Voucher_List"                                           */
/* ---------------------------------------------------------------------- */

CREATE TABLE [dbo].[All_Voucher_List] (
    [Voucher_No] VARCHAR(50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    [voucher_type] VARCHAR(40) NOT NULL,
    CONSTRAINT [PK_Voucher] PRIMARY KEY CLUSTERED ([Voucher_No], [voucher_type])
)
GO


/* ---------------------------------------------------------------------- */
/* Foreign key constraints                                                */
/* ---------------------------------------------------------------------- */

ALTER TABLE [dbo].[All_Voucher_List] ADD CONSTRAINT [Receipt_Voucher_All_Voucher_List] 
    FOREIGN KEY ([Voucher_No], [voucher_type]) REFERENCES [dbo].[Receipt_Voucher] ([Voucher_No],[voucher_type])
GO


ALTER TABLE [dbo].[All_Voucher_List] ADD CONSTRAINT [Payment_Voucher_All_Voucher_List] 
    FOREIGN KEY ([Voucher_No], [voucher_type]) REFERENCES [dbo].[Payment_Voucher] ([Voucher_No],[voucher_type])
GO
输入收据凭证

INSERT INTO dbo.Receipt_Voucher
        ( Voucher_No ,
          Date ,
          Dr_Account ,
          Dr_Amount ,
          Cr_Account ,
          Cr_Amount ,
          voucher_type
        )
VALUES  ( '0001' , -- Voucher_No - varchar(50)
          '2013-03-13 08:15:28' , -- Date - date
          5 , -- Dr_Account - int
          500 , -- Dr_Amount - money
          2 , -- Cr_Account - int
          500 , -- Cr_Amount - money
          '2'  -- voucher_type - varchar(40)
        )
现在进入所有凭证列表

INSERT INTO dbo.All_Voucher_List
        ( Voucher_No, voucher_type )
VALUES  ( '0001', -- Voucher_No - varchar(50)
          '2'  -- voucher_type - varchar(40)
          )
但也有一些错误

Msg 547, Level 16, State 0, Line 1
The INSERT statement conflicted with the FOREIGN KEY constraint "Payment_Voucher_All_Voucher_List". The conflict occurred in database "Accounting12", table "dbo.Payment_Voucher".
The statement has been terminated.

外键只能引用一个父表。

在您的情况下,
所有凭证列表的
[凭证编号],[凭证类型]
表引用了两个表
[收据凭证]
[付款凭证]

当您尝试将记录插入到
收据\u凭证
中时,您的第二个FK会阻止您这样做,因为您在
付款\u凭证
表中没有相应的记录


您可以阅读有关该主题的更多内容

如果正确,您的第二个约束不允许插入

我的第一个想法是创建一个包含所有凭证的表,并在单个约束中使用它,但我知道您的第三个表正是该表

因此,我的建议是在填充前两个表时,使用触发器或其他方法填充该表

我知道这是另一段需要维护的代码,但其他可能的解决方法(如使用视图或复制键引用两个表)只会使数据模型更加复杂,几乎没有好处,并且会给将来处理这些表的人员带来问题