且构网

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

我想为用户在java语言中注册信息时创建一个唯一的id。

更新时间:2023-10-01 23:44:16

由于您有数据库,因此很容易:使用IDENTITY列:

标识栏 - *** [ ^ ]

IDENTITY(属性)(Transact-SQL)| Microsoft Docs [ ^ ]

这样,数据库会维护该值并确保每个新行都有不同的数字。
As you have a database, it's easy: use an IDENTITY column:
Identity column - Wikipedia[^]
IDENTITY (Property) (Transact-SQL) | Microsoft Docs[^]
That way, the DB maintains the value and will ensure that each new row has a different number.


Quote:

我试图从数据库中获取行号以使它们成为id但问题是当我删除任何特定的用户细节时。所有现有ID都将同时更改。

I have tried to get row no from database to make them id but problem is that when i delete any particular user detail.All existing id will be changed simultaneously.



您刚刚发现了没人这样做的原因。



唯一的解决方案是拥有一个持有用户ID的字段。

在SQL中,您可以让服务器自动生成Id,并将Id作为主键,确保Id是唯一的。

否则,您需要在某处保存Id计数器。


You have just discovered the reason why nobody do that.

The only solution is to have a field holding the user Id.
In SQL, you can have the Id automatically generated by server, and making the Id a primary key ensure that Id will be unique.
Otherwise, you need to save an Id counter somewhere.