Results 1 to 4 of 4

Thread: [SQL 2000]Composite key (combination of PK from 2 tables) but allow null in the 2nd c

  1. #1

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Arrow [SQL 2000]Composite key (combination of PK from 2 tables) but allow null in the 2nd c

    To illustrate it better here is a sample data:

    Column1 Column2
    R1 NULL
    R2 NULL
    R2 X1
    R2 X2
    R3 NULL


    As you can see the combined columns is unique, that is the requirement, is there a workaround that I can make a Composite Key out of those two tables?

    Another thing, those two columns are PK from other tables, with the first column there is no problem cascading updates, but not so with the 2nd column, aside from a trigger is there any other trick that will let me cascade update the other tables using the reference from the 2nd column?

    TIA
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [SQL 2000]Composite key (combination of PK from 2 tables) but allow null in the 2

    I'm fairly sure what you're trying to do is not possible. I think you'd have to add an extra identity column to act as PK for the table and then make your two FK columns a unique key for the table, which I believe will allow nulls.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [SQL 2000]Composite key (combination of PK from 2 tables) but allow null in the 2

    PKs can not contain NULL values, so Jmcilhinney is correct what you want to do is not possible. Not leaving a null in the second column of the key.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [SQL 2000]Composite key (combination of PK from 2 tables) but allow null in the 2

    Here's an example of what I was talking about. Note that I set "Enforce Foreign Key Constraint" to False for the relationship between Table2 and Table3 in order to allow NULLs in the Table2ID column.
    Attached Images Attached Images  
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width