如何克隆一个带有guid的表以及引用这些guid作为外键的另一个表。克隆表及其外键
这两个表具有一对多的关系。我需要以自动的方式做到这一点。
我的问题是能够在克隆过程中创建新创建的GUID时引用两个表之间的关系。
编辑:我需要将条目克隆到表本身,而不是新表。
如何克隆一个带有guid的表以及引用这些guid作为外键的另一个表。克隆表及其外键
这两个表具有一对多的关系。我需要以自动的方式做到这一点。
我的问题是能够在克隆过程中创建新创建的GUID时引用两个表之间的关系。
编辑:我需要将条目克隆到表本身,而不是新表。
基本上,我们希望用新的ID复制表格,但保持与原件相同的关系。 如果需要,你可以保持相同的ID,但实际上这不应该是一个要求;为了测试,Ids应该不重要 - 只有关系。
我会用两个表说明这一点:
首先是AnimalType与ID(唯一标识符 - ROWGUID,主键)和AnimalType(NVARCHAR)列
二是动物与AnimalName(为nvarchar)和AnimalType(唯一标识符,外键)列
父/查找表:
创建一个新表(newTable)以填充现有表(oldTable)的数据。
有自己的主键Id列(ROWGUID,身份等),其默认创建newtable的
创建newtable的一个额外的列来保存oldTable的ID列的值复制
的ID列在newtable中会产生对创造的记录
唯一ID第二(子)表:
创建一个新的表(newChildTable)与填充现有表(oldChildTable)的数据。
有自己的外键列指向newtable中的主键列
创建newChildTable一个额外的列来保存oldChildTable的外键列的值复制
一旦创建创建newChildTable,我们填充新parent/lookup Table与原始表格中的数据进行比较,将Id值放入为此数据添加的额外列中。表格自己的ID将像往常一样独特生成。
接下来,我们使用原始表中的数据填充子表,将原始外键列值放入添加的此列数据列中。
接下来,我们加入保留原始Id值的列上的两个新表,并将外键列值更新为父/查找表中的新ID。
最后,我们可以删除拿着原来的ID值的列,我们只剩下链接到同一数据的两个表,但通过在记录被复制创建时,我们产生了新的ID。
你不会有原来的IDS任何参考 - 只需在您的测试在任何时候选择了错误的表的情况下(虽然这应该在不同的服务器来完成...)。如果您还需要原始Ids,则可以执行上述操作,不要移动Ids,重命名列等 - 如您所愿。
/*
Create copy of parent/lookup Table with its own Id column
Add a column to hold the original Ids
*/
CREATE TABLE [dbo].[AnimalTypeBak](
[Id] [uniqueidentifier] ROWGUIDCOL NOT NULL CONSTRAINT [DF_AnimalTypeBak_Id] DEFAULT (newid()),
[OriginalId] [uniqueidentifier] NOT NULL,
[AnimalType] [nvarchar](32) NOT NULL,
CONSTRAINT [PK_AnimalTypeBak] PRIMARY KEY CLUSTERED
(
[Id] ASC
)
) ON [PRIMARY]
GO
/*
Create copy of child Table
Add a column to hold the original Foreign Key values
*/
CREATE TABLE [dbo].[AnimalBak](
[AnimalName] [nvarchar](20) NOT NULL,
[OriginalAnimalType] [uniqueidentifier] NOT NULL,
[AnimalType] [uniqueidentifier] NOT NULL
) ON [PRIMARY]
GO
/*
Import data from the parent/lookup Table placing the origional Ids into the added Column
*/
INSERT INTO [dbo].[AnimalTypeBak]
([OriginalId]
,[AnimalType])
SELECT [Id], [AnimalType]
FROM [dbo].[AnimalType]
GO
/*
Import data from the child Table placing the origional Foreign Key values into the added Column
*/
INSERT INTO [dbo].[AnimalBak]
([OriginalAnimalType]
,[AnimalName])
SELECT [AnimalType], [AnimalName]
FROM [dbo].[Animal]
GO
/*
Update the child Table placing the new parent/lookup Ids into the Foreign Key Column
*/
UPDATE [dbo].[AnimalBak]
SET [dbo].[AnimalBak].[AnimalType] = [dbo].[AnimalTypeBak].[Id]
FROM [dbo].[AnimalBak]
INNER JOIN [dbo].[AnimalTypeBak]
ON [dbo].[AnimalBak].[OriginalAnimalType] = [dbo].[AnimalTypeBak].[OriginalId]
GO
/*
Drop the redundant Columns
*/
ALTER TABLE [dbo].[AnimalBak]
DROP COLUMN [OriginalAnimalType]
GO
ALTER TABLE [dbo].[AnimalTypeBak]
DROP COLUMN [OriginalId]
/*
Add the Foreign Key Contraint between the two Tables
*/
ALTER TABLE [dbo].[AnimalBak] WITH CHECK ADD CONSTRAINT [FK_AnimalBak_AnimalTypeBak] FOREIGN KEY([AnimalType])
REFERENCES [dbo].[AnimalTypeBak] ([Id])
GO
/*
And select the data to ensure the data is related as it was in the original Tables
*/
SELECT a.AnimalName, a.AnimalType, b.AnimalType FROM [dbo].[AnimalBak] as a INNER JOIN [dbo].[AnimalTypeBak] as b ON b.Id = a.AnimalType
declare @Parents table (Id uniqueidentifier, Name varchar(50));
declare @Children table (Id uniqueidentifier, ParentId uniqueidentifier, Name varchar(50));
declare @NewId uniqueidentifier = newid();
insert into @Parents values (@NewId, 'Original parent');
insert into @Children values (newid(), @NewId, 'First original child');
insert into @Children values (newid(), @NewId, 'Second original child');
declare @Ids table (CloneId uniqueidentifier, OriginalId uniqueidentifier);
merge @Parents as target
using (
select
CloneId = newid(),
OriginalId = Id,
Name = Name + ' (Cloned)'
from
@Parents
where
Id = @NewId
)
as source on source.CloneId = target.Id
when not matched by target then
insert (Id, Name)
values (source.CloneId, source.Name)
output
source.CloneId, source.OriginalId
into @Ids (CloneId, OriginalId);
merge @Children as target
using (
select
Id = newid(),
ParentId = ids.CloneId,
Name = Name + ' (Cloned)'
from
@Children c
inner join @Ids ids on ids.OriginalId = c.ParentId
)
as source on source.Id = target.Id
when not matched by target then
insert (Id, ParentId, Name)
values (source.Id, source.ParentId, source.Name);
select * from @Parents
select * from @Children
你有问题到底是什么?不确定你的RDBM,但在SQL服务器中,你可以简单地用关系来编写你的对象,做一个大容量拷贝,数据导入向导,编写对象和数据的脚本等 – Kritner
@Kritner我并不是试图复制表本身,而是复制表中的条目以获取测试数据。 – useruseruser
这是什么关系型数据库?如果您使用SQL Server使用SQL Server – Kritner