简体   繁体   English

使用Guids维护数据库中的键与使用整数的另一个键之间的数据库关系

[英]Maintain Database Relationships Between Keys in a Database with Guids and Another One with Integers

I'm trying to write an application that will take the information out of a SQL Server database, load up the object models via a webservice, and then pass that information into an Access DB, which is based on a different schema than the SQL Server database, but the information contained is the same (so I assume this would be akin to an ETL process). 我正在尝试编写一个应用程序,它将从SQL Server数据库中获取信息,通过Web服务加载对象模型,然后将该信息传递到Access数据库,该数据库基于与SQL Server不同的模式数据库,但包含的信息是相同的(所以我假设这将类似于ETL过程)。 The problem that I'm running into is that the Primary Keys in the database I'm pulling the information (SQL Server) are GUIDs whereas the database I'm putting them into (Access) are INTs. 我遇到的问题是我正在提取信息的数据库中的主键(SQL Server)是GUID,而我将它们放入(Access)的数据库是INT。 So for example: 例如:

Table 1, Persons Table in SQL Server: 表1,SQL Server中的人员表:

╔══════════════════════════════════════╦══════════╦════════════╗
║                  ID                  ║   Name   ║ CreatedOn  ║
╠══════════════════════════════════════╬══════════╬════════════╣
║ 3648F6BB-F743-4952-9C69-93336667F3B1 ║ Person 1 ║ 01/01/2012 ║
║ DE44577A-CAE7-4101-B962-C052214E723B ║ Person 2 ║ 02/01/2012 ║
║ 94115C5E-9C7E-40CF-8A87-D4D837BD5DB3 ║ Person 3 ║ 03/01/2012 ║
║ F93E77D9-C344-420C-9499-BA3B4DD0F0BA ║ Person 4 ║ 04/01/2012 ║
╚══════════════════════════════════════╩══════════╩════════════╝

Table 2, Persons Table in Access: 表2,访问中的人员表:

╔════╦══════════╦════════════╗
║ ID ║   Name   ║ CreatedOn  ║
╠════╬══════════╬════════════╣
║  1 ║ Person 1 ║ 01/01/2012 ║
║  2 ║ Person 2 ║ 02/01/2012 ║
║  3 ║ Person 3 ║ 03/01/2012 ║
║  4 ║ Person 4 ║ 04/01/2012 ║
╚════╩══════════╩════════════╝

So Table 1 is how the data is returned in the SQL Server database, and Table 2 is how the information should appear in the Access database. 因此,表1是如何在SQL Server数据库中返回数据,表2是信息应如何出现在Access数据库中。 So all GUIDs should be integers, but the relationships between data should remain the same. 所以所有GUID都应该是整数,但数据之间的关系应保持不变。 So for example, if I run a query to get the person's address (the address table would also be setup similarly) in SQL Server and Access, the query should return the same results regardless of whether or not a GUID or integer is used. 因此,例如,如果我在SQL Server和Access中运行查询以获取人员的地址(地址表也将类似地设置),则无论是否使用GUID或整数,查询都应返回相同的结果。

What I was thinking was using ROW_NUMBER() in SQL Server, ordered by the CreatedOn date (which is a date time type in the database and as such, should be unique across all records): 我在想的是在SQL Server中使用ROW_NUMBER(),按CreatedOn日期排序(这是数据库中的日期时间类型,因此,在所有记录中应该是唯一的):

SELECT 
  (ROW_NUMBER() OVER (ORDER BY CreatedOn)) AS ID,
  Name,
  CreatedOn
FROM Table2;

The only thing is that I'm seeing duplicate integer IDs being returned from the query. 唯一的事情是我看到从查询返回重复的整数ID。 So for example Table 1 above would look like this: 因此,例如上面的表1将如下所示:

╔════╦══════════╦════════════╗
║ ID ║   Name   ║ CreatedOn  ║
╠════╬══════════╬════════════╣
║  1 ║ Person 1 ║ 01/01/2012 ║
║  2 ║ Person 2 ║ 02/01/2012 ║
║  1 ║ Person 3 ║ 03/01/2012 ║
║  1 ║ Person 4 ║ 04/01/2012 ║
╚════╩══════════╩════════════╝

When each ID should be unique. 每个ID应该是唯一的。 Can anyone think of a good way to go about doing what I'm trying to accomplish? 谁能想到一个好的方法去做我想要完成的事情? Is there anything wrong with the way I'm currently trying to do it? 我目前正在尝试这样做的方式有什么问题吗?

Any help would be greatly appreciated. 任何帮助将不胜感激。

I wouldn't rely on ROW_NUMBER() if I were you, because you probably can't be sure that the order is always the same: 如果我是你,我不会依赖ROW_NUMBER() ,因为你可能不能确定订单总是一样的:

First, in your example you show only date values (without time) for the CreatedOn column. 首先,在您的示例中,您只显示CreatedOn列的日期值(没有时间)。 If more than one person is created at the same day, you can't be sure which comes first if you order by that date. 如果在同一天创建了多个人,如果您按该日期订购,则无法确定哪个人首先出现。
Even if you actually have a time in the CreatedOn column as well - if a person is deleted from the table, the ROW_NUMBER of all subsequent persons will change. 即使您实际上在CreatedOn列中也有时间 - 如果从表中删除了某个人,则所有后续人员的ROW_NUMBER都将更改。


The easiest solution would be to change one of the tables, like already suggested by webturner in his answer . 最简单的解决方案是改变其中一个表,就像webturner在他的回答中已经建议的那样。
If you can't do this for any reason (for example if you aren't allowed to change the schema of either database at all, or if legacy stuff would break if you changed the schema of the tables), you can create a mapping table where you store the relation between the two tables: 如果由于任何原因无法执行此操作(例如,如果您根本不允许更改任一数据库的模式,或者如果更改了表的模式,旧的东西会中断),则可以创建映射用于存储两个表之间关系的表:

╔══════════════════════════════════════╦══════════╗
║             SqlServerID              ║ AccessID ║
╠══════════════════════════════════════╬══════════╣
║ 3648F6BB-F743-4952-9C69-93336667F3B1 ║    1     ║
║ DE44577A-CAE7-4101-B962-C052214E723B ║    2     ║
║ 94115C5E-9C7E-40CF-8A87-D4D837BD5DB3 ║    3     ║
║ F93E77D9-C344-420C-9499-BA3B4DD0F0BA ║    4     ║
╚══════════════════════════════════════╩══════════╝

You can even put it into a third database if you are not allowed to change the existing ones. 如果不允许更改现有数据库,您甚至可以将其放入第三个数据库。

The simplest solution is to change the schema at one end or the other so they are the same. 最简单的解决方案是在一端或另一端更改架构,使它们相同。 If you are adding records in both the Access and SQL ends then I'd use the GUID to prevent the same ID being added for two different records. 如果要在Access和SQL两端添加记录,那么我将使用GUID来防止为两个不同的记录添加相同的ID。 Then all you are doing is rolling your own 'replication' system. 然后,您所做的就是滚动自己的“复制”系统。 Both Access and SQL support GUIDs. Access和SQL都支持GUID。

Failing that you are going to require some sort of lookup for each table. 如果你没有要求对每个表进行某种查找。 This will give you the Integer equivalent for each GUID and vice versa. 这将为每个GUID提供Integer等效项,反之亦然。 If you add the GUID as an additional column in the Access table (Table 2) then you can use this as the lookup table. 如果将GUID作为附加列添加到Access表(表2)中,则可以将其用作查找表。

ROW_NUMBER() will be returning unique numbers, but will start again at 1 each time it is used so all the inserts for each table would need to be done in one set. ROW_NUMBER()将返回唯一的数字,但每次使用时将从1开始,因此每个表的所有插入都需要在一个集合中完成。 If you use an Autonumber field in access this will give you unique values across separate inserts. 如果在访问中使用自动编号字段,则会在不同的插入中为您提供唯一值。

Given that you now you have SQL's GUID and unique IDs in the Access table you just need to do a lookup whenever you insert into a table with a foreign key, such as the address table. 鉴于您现在在Access表中拥有SQL的GUID和唯一ID,只需在每次插入带有外键的表(例如地址表)时进行查找。 So when you are inserting addresses from SQL with GUIDs to identify the person Join access's Person table on the GUID and insert the Person's integer ID instead. 因此,当您使用GUID从SQL插入地址时,可以在GUID上标识加入访问的Person表,并插入Person的整数ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将有关密钥的数据从一个数据库传输到另一个数据库 - transfer data from one database to another regarding keys 将一个数据库复制到另一个数 - Copy one database to another database 使用密钥将 SQL 服务器中的一个数据库从一台服务器迁移到另一台服务器的最佳方法是什么? - What is the best way to migrate one database from one server to another in SQL Server with the keys? 一对一关系数据库关系的可能优化 - Possible optimizations of one-to-one relational database relationships 保持数据库一致性 - maintain Database consistency 知道SQL服务器中所有数据库表之间的关系类型吗? - Know the type of relationships between all the tables of database in SQL Server? 将Access数据库导入到SQL Server中以保留表之间的关系 - Import Access database into SQL Server preserving relationships between tables 了解 SQL Server 中所有数据库表之间的关系 - Know relationships between all the tables of database in SQL Server 将数据从一个数据库传输到另一个数据库 - Transfer data from one database to another database 寻找一种将表数据从一个 SQL 服务器数据库复制到另一个的方法(关于身份和外键) - Searching for a way to copy table data from one SQL Server database to another (concerning Identity and Foreign Keys)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM