我正在尝试创建一个订单管理系统
但在创建基表后,即(Suppliers,Customers,Categories,Employees,Shippers
)
我通过创建产品和订单进入下一级,我可以在这些表中添加外键。
现在我添加了最后一级表oderDetail
并试图将foregin键添加到OrderID和ProdcuID但它只是给我错误150
mysqldump的
-- 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;
答案 0 :(得分:3)
两个字段之间的外键不完全相同。字段类型,维度和标志应该相同。
在表 unsigned
( productId, orderId )
列中添加 testdrive.orderdetail
int