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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:58:53+00:00 2026-06-17T12:58:53+00:00

Trying to explain the REFERENCES CONSTRAINT to someone else with a MySQL example, and

  • 0

Trying to explain the REFERENCES CONSTRAINT to someone else with a MySQL example, and I can’t make the pesky example work! It seems to work if I define it as a FOREIGN KEY, but I’ve textbooks that say I don’t have to do that (making it a column-level referential constraint rather than a table-level one).

The question is, after the UPDATE to dname in the dept table, why doesn’t the dname in the emp table change?

-- SQL CODE BEGINS
DROP TABLE IF EXISTS dept;
DROP TABLE IF EXISTS emp;

CREATE TABLE dept (
    dname CHAR(10),
    dnum  numeric(3,0)
) ENGINE=InnoDB;

CREATE TABLE emp (
  dname CHAR(10) REFERENCES dept(dname) ON UPDATE CASCADE,
  ename CHAR(10)
) ENGINE=InnoDB;

INSERT INTO dept VALUES ("AAA", 111);
INSERT INTO dept VALUES ("BBB", 222);

INSERT INTO emp VALUES ("CCC", "Carol"); 
INSERT INTO emp VALUES ("AAA", "Alice");

SELECT * from dept;
SELECT * from emp;
UPDATE dept SET dname="XYZ" WHERE dnum=111;
SELECT * from dept;
SELECT * from emp;

-- SQL CODE ENDS

ARGH!

  • 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-17T12:58:55+00:00Added an answer on June 17, 2026 at 12:58 pm

    excerpted from MySQL 5.5 Reference Manual

    <snip>

    InnoDB does not recognize or support “inline REFERENCES specifications” (as defined in the SQL standard) where the references are defined as part of the column specification. InnoDB accepts REFERENCES clauses only when specified as part of a separate FOREIGN KEY specification. For other storage engines, MySQL Server parses and ignores foreign key specifications.

    </snip>

    http://dev.mysql.com/doc/refman/5.5/en/create-table.html

    I think the authors of your two textbooks should have noted that their recommended approach, adding “REFERENCES” clause on the column definition, does NOT create or enforce foreign key constraints.


    In order to create a foreign key constraint, you need a unique index on the target column(s). In your case, you nee an index on dept(dname).

    ALTER TABLE dept ADD CONSTRAINT dept_UX 
      UNIQUE KEY (dname) ; 
    

    With that in place, you can then create a foreign key constraint:

    ALTER TABLE emp ADD CONSTRAINT FK_emp_dept 
      FOREIGN KEY (dname) REFERENCES dept(dname) ON UPDATE CASCADE ;
    

    (Normally, a foreign key references the PRIMARY KEY of a table. But a UNIQUE KEY is sufficient. Actually, with InnoDB, all that is required is an index, it doesn’t have to be unique, but we don’t want to go there.)

    To declare dname to be the PRIMARY KEY rather than a UNIQUE KEY:

    ALTER TABLE dept ADD PRIMARY KEY (dname) ;
    

    When the REFERENCES clause is added to the column definition, I believe that gets ignored by MySQL. (The syntax is accepted, but it doesn’t get recorded in the data dictionary. If you’re working with MyISAM it doesn’t matter, since MyISAM doesn’t enforce foreign key constraints.)

    To create foreign key constraints as part of the table definition, I put that on a separate line:

     CREATE TABLE emp (
     dname CHAR(10), 
     ename CHAR(10),
     CONSTRAINT FK_emp_dept FOREIGN KEY (dname) REFERENCES dept(dname)
     ) ENGINE=InnoDB ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am hoping that someone can explain the following behaviour. Suppose I am trying
Can someone explain the main benefits of different types of references in C#? Weak
I hope this I can explain what I am trying to achieve: I want
Can anyone explain this to me. I'm trying to inject a CSS file onto
May be I can't explain exactly in words what I am trying to achieve,
I'm beginner in Java, I'm trying to compile some small program, can somebody explain
I'm trying to make sure some data is auto-deleted when there's no more references
I am trying to explain to myself the forecasting result from applying an ARIMA
I have another programmer who I'm trying to explain why it is that a
I have legitimate reasons to do what I am trying to explain. I have

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.