Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
977 views
in Technique[技术] by (71.8m points)

sql - How to make string auto increment

I got 2 different table:

Table1

ID
AK001 
AK001 
AK001 

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

Table2

LastNum
 1

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

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)

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...