如何使字符串自动递增 [英] How to make string auto increment

查看:94
本文介绍了如何使字符串自动递增的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两张不同的桌子:

表 1

ID
AK001 
AK001 
AK001 

(上面应该是AK001、AK002、AK003等等....)

(above should be AK001, AK002, AK003 and so on....)

表 2

LastNum
 1

表1:更新table1 set ID = xx + RIGHT('....'+()) 依此类推

Table 1: update table1 set ID = xx + RIGHT('....'+()) and so on

Table 2: UPDATE table2 SET LastNum = LastNum + 1(这有效,但是当我在创建新表单时输入错误的细节时,还没有保存,它也会给我 +1)

Table 2: UPDATE table2 SET LastNum = LastNum + 1 (this works, but when I enter wrong detail when at create new form ,not yet save, its also give me +1)

我的问题是如何在表 2 更新后在表 1 中自动递增和更新?因为我面临的是 table1 在 table2 更新时保持给我相同的数字.请帮忙!

my question is how to auto increment and update in Table 1 after Table 2 have been updated? Because what im facing is the table1 keep give me the same number when table2 is updated. Please help!

推荐答案

您应该考虑使用 INT 作为主键而不是 varchar.然后将该字段用作外键.

You should consider using INT as primary key and not a varchar. Then use that field as foreign key.

这是关于为什么不将 VARCHAR 用作任何类型的键的详细答案.

Here is a detailed answer on why not to use VARCHAR as any type of key.

继续将你的存储 ID 存储为 varchar,只需在两个表上添加新字段,在主表上确保是 IDENTITY,它会自动递增.

Keep storing yout store ID as varchar, just add new field on both tables, on the main one make sure to be IDENTITY, it would auto-increment.

关于使用 VARCHAR 作为主键.你应该仔细分析你试图解决的问题.当然,也许你无法改变任何东西并使用你已经拥有的东西.在这种情况下,您需要按照@sgeddes 的建议使用一些变通方法作为触发器.

There are many opinions on the subject about using VARCHAR as primary key. You should carefully analize the problem you are trying to solve. And ofcourse, maybe you just can't change anything and work with what already you have. On that case you need to use some work around as a trigger as suggested by @sgeddes.

这篇关于如何使字符串自动递增的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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