Assuming I have开发者_如何学Python a table as such
[StockBarcodeID] [uniqueidentifier] NOT NULL,
[UserID] [uniqueidentifier] NOT NULL,
[StockID] [uniqueidentifier] NOT NULL,
[UnitPrice] [money] NOT NULL,
[Barcode] [varchar](16) NOT NULL,
[IsDefault] [bit] NOT NULL,
[LastUpdated] [datetime] NOT NULL,
How would one go about creating a constraint that allows for only 1 Default barcode row per stockid?
I cant seem to get my head around this one. Is this a unique constraint or a check constraint?
Create a view which shows only the default barcodes. Then create a unique index on this view.
CREATE VIEW dbo.DefaultBarcode
WITH SCHEMABINDING
AS
SELECT StockID, Barcode
FROM dbo.Barcode
WHERE IsDefault = 1
GO
CREATE UNIQUE CLUSTERED INDEX UC_DefaultBarcode ON dbo.DefaultBarcode (StockID)
GO
精彩评论