在sql server 2012中,我有表PurchaseReturn(VendorId,PurchaseOrderId)
。 “Vendorid”列是与Vendor(VendorId)的外键关系,“Purchaseorderid”是Purchaseorder(purchaseorderi)。
现在我的目标是将'VendorId'的外键类型设置为'On delete在Update Cascade上设置Null',但对于'purchaseorderid',它应该是'On delete CasCade On Update Cascade'。
但是当我为VendorId创建FK并尝试通过以下查询为PurchaseOrderId创建它时:
alter table PurchaseReturn add constraint FK_PR_PORD foreign key (Purchaseorderid) references PurchaseOrder(Purchaseorderid) on delete cascade ON UPDATE CASCADE ';
错误如下所示: 在表'PurchaseReturn'上引入FOREIGN KEY约束'FK_PR_PORD'可能会导致循环或多个级联路径。指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。
有什么问题?...请提前帮助谢谢 这是具有约束
的创建表查询alonqUSE [AsifTraders]
GO
/****** Object: Table [dbo].[PurchaseReturn] Script Date: 24-Sep-14 11:46:39 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[PurchaseReturn](
[Date] [datetime] NOT NULL,
[PurchaseReturnId] [nvarchar](20) NOT NULL,
[VendorId] [nvarchar](8) NULL,
[Description] [nvarchar](50) NOT NULL,
[ReturnType] [nvarchar](20) NOT NULL,
[Deduction] [numeric](15, 0) NULL,
[ProductQuantity] [numeric](10, 2) NOT NULL,
[PurchaseValue] [numeric](12, 0) NOT NULL,
[ReturnValue] [numeric](15, 0) NOT NULL,
[Paid] [numeric](15, 0) NULL,
[PBalance] [numeric](15, 0) NULL,
[NBalance] [numeric](15, 0) NULL,
[JEntryId] [int] NOT NULL,
[PURCHASEORDERID] [nvarchar](8) NULL,
CONSTRAINT [PK_PurchaseReturn] PRIMARY KEY CLUSTERED
(
[PurchaseReturnId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[PurchaseReturn] WITH CHECK ADD CONSTRAINT [FK_PR_VEN] FOREIGN KEY([PURCHASEORDERID])
REFERENCES [dbo].[PurchaseOrder] ([PurchaseOrderId])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[PurchaseReturn] CHECK CONSTRAINT [FK_PR_VEN]
GO
这是PurhcaseOrder的查询
USE [AsifTraders]
GO
/****** Object: Table [dbo].[PurchaseOrder] Script Date: 24-Sep-14 11:56:53 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[PurchaseOrder](
[Date] [datetime] NULL,
[PurchaseOrderId] [nvarchar](8) NOT NULL,
[JEntryId] [int] NULL,
[VendorId] [nvarchar](8) NULL,
[PurchaseType] [nvarchar](10) NULL,
[Description] [nvarchar](50) NOT NULL,
[ItemQuantity] [numeric](18, 0) NOT NULL,
[DeleiveryCharges] [numeric](18, 0) NULL,
[Discount] [numeric](18, 0) NULL,
[Total] [numeric](18, 0) NOT NULL,
[Paid] [numeric](18, 0) NULL,
[PBalance] [numeric](18, 0) NULL,
[NBalance] [numeric](18, 0) NULL,
PRIMARY KEY CLUSTERED
(
[PurchaseOrderId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[PurchaseOrder] WITH CHECK ADD CONSTRAINT [fk_purch_ven] FOREIGN KEY([VendorId])
REFERENCES [dbo].[Vendor] ([VendorId])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[PurchaseOrder] CHECK CONSTRAINT [fk_purch_ven]
GO