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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:03:52+00:00 2026-06-11T10:03:52+00:00

I have a problem with create table statement in MySQL. The scenario is, I

  • 0

I have a problem with create table statement in MySQL.

The scenario is, I want to have a table P_CDP wchich stores some informations, and the table P_CDPFiles which stores the data of the uploaded files.

The column SelectedCDPFileID by default should be NULL unless the user selects some file. Then that column is filled by the file ID. But, I’m still getting the errno 150, why ? I think the FK names are set properly

CREATE TABLE `P_CDP` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `P_OrderID` int(11) NOT NULL DEFAULT '0',
  `SelectedCDPFileID` int(11) NULL,
  PRIMARY KEY (`ID`),
  KEY `FK_P_CDP_P_Orders` (`P_OrderID`),
  KEY `FK_P_CDP_P_CDPFiles` (`SelectedCDPFileID`),
  CONSTRAINT `FK_P_CDP_P_Orders` FOREIGN KEY (`P_OrderID`) REFERENCES `P_Orders` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `FK_P_CDP_P_CDPFiles` FOREIGN KEY (`SelectedCDPFileID`) REFERENCES `P_CDPFiles` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

CREATE TABLE `P_CDPFiles` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `FileID` int(11) NOT NULL DEFAULT '0',
  `P_CDPID` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`ID`),
  KEY `FK_P_CDPFiles_File` (`FileID`),
  KEY `FK_P_CDPFiles_P_CDP` (`P_CDPID`),
  CONSTRAINT `FK_P_CDPFiles_File` FOREIGN KEY (`FileID`) REFERENCES `File` (`FileID`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `FK_P_CDPFiles_P_CDP` FOREIGN KEY (`P_CDPID`) REFERENCES `P_CDP` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
  • 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-11T10:03:53+00:00Added an answer on June 11, 2026 at 10:03 am

    You’re attempting to reference a table that does not yet exist.

    If you must have references in both directions between these two tables (which generally indicates quite poor design), you will have to:

    1. Create the first table without any reference to the as-yet-undefined table:

      CREATE TABLE `P_CDP` (
        `ID` int(11) NOT NULL AUTO_INCREMENT,
        `P_OrderID` int(11) NOT NULL DEFAULT '0',
        `SelectedCDPFileID` int(11) NULL,
        PRIMARY KEY (`ID`),
        KEY `FK_P_CDP_P_Orders` (`P_OrderID`),
        KEY `FK_P_CDP_P_CDPFiles` (`SelectedCDPFileID`),
        CONSTRAINT `FK_P_CDP_P_Orders` FOREIGN KEY (`P_OrderID`)
          REFERENCES `P_Orders` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION
      ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
      
    2. Create the second table, including its reference to the first table:

      CREATE TABLE `P_CDPFiles` (
        `ID` int(11) NOT NULL AUTO_INCREMENT,
        `FileID` int(11) NOT NULL DEFAULT '0',
        `P_CDPID` int(11) NOT NULL DEFAULT '0',
        PRIMARY KEY (`ID`),
        KEY `FK_P_CDPFiles_File` (`FileID`),
        KEY `FK_P_CDPFiles_P_CDP` (`P_CDPID`),
        CONSTRAINT `FK_P_CDPFiles_File` FOREIGN KEY (`FileID`)
          REFERENCES `File` (`FileID`) ON DELETE NO ACTION ON UPDATE NO ACTION,
        CONSTRAINT `FK_P_CDPFiles_P_CDP` FOREIGN KEY (`P_CDPID`)
          REFERENCES `P_CDP` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION
      ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
      
    3. Alter the first table to add the intended constraint:

      ALTER TABLE `P_CDP`
        ADD CONSTRAINT `FK_P_CDP_P_CDPFiles` FOREIGN KEY (`SelectedCDPFileID`)
        REFERENCES `P_CDPFiles` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION;
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have got a problem with my SQL Statement: Table structure: CREATE TABLE tags
I have the following problem: In mysql I have a table which contains two
I have a table in a Mysql 5.1 schema. Statement for create this table
i'm having a problem with my database. I have the folowing: create table book
Suppose I have a myTest.sql scripts, which contains thousands of create table blahblah statements.
I have a large mysql table (about 5M rows) on which i frequently insert
I have a problem fetching values from a MySQL cursor. I create a temporary
I'm not that great at SQL statements, sorry, I have a statement: CREATE TABLE
I use the following sql statement in mysql: CREATE TABLE User( userId int NOT
I have a problem to create a preprocessor macro function, that concatenates two Strings

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.