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

  • Home
  • SEARCH
  • 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 8082663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:11:32+00:00 2026-06-05T17:11:32+00:00

Usually I worked with PostgreSQL and never had a problem, but now I need

  • 0

Usually I worked with PostgreSQL and never had a problem, but now I need to work with MySQL and I experiencing some troubles.
I am creating a table in database user_main_db. There is some problem with Foreign keys, phpMyAdmin always gives me next error:

1005 – Can’t create table ‘user_main_db.user_main’ (errno: 150) (

engine=InnoDB&page=Status&token=136fdbb820093986c79180b24adb304d”>Details…)

This is the table I am trying to create in database user_main_db:

CREATE TABLE user_main
(
uid INTEGER,
fn VARCHAR(40) NOT NULL,
ava VARCHAR(255),
gender tinyint(1) NOT NULL,
dob DATE NOT NULL,
country_id INTEGER, 
PRIMARY KEY (uid),
FOREIGN KEY ( uid ) REFERENCES user_basic_db.user_basic(uid) ON DELETE CASCADE ON UPDATE            CASCADE,
FOREIGN KEY ( country_id ) REFERENCES countries_db.countries(country_id) ON DELETE CASCADE   ON UPDATE CASCADE
);

This is table user_basic in database user_basic_db:

CREATE TABLE user_basic
(
uid INTEGER AUTO_INCREMENT,
email VARCHAR(40) NOT NULL,
password VARCHAR(40) NOT NULL,
PRIMARY KEY (uid)
);

This is table countries in database countries_db:

 CREATE TABLE countries(
    country_id INTEGER PRIMARY KEY AUTO_INCREMENT,
    country VARCHAR(40) NOT NULL UNIQUE
    )

The strange thing is when I define the table as follows:

CREATE TABLE user_main
(
uid INTEGER PRIMARY KEY REFERENCES user_basic_db.user_basic(uid)  ON DELETE CASCADE ON    UPDATE CASCADE,
 VARCHAR(40) NOT NULL,
ava VARCHAR(255),
gender tinyint(1) NOT NULL,
dob DATE NOT NULL,
country_id INTEGER REFERENCES countries_db.countries(country_id) ON DELETE CASCADE ON  UPDATE CASCADE
);

the query runs successfully but FOREIGN KEYS are not created, when I open ‘relational view’ there are no references to uid and country_id and I can make insert to user_main(country_id) even if there is no country with such id.
Please help someone. Thank you.

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

    Check out the MySQL manual about foreign key constrains:

    If you re-create a table that was dropped, it must have a definition
    that conforms to the foreign key constraints referencing it. It must
    have the right column names and types, and it must have indexes on the
    referenced keys, as stated earlier. If these are not satisfied, MySQL
    returns error number 1005 and refers to error 150 in the error
    message.

    Sounds familiar, doesn’t it!?

    Better drop the tables and create it new with a well formed syntax, something like this:

    CREATE TABLE `countries` (
      `country_id` int(11) NOT NULL AUTO_INCREMENT,
      `country` varchar(40) NOT NULL,
      PRIMARY KEY (`country_id`),
      UNIQUE KEY `country` (`country`)
    ) ENGINE=InnoDB;
    
    CREATE TABLE `user_basic` (
      `uid` int(11) NOT NULL AUTO_INCREMENT,
      `email` varchar(40) NOT NULL,
      `password` varchar(40) NOT NULL,
      PRIMARY KEY (`uid`)
    ) ENGINE=InnoDB;
    
    CREATE TABLE `user_main` (
      `uid` int(11) NOT NULL,
      `fn` varchar(40) NOT NULL,
      `ava` varchar(255) DEFAULT NULL,
      `gender` tinyint(1) NOT NULL,
      `dob` date NOT NULL,
      `country_id` int(11) NOT NULL,
      PRIMARY KEY (`uid`),
      KEY `user_main_ibfk_2` (`country_id`),
      CONSTRAINT testc1
      FOREIGN KEY (`country_id`) REFERENCES `countries` (`country_id`) 
        ON DELETE CASCADE ON UPDATE CASCADE,
      CONSTRAINT testc2
      FOREIGN KEY (`uid`) REFERENCES `user_basic` (`uid`) 
        ON DELETE CASCADE ON UPDATE CASCADE
    ) ENGINE=InnoDB;
    

    This should do the job.

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

Sidebar

Related Questions

I worked with libevent2 for some time, but usually I used it to handle
Usually, I try to set my folders to have permission 775, but some web-hosting
I usually use sql parameters with queries, but in this case I need to
I've worked with many APIs and it's never usually an easy task. Messing about
I usually write C code in C89, now some features of C99 (like intxx_t
I have encountered a problem that I have never seen before. Usually, when I
Sorry for what seems like a silly question: But I've never, ever worked with
I usually work on my Master branch, and push to Production. I accidentally worked
I need to retrieve a web content. I usually use wget but it's giving
the development.log in my application has never worked since the beginning of creating the

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.