Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3336270
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:06:55+00:00 2026-05-18T00:06:55+00:00

Sorry if this is already answered somewhere, but how do i create a foreign

  • 0

Sorry if this is already answered somewhere, but how do i create a foreign key that is not always present.

Example: I have a table called invoice_payments with the INT columns ‘type’ and ‘cc_gateway’. I also have a 2nd table called invoice_cc_gateway. Since I can accept multiple payment types (cash, cc payments online) i store the type of payment in type (0=cash, 1=cc gateway), and when it is a payment from the gateway, I want to link to that specific item.

Now my problem is, I know how to link the two tables together if the key always exists, but what if the key won’t always be present. If we have a cash payment, I set cc_gateway to 0, since it does not relate.

In the past I just created a dummy row for 0 inside cc_gateway so that my keys always match, but there must be a more correct way to do this.

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T00:06:56+00:00Added an answer on May 18, 2026 at 12:06 am

    Rephrased answer

    Sorry, I was a bit quick before, of course you can have a foreign key in the Gateway-table that references the Items paid via gateway, but the opposite can not be true (ie a key in the Item table linking to the Gateway table)

    If the key will not always be present it cannot be a foreign key constraint (references ), but instead you will have to join the gateway table when that value is present.

    Example

    select * from invoice_payments inv
    left join invoice_cc_gateway gw
    on inv.paymentId = gw.paymentId
    where paymentType = 1
    union
    select * from invoice_payments
    where paymentType = 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm sorry if this was already answered, but it's kind of difficult to search
Sorry if this has already been answered, but I can't find it if so.
Sorry if this question has already been answered but I can't find an answer.
I'm sorry if this question has already been answered but I couldn't find it.
Sorry if this question is answered already, but I didn't find a suitable answer.
Sorry for this not being a real question, but Sometime back i remember seeing
Sorry if this question has already been answered, and sorry if it's too subjective
Ok sorry this might seem like a dumb question but I cannot figure this
Sorry if this sounds like a really stupid question, but I need to make
Sorry if this is a little off-topic for regular stackoverflow questions, but we're tearing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.