SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: FK that comes from two different tables?

  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2011
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    FK that comes from two different tables?

    I was wondering if there is anyway to have a foreign key that comes from two different tables. Why you ask?

    I have a table "cast" with a primary key of ID and a foreign key of user_id.

    In my database I have two different types of users (registered and unregistered) I would like to continue to keep them separate if possible.

    So I want to know if there is anyway to say that username is a foreign key for either registered.user_id or unresistered_user_id? The user_id for users and unregistered users have different numbering so they will never overlap.

    Code:
    CREATE TABLE `cast` (
      `cast_id` int(11) NOT NULL AUTO_INCREMENT,
      `cast_user_id` int(11) DEFAULT NULL,
      `cast_video_id` int(11) DEFAULT NULL,
      `cast_proj_id` int(11) DEFAULT NULL,
      PRIMARY KEY (`cast_id`),
      FOREIGN KEY(cast_user_id) REFERENCES users(user_id) 
      FOREIGN KEY(cast_user_id) REFERENCES unreg_users(unreg_user_id)
    )

  2. #2
    SQL Consultant silver trophybronze trophy
    SitePoint Award Recipient r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    38,463
    Mentioned
    35 Post(s)
    Tagged
    1 Thread(s)
    keeping registered and unregistered users separate should not be done with separate tables

    rather, you should have only one users table, and a column that indicates status (registered/unregistered)

    then your FK question disappears

    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

Bookmarks

Posting Permissions

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