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 9109621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:05:14+00:00 2026-06-17T03:05:14+00:00

ALTER TABLE Question ADD CONSTRAINT FK_SessionQuestion FOREIGN KEY (SessionId) REFERENCES Session (SessionId); SQL above

  • 0
ALTER TABLE Question ADD CONSTRAINT FK_SessionQuestion FOREIGN KEY
(SessionId) REFERENCES Session (SessionId);

SQL above gives me error below:

#1452 – Cannot add or update a child row: a foreign key constraint fails (mobile_app.#sql-567_20be3, CONSTRAINT FK_SessionQuestion
FOREIGN KEY (SessionId) REFERENCES Session (SessionId))

I want to include a foreign key in Question table to link up with SessionId but why won’t it let me and how can it be fixed?

Question table:

SessionId(PK) QuestionId(PK) QuestionContent
--------------------------------------------
1             1              What is 2+2
1             2              What is 3+3
2             1              What is 5+5
2             2              What is 4+7

Session table:

SessionId SessionName
---------------------
1         AAA
  • 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-06-17T03:05:15+00:00Added an answer on June 17, 2026 at 3:05 am

    The question table includes SessionIds that are not found in the session table (well, actually, just one: 2). The FOREIGN KEY constraint, however, requires every SessionId used in the question table to exist in the session table by its definition.

    Either insert all missing sessions, or remove all questions that reference sessions that don’t exist.


    To get a list of all SessionIds you’re missing in the table Session, you can use a LEFT JOIN:

    SELECT DISTINCT Question.SessionId
    FROM Question
         LEFT JOIN Session
           ON Question.SessionId = Session.SessionId
    WHERE Session.SessionId IS NULL;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

mysql> ALTER TABLE category ADD CONSTRAINT category_parent_category_id FOREIGN KEY (parent) REFERENCES category(id); ERROR 1005
I have this sql: ALTER TABLE dbo.ChannelPlayerSkins DROP CONSTRAINT FK_ChannelPlayerSkins_Channels but apparently, on some
When you create a foreign key constraint in a table and you create the
I need to add a foreign key field to an existing django model/postgres table.
Possible Duplicate: Error appears in sql when trying to add multiple foreign keys I
This is a trivial question, I added a field Alter table MyTbl add datecreated
I created a foreign key constraint between two of my SQL tables. Then I
I'm trying to do ALTER table tbl_name Engine=MyISAM; and I get ERROR 1217 (23000):
I have a computed column created with the following line: alter table tbPedidos add
I have a general question regarding table indices on foreign keys in database modeling.

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.