开发者

Constraint for bit value in TSQL

开发者 https://www.devze.com 2023-01-25 11:36 出处:网络
Assuming I have开发者_如何学Python a table as such [StockBarcodeID] [uniqueidentifier] NOT NULL,

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
0

精彩评论

暂无评论...
验证码 换一张
取 消