Вы не можете сделать это на столе, вам нужно использовать VIEW
и создать там индекс:
USE Sandbox;
GO
--Create sample tables
CREATE TABLE dbo.ChargeCarrier_Storage (ID_ChargeCarrier int,
ID_Storage int);
CREATE TABLE dbo.Storage (ID int,
ID_StorageType int);
GO
--Create Schema bound View
CREATE VIEW dbo.OnePickingStoragePerCarrier WITH SCHEMABINDING
AS
SELECT ccs.ID_ChargeCarrier,
s.ID AS ID_Storage
FROM dbo.ChargeCarrier_Storage ccs
INNER JOIN dbo.Storage s ON s.ID = ccs.ID_Storage
WHERE s.ID_StorageType = 1;
GO
--Create unique (clustered) index
CREATE UNIQUE CLUSTERED INDEX UQX_OnePickingStoragePerCarrier ON OnePickingStoragePerCarrier (ID_ChargeCarrier,ID_Storage);
GO
--Sample data in Storage
INSERT INTO dbo.Storage (ID,
ID_StorageType)
VALUES(1,1),
(2,1),
(3,2);
GO
--Tests
INSERT INTO dbo.ChargeCarrier_Storage (ID_ChargeCarrier,
ID_Storage)
VALUES(1,1); --Works
GO
INSERT INTO dbo.ChargeCarrier_Storage (ID_ChargeCarrier,
ID_Storage)
VALUES(2,1); --Works
GO
INSERT INTO dbo.ChargeCarrier_Storage (ID_ChargeCarrier,
ID_Storage)
VALUES(1,2); --Works
GO
INSERT INTO dbo.ChargeCarrier_Storage (ID_ChargeCarrier,
ID_Storage)
VALUES(2,1); --fails due to duplicate
GO
INSERT INTO dbo.ChargeCarrier_Storage (ID_ChargeCarrier,
ID_Storage)
VALUES(1,3); --works.
INSERT INTO dbo.ChargeCarrier_Storage (ID_ChargeCarrier,
ID_Storage)
VALUES(1,3); --works, as not Storage ID 1, so duplicate allowed
GO
GO
DROP VIEW dbo.OnePickingStoragePerCarrier;
DROP TABLE dbo.Storage;
DROP TABLE dbo.ChargeCarrier_Storage;