Advertisement
jdcrowe

normalizing stuff

Sep 14th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 2.95 KB | None | 0 0
  1.  
  2. /*all of this is assuming you are using manually created guids for (most) primary keys*/
  3.  
  4. /*denormalized table to represent your table*/
  5. declare @shit table ([Thing] varchar(1), [Type] varchar(10), [Details] varchar(10), [MoarDetails] varchar(10), [MultiRowDetails] varchar(10));
  6.  
  7. /*insert some shit*/
  8. insert @shit
  9.     values
  10.         ('a', 'small', 'wtf', 'wasdtwa', 'w111111')
  11.         , ('b', 'small', 'sucka', 'sewewe', 's111111')
  12.         , ('b', 'small', 'sucka', 'sewewe', 's222222')
  13.         , ('c', 'big', 'punk', 'poioisa', 'p111111')
  14.         , ('d', 'big', 'gucci', 'gang', 'g1111111');
  15.  
  16. select * from @shit;
  17.  
  18. /*create a table to hold your new guid and some identifying value from the original*/
  19. declare @shitid table ([ID] uniqueidentifier, [Thing] varchar(1));
  20.  
  21. /*insert some new shit, creating a guid per unique [Thing]*/
  22. insert @shitid
  23.     select newid(), [Thing]
  24.     from @shit
  25.     group by [Thing];
  26.  
  27. select * from @shitid
  28.  
  29.  
  30. /*new normalized tables*/
  31. declare @type table ([ID] uniqueidentifier, [Value] varchar(10));
  32. declare @thing table ([ID] uniqueidentifier primary key, [Thing] varchar(1), [TypeID] uniqueidentifier, [Details] varchar(10));
  33. declare @thing2 table ([ThingID] uniqueidentifier primary key, [MoarDetails] varchar(10)); /*primary key is foreign key to @thing*/
  34. declare @thingmulti table ([ID] int identity primary key, [ThingID] uniqueidentifier, [MultiRowDetails] varchar(10)); /*identity as primary key to prevent fuckery*/
  35. declare @thingmultiALT table ([ThingID] uniqueidentifier, [Order] int, [MultiRowDetails] varchar(10)); /*primary key is foreign key and order*/
  36.  
  37. /*create lookup shit*/
  38. insert @type
  39.     select newid(), [Type]
  40.     from @shit
  41.     group by [Type];
  42.  
  43. select * from @type;
  44.  
  45. /*create parent table*/
  46. insert @thing
  47.     select a.[ID], b.[Thing], c.[ID], b.[Details]
  48.     from @shitid a
  49.     inner join @shit b on a.[Thing] = b.[Thing]
  50.     inner join @type c on b.[Type] = c.[Value]
  51.     group by a.[ID], b.[Thing], c.[ID], b.[Details];
  52.  
  53. select * from @thing;
  54.  
  55. /*create 1:1 child table*/
  56. insert @thing2
  57.     select a.[ID], b.[MoarDetails]
  58.     from @shitid a
  59.     inner join @shit b on a.[Thing] = b.[Thing]
  60.     group by a.[ID], b.[MoarDetails];
  61.  
  62. select * from @thing2;
  63.  
  64. /*create 1:M child table*/
  65. insert @thingmulti
  66.     select a.[ID], b.[MultiRowDetails]
  67.     from @shitid a
  68.     inner join @shit b on a.[Thing] = b.[Thing]
  69.     group by a.[ID], b.[MultiRowDetails];
  70.  
  71. select * from @thingmulti;
  72.  
  73. /*create 1:M child table with order*/
  74. insert @thingmultiALT
  75.     select a.[ID], row_number() over(partition by a.[ID] order by b.[MultiRowDetails]), b.[MultiRowDetails]
  76.     from @shitid a
  77.     inner join @shit b on a.[Thing] = b.[Thing]
  78.     group by a.[ID], b.[MultiRowDetails];
  79.  
  80. select * from @thingmultiALT;
  81.  
  82. /*join it back together*/
  83. select a.[ID], a.[Thing], d.[Value] [Type], a.[Details], b.[MoarDetails], c.[MultiRowDetails]
  84. from @thing a
  85. inner join @thing2 b on a.[ID] = b.[ThingID]
  86. inner join @thingmulti c on a.[ID] = c.[ThingID]
  87. inner join @type d on a.[TypeID] = d.[ID]
  88. order by a.[Thing];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement