#1005-没有puedo crear tabla`blog`.`carros`(错误:150“外键约束格式不正确”)

raogr8fs  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(367)
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;
beq87vna

beq87vna1#

切换创建脚本的顺序,以便 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;

相关问题