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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:47:51+00:00 2026-06-13T08:47:51+00:00

Please consider the following SQL. I’m trying to create a table that references another.

  • 0

Please consider the following SQL. I’m trying to create a table that references another. The foreign key isn’t being enforced and I don’t know why.

--drop database test;
create database test;
use test;

create table person (
    id         int         auto_increment,
    name         varchar(30),
    primary key (id)
);

create table message (
    id         int         auto_increment,
    senderid    int,
    receiverid    int,
    primary key (id),
    foreign key (senderid) references person(id),
    foreign key (receiverid) references person(id)
);

If I do a SHOW CREATE TABLE message, I get the following:

CREATE TABLE `message` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `senderid` int(11) DEFAULT NULL,
 `receiverid` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `senderid` (`senderid`),
  KEY `receiverid` (`receiverid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 

Note that the foreign keys senderid/receiverid are just themselves. Without adding any rows into person:

mysql> insert into message (senderid, receiverid) values (1,2);
Query OK, 1 row affected (0.00 sec)

mysql> select * from person;
Empty set (0.00 sec)

mysql> select * from message;
+----+----------+------------+
| id | senderid | receiverid |
+----+----------+------------+
|  1 |        1 |          2 |
+----+----------+------------+
1 row in set (0.00 sec)

Why isn’t my foreign key working?

  • 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-13T08:47:52+00:00Added an answer on June 13, 2026 at 8:47 am

    You are using MyISAM but, as documented under Foreign Key Differences:

    For storage engines other than InnoDB, MySQL Server parses the FOREIGN KEY syntax in CREATE TABLE statements, but does not use or store it.

    Suggest you use InnoDB instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please consider the following SQL Server table and stored procedure. create table customers(cusnum int,
Please consider following schema CREATE table articles ( id Int UNSIGNED NOT NULL AUTO_INCREMENT,
Please consider the following tweets table: tweet_id user_id text ---------------------------- 1 1 lorem ipsum
Please consider following sample table structure: +-------------------------------+--------------+------+-----+---------+-------+ | Field | Type | Null |
I have an app that's about presenting fictional simplified cities. Please consider the following
Please consider following example: The source image consists of 6 areas that need to
Please consider the following code. I'm trying to output a vector of vectors to
Please Consider the following table : As you can see the Name column has
Please consider following tables. Table: Document docID, docNr, docScanTime 10, 99, 2012-08-02 11, 98,
I am using SQL Server 2005. Please consider the following query: DECLARE @SearchTerm NVARCHAR(4)

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.