我有兩個表。一個是'subcategory',id是一個自動遞增的主鍵。MySQL錯誤號。 150當試圖創建外鍵
從第二個表,「product_subcategory」我希望有現場類別使用「subcategory.id」作爲一個外鍵。
當使用
ALTER TABLE Product_SubCategory ADD CONSTRAINT fk_subcategory_product
FOREIGN KEY (subCategory) REFERENCES subCategory(id);
我收到錯誤沒有。 150.
說完看着,我發現this問題與規定這些條件可能會導致錯誤沒有150的回答錯誤:
1. The two tables must be ENGINE=InnoDB. (can be others: ENGINE=MyISAM works too) 2. The two tables must have the same charset. 3. The PK column(s) in the parent table and the FK column(s) must be the same data type. 4. The PK column(s) in the parent table and the FK column(s), if they have a define collation type, must have the same collation type; 5. If there is data already in the foreign key table, the FK column value(s) must match values in the parent table PK columns. 6. And the child table cannot be a temporary table.
所以我用SHOW TABLE STATUS來確認表是合適的:
| Name | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time | Update_time | Check_time | Collation | Checksum | Create_options | Comment |
| SubCategory | InnoDB | 10 | Compact | 0 | 0 | 16384 | 0 | 16384 | 6291456 | NULL | 2013-07-31 16:37:03 | NULL | NULL | latin1_swedish_ci | NULL | | |
&
| Name | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time | Update_time | Check_time | Collation | Checksum | Create_options | Comment |
| Product_SubCategory | InnoDB | 10 | Compact | 0 | 0 | 16384 | 0 | 0 | 6291456 | NULL | 2013-07-31 16:35:04 | NULL | NULL | latin1_swedish_ci | NULL | | |
唯一的區別是所述index_length。
各PK & FK列數據是從我可以看到相同的,並且這兩個表是空的。
有沒有人有什麼可能會導致此錯誤的150有什麼建議?或者可以建議其他地區看看幫我解決這個問題?
編輯:確切的錯誤信息是:
ERROR 1005(HY000):無法創建表 'NTM#SQL-31c_365。'(錯誤:150)
DDL信息:
> -- MySQL dump 10.13 Distrib 5.5.31, for debian-linux-gnu (x86_64)
--
-- Host: localhost Database: NTM
-- ------------------------------------------------------
-- Server version 5.5.31-0ubuntu0.13.04.1
/*!40101 SET @[email protected]@CHARACTER_SET_CLIENT */;
/*!40101 SET @[email protected]@CHARACTER_SET_RESULTS */;
/*!40101 SET @[email protected]@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @[email protected]@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @[email protected]@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @[email protected]@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @[email protected]@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @[email protected]@SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table `Product`
--
DROP TABLE IF EXISTS `Product`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `Product` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`category` int(11) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
`price` int(11) DEFAULT NULL,
`tax` double DEFAULT NULL,
`weight` int(11) DEFAULT NULL,
`size` varchar(255) DEFAULT NULL,
`discount` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fk_product_category` (`category`),
CONSTRAINT `fk_product_category` FOREIGN KEY (`category`) REFERENCES `Category` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `Product_SubCategory`
--
DROP TABLE IF EXISTS `Product_SubCategory`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `Product_SubCategory` (
`product` int(11) NOT NULL,
`subCategory` int(11) DEFAULT NULL,
PRIMARY KEY (`product`),
CONSTRAINT `fk_product_subcategory` FOREIGN KEY (`product`) REFERENCES `Product` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Table structure for table `SubCategory`
--
DROP TABLE IF EXISTS `SubCategory`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `SubCategory` (
`id` int(11) NOT NULL DEFAULT '0',
`parent` int(11) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
`text` longtext,
`summary` longtext,
`image` varchar(255) DEFAULT NULL,
`featured` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fk_SC_parent_id` (`parent`),
CONSTRAINT `fk_SC_parent_id` FOREIGN KEY (`parent`) REFERENCES `Category` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40103 SET [email protected]_TIME_ZONE */;
/*!40101 SET [email protected]_SQL_MODE */;
/*!40014 SET [email protected]_FOREIGN_KEY_CHECKS */;
/*!40014 SET [email protected]_UNIQUE_CHECKS */;
/*!40101 SET [email protected]_CHARACTER_SET_CLIENT */;
/*!40101 SET [email protected]_CHARACTER_SET_RESULTS */;
/*!40101 SET [email protected]_COLLATION_CONNECTION */;
/*!40111 SET [email protected]_SQL_NOTES */;
你能顯示整個錯誤信息嗎? –
你可以顯示錶DDL信息嗎?你有這些表中預先存在的數據會破壞外鍵約束嗎? –
不應該將外鍵作爲列嗎? – Mihai