mysql 1005 150,MySQL外键错误1005错误150

I'm doing a small DataBase with MySQL Workbench. I have a main table, called "Immobili", which has a Primary Key composed by four columns: (Comune, Via, Civico, Immobile).

Now, I also have three other tables, wich have the same primary key (Comune, Via, Civico, Immobile), but these fields are also referenced to the table Immobili.

First question: Can I make a Primary Key that is also a Foreign Key?

Second Question: When I try to export the changes it says:

Executing SQL script in server

# ERROR: Error 1005: Can't create table 'dbimmobili.condoni' (errno: 150)

CREATE TABLE IF NOT EXISTS `dbimmobili`.`Condoni` (

`ComuneImmobile` VARCHAR(50) NOT NULL ,

`ViaImmobile` VARCHAR(50) NOT NULL ,

`CivicoImmobile` VARCHAR(5) NOT NULL ,

`InternoImmobile` VARCHAR(3) NOT NULL ,

`ProtocolloNumero` VARCHAR(15) NULL ,

`DataRichiestaSanatoria` DATE NULL ,

`DataSanatoria` DATE NULL ,

`SullePartiEsclusive` TINYINT(1) NULL ,

`SullePartiComuni` TINYINT(1) NULL ,

`OblazioneInEuro` DOUBLE NULL ,

`TecnicoOblazione` VARCHAR(45) NULL ,

`TelefonoTecnico` VARCHAR(15) NULL ,

INDEX `ComuneImmobile` (`ComuneImmobile` ASC) ,

INDEX `ViaImmobile` (`ViaImmobile` ASC) ,

INDEX `CivicoImmobile` (`CivicoImmobile` ASC) ,

INDEX `InternoImmobile` (`InternoImmobile` ASC) ,

PRIMARY KEY (`ComuneImmobile`, `ViaImmobile`, `CivicoImmobile`, `InternoImmobile`) ,

CONSTRAINT `ComuneImmobile`

FOREIGN KEY (`ComuneImmobile` )

REFERENCES `dbimmobili`.`Immobile` (`ComuneImmobile` )

ON DELETE CASCADE

ON UPDATE CASCADE,

CONSTRAINT `ViaImmobile`

FOREIGN KEY (`ViaImmobile` )

REFERENCES `dbimmobili`.`Immobile` (`ViaImmobile` )

ON DELETE CASCADE

ON UPDATE CASCADE,

CONSTRAINT `CivicoImmobile`

FOREIGN KEY (`CivicoImmobile` )

REFERENCES `dbimmobili`.`Immobile` (`CivicoImmobile` )

ON DELETE CASCADE

ON UPDATE CASCADE,

CONSTRAINT `InternoImmobile`

FOREIGN KEY (`InternoImmobile` )

REFERENCES `dbimmobili`.`Immobile` (`InternoImmobile` )

ON DELETE CASCADE

ON UPDATE CASCADE

) ENGINE = InnoDB

Showing the Engine Status:

Error in foreign key constraint of table dbimmobili/valutazionimercato:

Cannot find an index in the referenced table where the referenced columns appear as the first columns,

or columns typse in the table and the referenced table do not match for constraint. Note that the internal storage type of ENUM and SET changed in tables created with >=

InnoDB-4.1.12, and such columns in old tables cannot be referenced by such columns

in new tables.

Where I'm doing wrong?

解决方案

When creating a foreign key constraint, MySQL requires a usable index on both the referencing table and also on the referenced table. The index on the referencing table is created automatically if one doesn't exist, but the one on the referenced table needs to be created manually (Source). Yours appears to be missing.

Test case:

CREATE TABLE tbl_a (

id int PRIMARY KEY,

some_other_id int,

value int

) ENGINE=INNODB;

Query OK, 0 rows affected (0.10 sec)

CREATE TABLE tbl_b (

id int PRIMARY KEY,

a_id int,

FOREIGN KEY (a_id) REFERENCES tbl_a (some_other_id)

) ENGINE=INNODB;

ERROR 1005 (HY000): Can't create table 'e.tbl_b' (errno: 150)

But if we add an index on some_other_id:

CREATE INDEX ix_some_id ON tbl_a (some_other_id);

Query OK, 0 rows affected (0.11 sec)

Records: 0 Duplicates: 0 Warnings: 0

CREATE TABLE tbl_b (

id int PRIMARY KEY,

a_id int,

FOREIGN KEY (a_id) REFERENCES tbl_a (some_other_id)

) ENGINE=INNODB;

Query OK, 0 rows affected (0.06 sec)

This is often not an issue in most situations, since the referenced field is often the primary key of the referenced table, and the primary key is indexed automatically.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值