2014-07-18 93 views
0

我遇到週期問題。我正在爲IT部門開發庫存系統。 我有3個表的問題。SQL循環或多級聯路徑

INVENTORY表用於保留用戶操作的歷史記錄,SOFTWAREHARDWARE表用於存儲庫存。

我想保持一個inventoryID這是SOFTWAREHARDWARE表獨特的,當我使用ON CASCADE DELETE我得到一個錯誤:

Msg 1785, Level 16, State 0, Line 1
Introducing FOREIGN KEY constraint 'fk_inventory_inventoryIDhw' on table 'inventory' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

這裏是我的表。任何人都可以幫我嗎?如何解決這個問題。無法在網絡上找到正確的解決方案。謝謝

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 
+1

[不良習慣踢:聲明VARCHAR沒有(長度)](http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/09/bad-habits-to-kick-declaring -varchar-without-length.aspx) - 你應該總是**爲任何'varchar'列(例如'description')提供一個長度,你使用的變量和參數 –

+1

你的外鍵關係都是錯的 - 他們應該從'Software'和'Hardware'轉到'Inventory' - 而不是相反!你需要在'Software'和'Hardware'中有一個'InventoryId(FK)' - 但是你絕對不需要FK約束到'Inventory'中的那些「子」表!這就是導致'ON DELETE CASCADE'設置中的循環的原因! –

+0

@marc_s感謝您的評論。我會給nvarchar添加長度。關於PK和FK。而不是在軟件和硬件表中有庫存PK,我需要將它們設置爲FK,庫存表中它應該是PK?我有點困惑。 – Roman

回答

相關問題