跨多个 SQL 服务器的唯一 ID [英] Unique ID across multiple SQL servers

查看:42
本文介绍了跨多个 SQL 服务器的唯一 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一些将在全国多个实例中使用的软件.像许多使用登录的软件一样,我需要为每个用户提供一个唯一的 ID.软件的每个实例都需要完全独立运行,但最终合并几个数据库的可能性很高.在这种情况下,我希望每个用户的 ID 在所有服务器中都是唯一的.

I'm developing some software that will be used in multiple instances across the country. Like much software that uses logins, I need a unique ID for each user. Each instance of the software needs to operate completely independent, but the chances are high that eventually a few of the databases will be combined. In this case, I would like the ID for each user to be unique across all servers.

如果服务器之间没有通信(它们只为 LAN 提供服务),我认为从精确到毫秒的时间戳生成 ID 可能可行.由于用户池只有数千而不是数百万,因此在同一毫秒内创建一个用户与另一台服务器上的另一个用户的几率非常低.

Without communication between the servers (They only serve LANs), I've thought that maybe generating an ID from a timestamp accurate to milliseconds could work. With a userpool of only thousands and not millions, the odds of one user being created at the same millisecond as another user on another server are pretty low.

真的有什么方法可以保证所有服务器之间的唯一 ID 而无需它们之间的通信吗?

Is there really any way to guarantee a unique ID across all servers without communication between them?

推荐答案

使用 16 字节 uniqueidentifier 数据类型

Use the 16-byte uniqueidentifier data type

一个例子是

SELECT NEWID()
GO
-- This will return a new random uniqueidentifier e.g.
E75B92A3-3299-4407-A913-C5CA196B3CAB

在变量中选择此 Guid

To select this Guid in in a variable

--assign uniqueidentifier in a variable
DECLARE @EmployeeID uniqueidentifier
SET @EmployeeID = NEWID()
You can directly use this with INSERT statement to insert new row in table.

-- 在员工表中插入数据.

-- Inserting data in Employees table.

INSERT INTO Employees
(EmployeeID, Name, Phone)
VALUES
(NEWID(), 'John Kris', '99-99999')

示例来自这里,如果您想了解更多信息

examples were from here, if you want more info

这篇关于跨多个 SQL 服务器的唯一 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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