0

Source Table Structures

I have two tables that I would like to combine. Two columns act as keys, and rest of fields are values or attributes.

TABLE A (Has both KeyA and KeyB) :
KEYA | KEYB | Val1 | Val2 | Val3 | ... | ValN
---------------------------------------------
keyA1| keyB1|  V11 |  V12 |  V13 | ... |  V1n 
keyA1| keyB2|  V21 |  V22 |  V23 | ... |  V2n
keyA1| keyB3|  V11 |  V12 |  V13 | ... |  V1n
              ....
keyAm| keyB1|  Vm1 |  Vm2 |  Vm3 | ... |  Vmn


Table B (only keyA present):
KEYA |  Str1  |   Str2 
----------------------
keyA1|  str11 |  str12 
keyA2|  str21 |  str22
        ....
keyAm|  strm1 |  strm2

Result Table Structure Needed

I would like to combine the two tables like so Str1 and Str2 from TABLEB are to be mapped to TABLEA using KeyA.

TABLE C (contains both KeyA and KeyB) :
KEYA | KEYB | Val1 | Val2 | Val3 | ... | ValN |     Str1 | Str2
------------------------------------------------------------------
keyA1| keyB1|  V11 |  V12 |  V13 | ... |  V1n |    str 11| str 12
keyA1| keyB2|  V21 |  V22 |  V23 | ... |  V2n |    str 11| str 12
keyA2| keyB3|  V11 |  V12 |  V13 | ... |  V1n |    str 21| str 22
              ....
keyAm| keyB1|  Vm1 |  Vm2 |  Vm3 | ... |  Vmn |    str m1| str m2

Issue

Sometimes, new value fields (Val n) get added to Table A, but schema for Table B remains constant. If I combine the tables like so -

create TABLEC as
select a.*, b.Str1 as Str1, b.Str2 as Str2 from
TABLEA a INNER JOIN TABLEB b ON a.keyA=b.keyA

The resultant table has field names as a.val1, a.val2 etc.


This is what I currently use

create TABLEC as
select a.Val1 as Val1,
a.Val2 as Val2,
  ...
a.Valn as Valn,    

b.Str1 as Str1,
b.Str2 as Str2
from TABLEA a INNER JOIN TABLEB b ON a.keyA=b.keyA

I have to modify code everytime an additional Val field is added. Is there a way to create TABLEC directly so that the field names from TABLEA and TABLEB are preserved (keyA would be the common field).

tdk
  • 342
  • 1
  • 13

1 Answers1

1

As @wildplasser alludes to in the comments, the fundamental issue is that you are "regularly" adding columns to table A. In a relational database, adding columns to an existing table should not be a "regular" operation. Okay, sometimes it is necessary, but that should be because of changes to the data model.

Having columns names "val1", "val2", and so on is suspicious. There are numerous methods for storing such data in a more SQLish way, some of which depend on the functionality of the underlying database:

  • Using a junction table.
  • Using an entity-attribute-value data model (or more likely, a hybrid).
  • Using JSON or XML columns to store lists.
  • Using arrays.
  • Using nested tables.

The proximal cause of your problem is based on the definition of the view. When a view is defined, the * is expanded to all columns. This expansion doesn't occur when the view is used. You can readily see this by looking at the columns of the view which is stored in the metadata.

Changes to the underlying table do not necessarily result in the view being reparsed (although changes to underlying tables could invalidate the view).

Some databases allow triggers on DDL (data definition language). If your database supports such triggers, you could actually have the database modify the view when a new column is added. Alternatively, you could wrap the DDL changes in a stored procedure and include the changes to the view when new columns are added.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786