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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:00:42+00:00 2026-06-14T17:00:42+00:00

Just wondering what happens to the foreign key constrains of a source table, when

  • 0

Just wondering what happens to the foreign key constrains of a source table, when it is replicated using CREATE...SELECT statement. Will the referential constraints be copied or ignored?

Here is an example:

CREATE TABLE foo(a int, b int, PRIMARY KEY(a));
CREATE TABLE source(id int, a int, PRIMARY KEY(id), FOREIGN KEY(a) REFERENCES foo(a));

CREATE TABLE target SELECT * FROM source;

So, my question is if attribute a on target also points to foo(a)? If the answer is vendor specific, I’m looking the answer for MySQL.

  • 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-14T17:00:44+00:00Added an answer on June 14, 2026 at 5:00 pm

    No, it won’t create the FK constraint in target. CREATE TABLE…SELECT just looks at the columns and data types of the SELECT result set, and that determines the definition of the new table.

    I thought that using CREATE TABLE…LIKE could be a workaround for you, but I tested it on MySQL 5.5, and it also does not create the foreign keys.

    CREATE TABLE target LIKE source;
    

    I’m guessing because foreign keys are implemented in the InnoDB storage engine, not recognized by the storage-independent layer of MySQL, they probably don’t show up in the storage-independent .FRM file.

    This bug seems to confirm that CREATE TABLE…LIKE is implemented by simply copying the .FRM file: http://bugs.mysql.com/bug.php?id=35526

    But the fix mentioned was put into the MySQL 6.1 branch, and that branch was killed long ago.

    So for now, you’ll have to declare the foreign keys as a separate step. I understand that in the future, the .FRM file is going away, so they’ll have to change how CREATE TABLE…LIKE works. Anyway, that doesn’t help you today.

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

Sidebar

Related Questions

just wondering when I had an iphone I was able to create a web
I'm just playing around with some PHP and was wondering what happens when an
I'm just wondering what happens in the case of a time zone changing, when
I was just wondering why this certain problem happens to me. If you can
Just wondering, if I statically create an object that has a pointer as a
Just wondering what actually happens after you exceed 2147483647 records?
I am just wondering what happens when a fork is done on github. For
Just say I had a query as below.. SELECT name,category,address,city,state FROM table WHERE MATCH(name,subcategory,category,tag1)
I was just wondering what happens if I was to load the same assembly
just wondering if there is a way to reduce the amount of code needed

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.