gpt4 book ai didi

mysql 外键错误 150

转载 作者:行者123 更新时间:2023-11-29 06:03:12 25 4
gpt4 key购买 nike

我正在尝试创建一个订单管理系统但在创建基表后,即(Suppliers,Customers,Categories,Employees,Shippers)我通过创建产品和订单进入下一个级别,我可以在这些表中添加外键。现在我添加了最后一级表oderDetail并尝试将foregin键添加到OrderID和ProdcuID,但它只是给了我错误150mysql转储

-- MySQL Administrator dump 1.4
--
-- ------------------------------------------------------
-- Server version 5.5.8


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;


--
-- Create schema testdrive
--

CREATE DATABASE IF NOT EXISTS testdrive;
USE testdrive;

--
-- Definition of table `categories`
--

DROP TABLE IF EXISTS `categories`;
CREATE TABLE `categories` (
`CategoryID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`Description` varchar(45) NOT NULL,
`Picture` blob,
PRIMARY KEY (`CategoryID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `categories`
--

/*!40000 ALTER TABLE `categories` DISABLE KEYS */;
/*!40000 ALTER TABLE `categories` ENABLE KEYS */;


--
-- Definition of table `customers`
--

DROP TABLE IF EXISTS `customers`;
CREATE TABLE `customers` (
`CustomerID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`CompanyName` varchar(255) NOT NULL,
`ContactName` varchar(255) NOT NULL,
`ContactTitle` varchar(255) NOT NULL,
`Address` text NOT NULL,
`City` varchar(255) NOT NULL,
`Region` varchar(255) NOT NULL,
`PostalCode` varchar(45) NOT NULL,
`Country` varchar(255) NOT NULL,
`Phone` varchar(255) NOT NULL,
`Fax` varchar(255) NOT NULL,
PRIMARY KEY (`CustomerID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `customers`
--

/*!40000 ALTER TABLE `customers` DISABLE KEYS */;
/*!40000 ALTER TABLE `customers` ENABLE KEYS */;


--
-- Definition of table `employees`
--

DROP TABLE IF EXISTS `employees`;
CREATE TABLE `employees` (
`EmployeeID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`LastName` varchar(255) NOT NULL,
`FirstName` varchar(255) NOT NULL,
`Title` varchar(255) NOT NULL,
`BirthDate` date NOT NULL,
`HireDate` date NOT NULL,
`Address` text NOT NULL,
`City` varchar(255) NOT NULL,
`Region` varchar(255) NOT NULL,
`PostalCode` varchar(45) NOT NULL,
`Country` varchar(255) NOT NULL,
`HomePhone` varchar(255) NOT NULL,
`Extension` varchar(255) DEFAULT NULL,
`Photo` blob NOT NULL,
`Notes` varchar(255) DEFAULT NULL,
PRIMARY KEY (`EmployeeID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `employees`
--

/*!40000 ALTER TABLE `employees` DISABLE KEYS */;
/*!40000 ALTER TABLE `employees` ENABLE KEYS */;


--
-- Definition of table `orderdetail`
--

DROP TABLE IF EXISTS `orderdetail`;
CREATE TABLE `orderdetail` (
`OrderDetailID` int(10) NOT NULL,
`ProductID` int(10) NOT NULL,
`OrderID` int(10) NOT NULL,
`Quantity` varchar(255) NOT NULL,
`Discount` varchar(255) NOT NULL,
PRIMARY KEY (`OrderDetailID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `orderdetail`
--

/*!40000 ALTER TABLE `orderdetail` DISABLE KEYS */;
/*!40000 ALTER TABLE `orderdetail` ENABLE KEYS */;


--
-- Definition of table `ordres`
--

DROP TABLE IF EXISTS `ordres`;
CREATE TABLE `ordres` (
`OrderID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`CustomerID` int(10) unsigned NOT NULL,
`EmployeeID` int(10) unsigned NOT NULL,
`OderDate` date NOT NULL,
`RequiredDate` date NOT NULL,
`ShippedDate` date NOT NULL,
`ShipperID` int(10) unsigned NOT NULL,
PRIMARY KEY (`OrderID`),
KEY `Order_Customer` (`CustomerID`),
KEY `Order_Employee` (`EmployeeID`),
KEY `Order_Shipper` (`ShipperID`),
CONSTRAINT `Order_Customer` FOREIGN KEY (`CustomerID`) REFERENCES `customers` (`CustomerID`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `Order_Employee` FOREIGN KEY (`EmployeeID`) REFERENCES `employees` (`EmployeeID`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `Order_Shipper` FOREIGN KEY (`ShipperID`) REFERENCES `shippers` (`ShipperID`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `ordres`
--

/*!40000 ALTER TABLE `ordres` DISABLE KEYS */;
/*!40000 ALTER TABLE `ordres` ENABLE KEYS */;


--
-- Definition of table `products`
--

DROP TABLE IF EXISTS `products`;
CREATE TABLE `products` (
`ProductID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`ProductName` varchar(255) NOT NULL,
`SupplierID` int(10) unsigned NOT NULL,
`CategoryID` int(10) unsigned NOT NULL,
`QuantityPerUnit` varchar(255) NOT NULL,
`UnitPrice` varchar(255) NOT NULL,
PRIMARY KEY (`ProductID`),
KEY `Product_Supplier` (`SupplierID`),
KEY `Product_Category` (`CategoryID`),
CONSTRAINT `Product_Supplier` FOREIGN KEY (`SupplierID`) REFERENCES `suppliers` (`SupplierID`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `Product_Category` FOREIGN KEY (`CategoryID`) REFERENCES `categories` (`CategoryID`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `products`
--

/*!40000 ALTER TABLE `products` DISABLE KEYS */;
/*!40000 ALTER TABLE `products` ENABLE KEYS */;


--
-- Definition of table `shippers`
--

DROP TABLE IF EXISTS `shippers`;
CREATE TABLE `shippers` (
`ShipperID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`CompanyName` varchar(255) NOT NULL,
`Phone` varchar(255) NOT NULL,
PRIMARY KEY (`ShipperID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `shippers`
--

/*!40000 ALTER TABLE `shippers` DISABLE KEYS */;
/*!40000 ALTER TABLE `shippers` ENABLE KEYS */;


--
-- Definition of table `suppliers`
--

DROP TABLE IF EXISTS `suppliers`;
CREATE TABLE `suppliers` (
`SupplierID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`CompanyName` varchar(255) NOT NULL,
`ContractTittle` varchar(255) NOT NULL,
`Address` text NOT NULL,
`City` varchar(255) NOT NULL,
`Region` varchar(255) NOT NULL,
`PostalCode` varchar(255) NOT NULL,
`Country` varchar(255) NOT NULL,
`Phone` varchar(255) NOT NULL,
`Fax` varchar(255) NOT NULL,
`HomePage` text NOT NULL,
PRIMARY KEY (`SupplierID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `suppliers`
--

/*!40000 ALTER TABLE `suppliers` DISABLE KEYS */;
/*!40000 ALTER TABLE `suppliers` ENABLE KEYS */;

这就是我想做的

  ALTER TABLE testdrive.orderdetail
ADD CONSTRAINT orderDetail_Product FOREIGN KEY (ProductID) REFERENCES testdrive.products (ProductID) ON UPDATE CASCADE ON DELETE CASCADE,
ADD CONSTRAINT OrderDetail_Order FOREIGN KEY (OrderID) REFERENCES testdrive.ordres (OrderID) ON UPDATE CASCADE ON DELETE CASCADE;

最佳答案

两个字段之间的forein 键不完全相同。字段类型、维度和标志应该相同。

在表 testdrive.orderdetail (productId, orderId) 列中添加 unsigned int

关于mysql 外键错误 150,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12857856/

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