简体   繁体   English

SQL将具有多列的表转换为两个表之间具有FK

[英]SQL transform table with multiple columns to two tables with FK between them

I need to convert my table with several fields to two other tables where one of the new tables has a row for each field in the first table in Microsoft SQL Server. 我需要将具有多个字段的表转换为另外两个表,其中一个新表在Microsoft SQL Server的第一个表中每个字段都有一行。

Table1(Table1Id, Field1, Field2, Field3)

for each row in Table1 create 为Table1中的每一行创建

Table2a(Table2aId)
Table2b(Table2bId, Table2aId, Field1)
Table2b(Table2bId, Table2aId, Field2)
Table2b(Table2bId, Table2aId, Field3)

Details 细节
I currently have the table 我目前有桌子

Table1 表格1

[dbo].[CommunityAssetTemplates]  
      ,[CommunityId]  
      ,[CommunityAssetTemplateId]  
      ,[BaseHouseSpecsAssetId]  
      ,[CommunityLogoAssetId]  
      ,[CommunityMarketingMapAssetId]  
      ,[CommunityPhotoAssetId]  
      ,[CommunityVideoDraftAssetId]  
      ,[CommunityVideoAssetId]  

This was mostly a quick way to fulfill a business need before we fully implemented the new feature where users can define multiple templates with different assets in them, so I made two new tables one is just to relate the second table to a Community 在我们完全实现新功能之前,这主要是满足业务需求的一种快速方法,在该功能中,用户可以定义其中包含不同资产的多个模板,因此我创建了两个新表,其中一个只是将第二个表与社区相关联。

Table2a Table2a

[dbo].[CommunityAssetDataTemplates]  
,[CommunityAssetDataTemplateId]    
,[CommunityAssetTemplateTypeId]  
,[CommunityId]  

Table2b Table2b

[dbo].[CommunityAssetTemplateFiles]  
,[CommunityAssetTemplateFileId]  
,[CommunityAssetDataTemplateId]  
,[CommunityAssetId]  

These two tables map together like so, each Table1 row creates 1 Table2a row and 6 Table2b rows 如此,这两个表映射在一起,每个Table1行创建1个Table2a行和6个Table2b行

Table2a Table2a

[CommunityAssetDataTemplateId] Auto Increments  
[CommunityAssetTemplateTypeId] = 1  
[CommunityId]  = Table1.CommunityId  

Table2b - 1 表2b-1

[CommunityAssetTemplateFileId]  Auto increments  
    ,[CommunityAssetDataTemplateId] = Table2a.[CommunityAssetDataTemplateId]  
    ,[CommunityAssetId] = Table1.[BaseHouseSpecsAssetId] (THIS CHANGES)  

Table2b - 2 表2b-2

[CommunityAssetTemplateFileId]  Auto increments  
    ,[CommunityAssetDataTemplateId] = Table2a.[CommunityAssetDataTemplateId]  
    ,[CommunityAssetId] = Table1.[CommunityLogoAssetId] (THIS CHANGES)  

continues for the remaining 4 'AssetId's fields of Table1 继续执行表1的其余4个'AssetId'字段

Here is one way to accomplish this using CROSS APPLY to separate Field1 , Field2 , and Field3 columns into rows: 这是使用CROSS APPLYField1Field2Field3列分隔为行的一种方法:

insert into Table2A (Table2Id)
select Table1Id from Table1

insert into Table2B(Table2Id, Field4)
select Table1Id, Field
from Table1
cross apply (values(Field1), (Field2), (Field3)) as ColumnsAsRows(Field)

Here is a sample: 这是一个示例:

declare @t1 table (Table1Id int identity(1,1), Field1 int, Field2 int, Field3 int)
declare @t2 table (Table2Id int primary key clustered)
declare @t3 table (Table3Id int identity(1,1) primary key clustered, Table2Id int, Field4 int)

insert into @t1 (Field1, Field2, Field3)
values (1, 2, 3), (4, 5, 6), (7, 8, 9)

select * from @t1

insert into @t2 (Table2Id)
select Table1Id from @t1

insert into @t3 (Table2Id, Field4)
select Table1Id, Field
from @t1
cross apply (values(Field1), (Field2), (Field3)) as ColumnsAsRows(Field)

select * from @t2
select * from @t3

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM