gpt4 book ai didi

mysql - #1005 - 无法创建表 'testt.rating'(错误号 : 150) with MySQL

转载 作者:行者123 更新时间:2023-11-30 23:31:04 25 4
gpt4 key购买 nike

我在 MySQL 中得到 errorno:150。当我尝试创建第三个表时,它显示无法创建表。以下是完整的查询。

CREATE DATABASE `test`;
USE `test`;

-- --------------------------------------------------------

--
-- Table structure for table `product`
--

CREATE TABLE IF NOT EXISTS `product` (
`product_no.` int(10) NOT NULL AUTO_INCREMENT COMMENT 'This number represent a unique product',
`name` varchar(50) NOT NULL ,
`price` decimal(5,2) NOT NULL COMMENT 'Price should have a fomat like 25.90',
`max_Rating` int(10) NOT NULL COMMENT 'Maximum available rating for each product (n)',
PRIMARY KEY (`product_no.`)
);

-- --------------------------------------------------------

--
-- Table structure for table `user`
--
CREATE TABLE IF NOT EXISTS `user` (
`user_id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`password` varchar(20) NOT NULL,
PRIMARY KEY (`user_id`)
);

-- --------------------------------------------------------

--
-- Table structure for table `rating`
--
CREATE TABLE IF NOT EXISTS `rating` (
`s.n.` int(10) NOT NULL AUTO_INCREMENT,
`product_no.` int(10) NOT NULL,
`user_id` int(10) DEFAULT NULL,
`given_rating` int(10) DEFAULT NULL,
PRIMARY KEY (`s.n.`),
foreign key (`user_id`) references `user(user_id)`,
foreign key (`product_no.`) references `product(product_no.)`
);

是否发生了字段不匹配?我现在卡住了。

最佳答案

它看起来与您的外键定义中的引号有关。

CREATE TABLE IF NOT EXISTS `rating` (
`s.n.` int(10) NOT NULL AUTO_INCREMENT,
`product_no.` int(10) NOT NULL,
`user_id` int(10) DEFAULT NULL,
`given_rating` int(10) DEFAULT NULL,
PRIMARY KEY (`s.n.`),
/* Remove the backquotes surrounding user(user_id) and product(product_no.) */
foreign key (`user_id`) references user (user_id),
/* Inside the parens, `product_no.` should be quoted because of the period */
foreign key (`product_no.`) references product (`product_no.`)
);

我只想指出,您不会经常遇到以特殊字符命名的表,例如 s.nproduct_no.。尽管它们在被引用时被允许并且可以工作,但由于它们引入的引用需求,使用它们有些不常见。如果没有 .,则没有必要引用名为 product_no 的列。

关于mysql - #1005 - 无法创建表 'testt.rating'(错误号 : 150) with MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10623145/

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