Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Why combine the 3 tables into one? Would be really bad db design. But here's a way to do it:</p> <p>The one table will have a column for each of the 3 tables' columns you want in the final table. I am making the assumption that <code>TableX_Hash</code> is the same type, so that remains as one unique column:</p> <pre><code>Table_All_in_One ( Table1_PK1, Table1_PK2, Table1_PK3, # space just for clarity of grouping Table2_PK1, Table2_PK2, Table3_PK1, Table3_PK2, Table3_PK3, Table3_PK4, Table3_PK5, TableX_Hash # Assuming all the _Hash'es are the same type+length, # otherwise, add Table1_Hash, Table2_Hash, Table3_Hash # This can be your new primary key ) </code></pre> <p>The Primary Keys (<code>PKx</code>) are required to be non-<code>NULL</code> only in their own tables. For this table, they have to allow nulls. <strong>The idea is that each row of this new table will only hold the data for one of the tables. The other columns will be empty for that row.</strong> If you want to associate the row of one table with another, you can add that to the same row or add <code>FK_Table1_Hash</code>, <code>FK_Table2_Hash</code> and <code>FK_Table3_Hash</code> columns which will refer to the <code>TableX_Hash</code> value of a record.</p> <p>PS: I wonder if what you are really looking for is a <code>View</code> and not this really bad all-in-one table.</p> <hr> <p>Edit: Combining them into one "without knowing how many primary keys a table can have." as per your comment:</p> <p>Store all the _PKs concatenated into one column:</p> <pre><code>Table_All_in_One ( New_PK, TableX_Hash, Table1_PKx, # Concatenated PKs of Table1 Table2_PKx, # Concatenated PKs of Table2, etc. ..., # OR just one TableX_PKs, # concatenate all the PK's into one VARCHAR field # Add a pipe `|` between them optionally. Table_Num # If using just one, then you'll need to store the table number </code></pre> <p>) You will not be able to conveniently pick records based on <em>part</em> of their composite primary key. It will always have to be <code>TableX_PKs = CONCAT_WS('|', Table1_PK1, Table1_PK2, ...)</code>. So your only dependency is the number of PKs in the original column.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload