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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:25:40+00:00 2026-05-29T08:25:40+00:00

I want to create a table with Primary key referenced to Two columns of

  • 0

I want to create a table with Primary key referenced to Two columns of other table on MySql
Something like this

CREATE  TABLE IF NOT EXISTS `tarina`.`Geo_Distrito` (
  `departamento` INT(10) NOT NULL ,
  `provincia` INT(10) NOT NULL ,
  `codigo` INT(10) NOT NULL ,
  `nombre` VARCHAR(80) NULL ,
  `estado` INT(10) NULL ,
  PRIMARY KEY (`departamento`, `provincia`, `codigo`) ,
  CONSTRAINT `FK_ProvinciaDistrito`  FOREIGN KEY (`departamento` , `provincia` )  REFERENCES `Geo_Provincia` (`departamento` , `codigo` )
)ENGINE = InnoDB;

And got this error:

Can't create table 'tarina.Geo_Distrito' (errno: 150)

Can someone tell me what is the problem?

  • 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-05-29T08:25:41+00:00Added an answer on May 29, 2026 at 8:25 am

    Cannot create table. If the error message refers to error 150, table creation failed because a foreign key constraint was not correctly formed. If the error message refers to error –1, table creation probably failed because the table includes a column name that matched the name of an internal InnoDB table.

    This is due because of the the definition of your column has to be the same. In your case:

    the columns are not the same type, codigo int(10) unsigned NOT NULL and codigo INT(10) NOT NULL ,

    Also, you use the FK provincia which is also not the same type as codigo.

    Here’s the script:

    DROP TABLE IF EXISTS `Geo_Distrito`;
    DROP TABLE IF EXISTS `Geo_Provincia`;
    DROP TABLE IF EXISTS `Geo_Departamento`;
    
    CREATE TABLE `Geo_Departamento` (
    `codigo` int(10) unsigned NOT NULL COMMENT 'Codigo autogenerado',
    `nombre` varchar(80) NOT NULL COMMENT 'Nombre del departamento',
    `estado` int(10) unsigned NOT NULL COMMENT 'Estado de departamento',
    PRIMARY KEY (`codigo`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='Tabla de Departamentos';
    
    
    CREATE TABLE `Geo_Provincia` (
    `codigo` int(10) unsigned NOT NULL COMMENT 'Codigo autogenerado',
    `departamento` int(10) unsigned NOT NULL,
    `nombre` varchar(45) NOT NULL,
    `estado` int(10) unsigned NOT NULL,
    PRIMARY KEY (`departamento`,`codigo`) USING BTREE,
    CONSTRAINT `FK_ProvinciaDepartamento` FOREIGN KEY (`departamento`) REFERENCES `Geo_Departamento` (`codigo`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='Tabla de provincias del sistema.';
    
    CREATE  TABLE IF NOT EXISTS `Geo_Distrito` (
    `departamento` int(10) unsigned NOT NULL,
    `provincia` int(10) unsigned NOT NULL,
    `codigo` int(10) unsigned NOT NULL COMMENT 'Codigo autogenerado',
    `nombre` VARCHAR(80) NULL ,
    `estado` INT(10) NULL ,
    PRIMARY KEY (`departamento`, `provincia`, `codigo`) ,
    CONSTRAINT `FK_ProvinciaDistrito`  FOREIGN KEY (`departamento` , `provincia` )  REFERENCES `Geo_Provincia` (`departamento` , `codigo` )
    )ENGINE = InnoDB;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to do something like this: create table app_users ( app_user_id smallint(6) not
i want to create a table : products which looks like (these are columns
I have two already-existing tables which look (in part) roughly like this: CREATE TABLE
i have two tables CREATE TABLE IF NOT EXISTS `user` ( `user_id` int(20) NOT
I have two tables. Table user : CREATE TABLE IF NOT EXISTS `user` (
I have two tables like these: CREATE TABLE people ( id INT NOT NULL,
I have three tables like this CREATE TABLE `money`.`categories` ( `ID` bigint(20) unsigned NOT
I have a table defined like this: CREATE TABLE mytable (id INT NOT NULL
I want to create a table like myTable = { [0] = { [a]
I want to implement the following constraints in mysql: create table TypeMapping( ... constraint

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.