表设计为每个唯一列增加一列 [英] Table design increment a column for each unique column

查看:80
本文介绍了表设计为每个唯一列增加一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个Customer表

Say I have a Customer table

id  |  customerId
1       569
2       569
1       577 
1       555
2       555
3       555

在每个插入项上,我都希望id随customerId列增加1。我已经按如下所示准备了插入sp,但是我想知道是否存在一种使表表现出这种方式的本地方法(当创建具有任何约束的表时)。

On each insert I want id increment by one depend on the customerId column. I have prepared my insert sp as below, but I wonder if there is a native way to make my table behave like this (when creating table with any constraints).

ALTER dbo.ins_Customer
@CustomerId INT
AS 
BEGIN
   DECLARE @MaxSeqId INT
   SELECT @MaxSeqId = MAX(id) FROM dbo.Customer 
   WHERE customerId = @CustomerId 
   INSERT INTO dbo.Customer VALUES (@MaxSeqId+1, @CustomerId)
END

我希望这个简单的插入语句以任何可能的本机约束生成上面的结果。

I want this simple insert statement produce the result above with any possible, native constraints.

ALTER dbo.ins_Customer
@CustomerId INT
AS 
BEGIN
   INSERT INTO dbo.Customer VALUES (@CustomerId)
END


推荐答案


我想知道是否存在本机方法使我的表的行为像这样
(在创建具有任何约束的表时)。

I wonder if there is a native way to make my table behave like this (when creating table with any constraints).

不,没有。您将必须编写自定义代码以使表的行为像这样。

No, there is not. You will have to write custom code to make your table behave like this.

这篇关于表设计为每个唯一列增加一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆