mysql 中取最后一个值,获取数据库表中的最后一个值

本文探讨了如何在数据库中处理并发插入时避免文件代码重复的问题。作者提供了两种解决方案:使用自动增长的Identity列,以及创建计数器表来跟踪并分配唯一的文件代码。还介绍了第三种可能的用户体验较差的方法,即实时检查并更新文件代码。
摘要由CSDN通过智能技术生成

Hi all

Please Help me

I have the Following table

FileCode ... Name

125 .. A

126 .. B

127 .. C

and I have textbox and I want to fill it with the last value in table and increase by one like 128 .. 129 .. etc

I got it but when I try to insert in "FileCode" Column the textbox's value "128"

it got error because other one already inserted in database before me and took the number 128

how can I avoid these errors when two inserting in the same table at the same time

解决方案You have to ways of handling this...

Easiest is to let the database worry about it by using identity column (sequence) and enter it at the insert time not letting users see the number before insert is done.

Second one is harder as you have concurrency to worry about and you have to accept that you might have "holes" in your FileCode sequence

Have one table (say COUNTER) hold current max value of the FileCode

When you get current value, increase it by one immediately. Next person that comes for the number will get that increased number and will also increase it.

The holes come from users that don't finish the insert for whatever reason - they still "used up" the number.

There is third way and that is to show the user one number, but check if it exists before the insert and then change it "on the fly" and then refresh user view, but I think this is bad user experience.

Good luck!

Try like this:

1)Click[^]

2 )Create a trigger to update the FileCode while inserting Rows in Sql server

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值