#1005 - 没有 puedo crear tabla `blog`.`carros`(错误:150“外键约束格式不正确”)
Posted
技术标签:
【中文标题】#1005 - 没有 puedo crear tabla `blog`.`carros`(错误:150“外键约束格式不正确”)【英文标题】:#1005 - No puedo crear tabla `blog`.`carros` (Error: 150 "Foreign key constraint is incorrectly formed") 【发布时间】:2019-05-02 05:15:30 【问题描述】:USE blog;
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
CREATE TABLE usuarios (
id INT NOT NULL UNIQUE AUTO_INCREMENT,
nombre VARCHAR(25) NOT NULL UNIQUE,
email VARCHAR(255) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
fecha_registro DATETIME NOT NULL,
activo TINYINT NOT NULL,
PRIMARY KEY(id)
);
CREATE TABLE marcas (
marca_id int(100) NOT NULL AUTO_INCREMENT,
marca_titulo text NOT NULL,
PRIMARY KEY (marca_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE carros (
c_id INT(10) NOT NULL,
p_id INT NOT NULL,
add_ip varchar(250) NOT NULL,
usuario_id INT NOT NULL,
cantidad int(10) NOT NULL,
PRIMARY KEY(c_id),
FOREIGN KEY(p_id)
REFERENCES productos(producto_id)
ON UPDATE CASCADE
ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE categorias (
cat_id int(100) NOT NULL AUTO_INCREMENT,
cat_titulo text NOT NULL,
PRIMARY KEY (cat_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE orders (
`order_id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`product_id` int(11) NOT NULL,
`qty` int(11) NOT NULL,
`trx_id` varchar(255) NOT NULL,
`p_status` varchar(20) NOT NULL,
PRIMARY KEY (`order_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE productos (
producto_id INT NOT NULL AUTO_INCREMENT,
p_cat INT(100) NOT NULL,
p_marca INT(100) NOT NULL,
p_titulo varchar(255) NOT NULL,
p_precio int(100) NOT NULL,
p_desc text NOT NULL,
p_imagen LONGBLOB,
p_claves text NOT NULL,
PRIMARY KEY (producto_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
【问题讨论】:
为什么人们投票不喜欢并且不提供帮助? 【参考方案1】:切换创建脚本的顺序,以便首先创建 productos
表:
USE blog;
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
CREATE TABLE usuarios (
id INT NOT NULL UNIQUE AUTO_INCREMENT,
nombre VARCHAR(25) NOT NULL UNIQUE,
email VARCHAR(255) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
fecha_registro DATETIME NOT NULL,
activo TINYINT NOT NULL,
PRIMARY KEY(id)
);
CREATE TABLE marcas (
marca_id int(100) NOT NULL AUTO_INCREMENT,
marca_titulo text NOT NULL,
PRIMARY KEY (marca_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE productos (
producto_id INT NOT NULL AUTO_INCREMENT,
p_cat INT(100) NOT NULL,
p_marca INT(100) NOT NULL,
p_titulo varchar(255) NOT NULL,
p_precio int(100) NOT NULL,
p_desc text NOT NULL,
p_imagen LONGBLOB,
p_claves text NOT NULL,
PRIMARY KEY (producto_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE carros (
c_id INT(10) NOT NULL,
p_id INT NOT NULL,
add_ip varchar(250) NOT NULL,
usuario_id INT NOT NULL,
cantidad int(10) NOT NULL,
PRIMARY KEY(c_id),
FOREIGN KEY(p_id)
REFERENCES productos(producto_id)
ON UPDATE CASCADE
ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE categorias (
cat_id int(100) NOT NULL AUTO_INCREMENT,
cat_titulo text NOT NULL,
PRIMARY KEY (cat_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE orders (
`order_id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`product_id` int(11) NOT NULL,
`qty` int(11) NOT NULL,
`trx_id` varchar(255) NOT NULL,
`p_status` varchar(20) NOT NULL,
PRIMARY KEY (`order_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
【讨论】:
非常感谢!你救了我! 没问题。很高兴为您提供帮助以上是关于#1005 - 没有 puedo crear tabla `blog`.`carros`(错误:150“外键约束格式不正确”)的主要内容,如果未能解决你的问题,请参考以下文章