in

System.Data.SQLite

An open source ADO.NET provider for the SQLite database engine

Compound Foreign Key in Designer

Last post 07-27-2009 7:11 AM by alanetaylor. 3 replies.
Page 1 of 1 (4 items)
Sort Posts: Previous Next
  • 07-23-2009 12:40 AM

    • alanetaylor
    • Not Ranked
    • Joined on 07-23-2009
    • Keele, Staffordshire, England
    • Posts 2

    Compound Foreign Key in Designer

    I have a Master table:

    Code       nvarchar(50)
    FuelType nvarchar(50)
    <...other columns...>

    and a Child table:

    Id            int
    Code       nvarchar(50)
    FuelType nvarchar(50)
    <...other columns...>

    I need to define a foreign key from the Code and FuelType columns in the child table to the same columns in the master table.

    The designer only appears to allow a single column to be selected when defining a foreign key.

    Is there any way to get this to work?

    Alan E Taylor
  • 07-24-2009 6:24 PM In reply to

    Re: Compound Foreign Key in Designer

    This is on my todo list.  Currently the only way to get this to work is to not use the designer for it, but to write the CREATE table statement yourself.

     

  • 07-24-2009 9:30 PM In reply to

    Re: Compound Foreign Key in Designer

    Actually I lied.  You can do this in the designer currently ... all you have to do is add multiple foreign keys, and give them the same name ... for example, to support this schema:

    CREATE TABLE accounts (
       acc_num INTEGER,
       acc_type INTEGER,
       acc_descr CHAR(20),
       PRIMARY KEY (acc_num, acc_type))

    CREATE TABLE sub_accounts (
       sub_acc INTEGER PRIMARY KEY,
       ref_num INTEGER NOT NULL,
       ref_type INTEGER NOT NULL,
       sub_descr CHAR(20),
       FOREIGN KEY (ref_num, ref_type) REFERENCES accounts
          (acc_num, acc_type))

    ... When creating the foreign keys in sub_accounts, it'd look like this in the designer:

    Foreign Key 1

    Foreign Key 2

    I realize this looks rather counter-intuitive ... that's something I'll have to work on in the future.

     

  • 07-27-2009 7:11 AM In reply to

    • alanetaylor
    • Not Ranked
    • Joined on 07-23-2009
    • Keele, Staffordshire, England
    • Posts 2

    Re: Compound Foreign Key in Designer

    Thanks Robert,

     I'll give this a try for now, but I'll look forward to a fix that won't leave me scratching my head when I come to maintain it in six month's time ;o)

    Alan E Taylor
Page 1 of 1 (4 items)
Powered by Community Server (Commercial Edition), by Telligent Systems