SQL循环或多个级联路径

时间:2014-07-18 18:12:27

标签: sql sql-server

我遇到了周期问题。我正在为IT部门开发库存系统。 我遇到了3张桌子的问题。

INVENTORY表用于保存用户操作的历史记录,SOFTWAREHARDWARE表用于存储广告资源。

我想保留一个inventoryIDSOFTWARE表格唯一的HARDWARE,当我使用ON CASCADE DELETE时,我收到错误:

  

Msg 1785,Level 16,State 0,Line 1
  在表'inventory'上引入FOREIGN KEY约束'fk_inventory_inventoryIDhw'可能会导致循环或多个级联路径。指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。

这是我的表格。任何人都可以帮我吗?如何解决问题。无法在网上找到正确的解决方案。感谢

CREATE TABLE inventory
( 
   statusID int,
   inventoryStatus nvarchar(15),
   inventoryID int,
   userName nvarchar(15),
   dates datetime2,
   inventoryAction nvarchar(10),
   categoryID int,

   CONSTRAINT pk_inventory_statusID PRIMARY KEY(statusID),
   CONSTRAINT fk_inventory_inventoryIDsw FOREIGN KEY(inventoryID) REFERENCES     software(inventoryID) ON DELETE CASCADE, 
   CONSTRAINT fk_inventory_inventoryIDhw FOREIGN KEY(inventoryID) REFERENCES hardware(inventoryID) ON DELETE CASCADE,
   CONSTRAINT fk_inventory_userName FOREIGN KEY(userName) REFERENCES users(userName) ON DELETE CASCADE
)
GO

CREATE TABLE hardware
(
   inventoryID int,
   hardwareID int,
   partNumber nvarchar(15),
   serial nvarchar(50),
   price float,
   supportID int,
   manufacturerID int,
   categoryID int,
   description nvarchar,
   deviceTypeID int,

   CONSTRAINT pk_hardware_inventoryID PRIMARY KEY(inventoryID),
   CONSTRAINT fk_hardware_categoryID FOREIGN KEY(categoryID) REFERENCES category(categoryID) ON DELETE CASCADE, 
   CONSTRAINT fk_hardware_supportID FOREIGN KEY(supportID) REFERENCES support(supportID) ON DELETE CASCADE,
   CONSTRAINT fk_hardware_deviceTypeID FOREIGN KEY(deviceTypeID) REFERENCES deviceType(deviceTypeID) ON DELETE CASCADE,
   CONSTRAINT fk_hardware_manufacturerID FOREIGN KEY(manufacturerID) REFERENCES manufacturer(manufacturerID) ON DELETE CASCADE,
)
GO

CREATE TABLE software
(
   inventoryID int,
   softwareID int,
   version nvarchar(10),
   name nvarchar(50),
   license nvarchar(50),
   price float,
   supportID int,
   categoryID int,
   manufacturerID int,
   programTypeID int,
   description nvarchar,

   CONSTRAINT pk_software_inventoryID PRIMARY KEY(inventoryID),
   CONSTRAINT fk_software_categoryID FOREIGN KEY(categoryID) REFERENCES category(categoryID) ON DELETE CASCADE, 
   CONSTRAINT fk_software_supportID FOREIGN KEY(supportID) REFERENCES support(supportID) ON DELETE CASCADE,
   CONSTRAINT fk_software_programTypeID FOREIGN KEY(programTypeID) REFERENCES     programType(programTypeID) ON DELETE CASCADE,
   CONSTRAINT fk_software_manufacturerID FOREIGN KEY(manufacturerID) REFERENCES     manufacturer(manufacturerID) ON DELETE CASCADE
)
GO

2 个答案:

答案 0 :(得分:0)

据我所知,与多个父表关联的同一列上的多个fk约束是不可行的。您可能需要另一个表来维护此关系。请参阅:http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=173589

答案 1 :(得分:0)

与多个父表关联的multipe Foriegn键约束的同一列具有创建参照完整性问题。

最好的方法是对设计进行规范化,并引入一个中间表以将该关系保留为2个(或更多)关系。