In our retail system, we are planning to move basket information from the browser cookies to a table. We currently implement the basket limitations in the C# application but would like to move that business logic down to the database as constraints, if possible.
CREATE TABLE [dbo].[BasketProduct](
[BasketProductId] [int] IDENTITY(1,1) NOT NULL,
[BasketId] [int] NOT NULL,
[ProductId] [int] NOT NULL,
[Quantity] [int] NOT NULL,
CONSTRAINT [PK_BasketProduct] PRIMARY KEY CLUSTERED
([BasketProductId] ASC))
We want to restrict each baskets to have a maximum of 10 unique items but allow any quantities of each, so the BasketId value should only be present a maximum of 10 times in the table.
There doesn't seem to be an obvious constraint to do this as aggregates including window functions are not allowed in computed columns.
Can someone recommend an appropriate method?