gpt4 book ai didi

mysql表创建错误代码: 1005 in this specific case

转载 作者:行者123 更新时间:2023-11-29 13:22:16 25 4
gpt4 key购买 nike

我已尝试一切方法来解决此问题,是的,我知道此处已提出此类问题,但我无法解决我的问题它是mysql数据库

 Error Code: 1005

Can't create table '.\project\comments.frm' (errno: 150)

外键在结构上匹配(即长度类型),那么表创建中可能存在什么问题

给出错误的表是comments:

 CREATE TABLE `comments`(
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`description` VARCHAR(100) NOT NULL,
`user_id` INT(10) UNSIGNED NOT NULL,
`post_id` INT(10) UNSIGNED NOT NULL,
FOREIGN KEY (`post_id`) REFERENCES `posts`.`id`,
FOREIGN KEY (`user_id`) REFERENCES `users`.`id`,
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=latin1;`

这是已经在数据库中创建的帖子表

 CREATE TABLE `posts` (
`id` int(10) unsigned NOT NULL auto_increment,
`title` varchar(30) default NULL,
`description` longtext,
`image` varchar(50) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

这是已经在数据库中创建的用户表

 CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL auto_increment,
`user_name` varchar(33) default NULL,
`email` varchar(255) NOT NULL,
`password` varchar(255) default NULL,
`type` varchar(255) NOT NULL,
`registrationDate` date NOT NULL,
PRIMARY KEY (`id`,`email`,`type`)
)

最佳答案

您的语法不正确。 REFERENCES 关键字后应跟表(列):

CREATE TABLE `comments`(
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`description` VARCHAR(100) NOT NULL,
`user_id` INT(10) UNSIGNED NOT NULL,
`post_id` INT(10) UNSIGNED NOT NULL,
FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`),
FOREIGN KEY (`user_id`) REFERENCES `users` (`id`),
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=latin1;

关于mysql表创建错误代码: 1005 in this specific case,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20658801/

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