Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL Information Schema - reading COMPOSITE foreign keys
    primarykey
    data
    text
    <p>I'm working on a <em>generic</em> application that uses a set of pre-selected database tables (and it's relationships) to generate a user interface for managing data in those tables. Basicaly, I'm just looking up stuff from the information schema, getting all the primary and foreign keys and generating input fields and links to other dialogs based on that. Because it's supposed to be a generic solution, it should work with any set of tables that do have a meaningful relationship among each other. </p> <p>One of the things I ran into are foreign keys, in particular - COMPOSITE foreign keys under one constraint. For example, if I have the following tables: </p> <p><strong>Company</strong></p> <pre><code>CompanyID, CompanyName, CompanyID --companyID is the primary key which identifies the company. </code></pre> <p><strong>Division</strong></p> <pre><code>DivisionID, CompanyID, DivisionName, DivisionID+CompanyID --DivisionID+CompanyID is a composite primary key for a division, because it's a one-to-many relationship and division is DEPENDENT on Company. </code></pre> <p><strong>Team</strong></p> <pre><code>TeamID, DivisionID, CompanyID, TeamName, TeamID+DivisionID+CompanyID --same as above - a Team is dependent on Division, which has a composite primary key. </code></pre> <p>Now, this database model is - by all definitions - a valid model (and SQL Server allows it) - but I've come across a certain problem. </p> <p>For example, in the information schema, both DivisionID and CompanyID are 'assigned' to the same CONSTRAINT. So, when I join the right tables, I get a problem. There's no way of knowing which column in one table is which column in the other. In the above example, the column names are the same (CompanyID is the name of every column that relates to the ID of the company - be it in the COMPANY table or the TEAM table, etc...) but, there's no rule that says the name of the columns must be the same, so I am at a loss on how to actually make the program know which column is which.</p> <pre><code>TABLE1 COLUMN1 CONSTRAINT TABLE2 COLUMN2 TEAM CompID TEAM_HAS_DIVISION DIVISION CompanyID TEAM DivID TEAM_HAS_DIVISION DIVISION DivisionID </code></pre> <p>Is there a way for the computer to know that CompID in Table TEAM references CompanyID, and not DivisionID in the DIVISION table? </p> <p>I couldn't find any way to do this from the data in INFORMATION_SCHEMA views. </p> <p>Yes, a human can easily figure out that CompID=CompanyID, etc... but as I mentioned previously - I'm trying to make a generic solution that wouldn't require a person to look at this and make decisions and mistakes :=) </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.
 

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