gpt4 book ai didi

sql - 我无法在 mysql 错误 150 中创建外键。请帮助

转载 作者:搜寻专家 更新时间:2023-10-30 23:23:44 24 4
gpt4 key购买 nike

我正在尝试在我的表中创建一个外键。但是当我执行查询时,它显示错误 150。

Error Code : 1025
Error on create foreign key of '.\vts\#sql-6ec_1' to '.\vts\tblguardian' (errno: 150)
(0 ms taken)

我的查询是

创建外键的查询

alter table `vts`.`tblguardian` add constraint `FK_tblguardian` FOREIGN KEY (`GuardianPickPointId`) REFERENCES `tblpickpoint` (`PickPointId`)

主键表

CREATE TABLE `tblpickpoint` (                                                           
`PickPointId` int(4) NOT NULL auto_increment,
`PickPointName` varchar(500) default NULL,
`PickPointLabel` varchar(500) default NULL,
`PickPointLatLong` varchar(100) NOT NULL,
PRIMARY KEY (`PickPointId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC

外键表

CREATE TABLE `tblguardian` (                           
`GuardianId` int(4) NOT NULL auto_increment,
`GuardianName` varchar(500) default NULL,
`GuardianAddress` varchar(500) default NULL,
`GuardianMobilePrimary` varchar(15) NOT NULL,
`GuardianMobileSecondary` varchar(15) default NULL,
`GuardianPickPointId` int(4) default NULL,
PRIMARY KEY (`GuardianId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

最佳答案

您的问题是约束中列的类型不同。它们必须相同。

`PickPointId` int(4) NOT NULL auto_increment, 

`GuardianPickPointId` varchar(100) default NULL,

有关详细信息,请参阅 the documentation :

Corresponding columns in the foreign key and the referenced key must have similar internal data types inside InnoDB so that they can be compared without a type conversion. The size and sign of integer types must be the same. The length of string types need not be the same. For nonbinary (character) string columns, the character set and collation must be the same.

关于sql - 我无法在 mysql 错误 150 中创建外键。请帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2577303/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com