且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使字符串自动递增

更新时间:2023-11-14 22:38:28

您应该考虑使用 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.