简体   繁体   English

如何创建两个与外键链接的表

[英]How to create two tables linked with foreign key

This is my problem: I´m working in a database named David , there I´m trying to create two tables: colores and productos which will be linked by a foreign key where the father table will be colores and the son table will be productos , but when I click continue this message appears: 这是我的问题:我正在一个名为David的数据库中工作,在那里我试图创建两个表: coloresproductos ,这两个表将通过外键链接,其中父表将为colores ,子表将为productos ,但是当我单击继续时,将显示以下消息:

errno: 150 "Foreign key constraint is incorrectly formed" errno:150“外键约束格式不正确”

someone could help me? 有人可以帮助我吗?

This is the code that I wrote: 这是我写的代码:

create table `color`(
    id_color int(3)not null AUTO_INCREMENT,
    nombre varchar(30)not null,
    PRIMARY KEY(id_color)
) ENGINE=INNODB;


create table`producto`(
    id_producto int(3)NOT null AUTO_INCREMENT,
    nombre varchar(30)not null,
    id_color int(3)not null,
    precio decimal(10) not null,
    PRIMARY KEY(id_producto),
    INDEX(id_color),
    FOREIGN KEY(id_color)
    REFERENCES`color`(id_color) on UPDATE CASCADE
) ENGINE= INNODB;

This is the code that I wrote. 这是我编写的代码。 在此处输入图片说明

This is the error that appears. 这是出现的错误。 在此处输入图片说明

The problem technically is just the missing space after the word references but the whole thing needs a few more spaces. 从技术上讲,问题仅是单词references后面的空格,但整个过程还需要更多空格。

create table `color`(
    id_color int(3) not null AUTO_INCREMENT,
    nombre varchar(30) not null,
    PRIMARY KEY(id_color)
) ENGINE=INNODB;

create table`producto`(
    id_producto int(3) NOT null AUTO_INCREMENT,
    nombre varchar(30) not null,
    id_color int(3) not null,
    precio decimal(10) not null,
    PRIMARY KEY(id_producto),
    INDEX(id_color),
    FOREIGN KEY(id_color)
    REFERENCES `color`(id_color) on UPDATE CASCADE
) ENGINE= INNODB;

Also, NOT NULL is not necessary on PK columns. 另外,PK列上不需要NOT NULL Just saying. 只是说。 It becomes automatic and redundant. 它成为自动和冗余的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM