我正在将数据库从 MySQL 5 迁移到 SQL Server 2008。我正在使用 SQL Server 迁移助手,它给了我一个我不明白的错误。
我有看起来像这样的表:
Table A
ID (primary key)
ProductionBatch
Manufacturer
LotNo
Cost
(and a bunch of other fields)
Table B
ProductionBatch (primary key)
Manufacturer (primary key)
LotNo (primary key)
Cost (primary key)
(and a bunch of other fields)
在 MySQL 中,我在两个表中指出的四个字段构成表 B 中的主键和表 A 中的外键;我不希望将任何新记录插入到表 B 中,除非表 A 中的这些字段匹配。在 MySQL 中工作正常,但迁移助手给我以下错误:
M2SS0048: Foreign Key does not contains all the columns of Primary/Unique Key
除了语法错误,我无法弄清楚这个错误是什么意思。是不是我允许表 A 中的那些字段为空值?或者它们在表 A 中不必是唯一的?我尝试使用自动递增整数作为表 B 的主键(而不是复合键),但这似乎没有帮助。
任何想法或建议将不胜感激。
编辑:要清楚,我的外键在表 B 上。我先将记录放入表 A,然后将记录放入表 B。但我不想将与表 A 中的任何不匹配的记录放入表 B .
第二次编辑:我被要求展示这个问题的代码。正如我对提问者所说,我一直在用一个简单的例子来提问;我的表格实际上更复杂,我认为简化版本会更容易回答。但这里是实际的表格,如果有人想看看我真正在做什么。我链接的实际字段是 QCBatchID、LaboratoryName、Constituent 和 Fraction(我在上面的示例中使用了通用字段)。
表 A 是:
CREATE TABLE `chemistry_qc` (
`QCBatchID` varchar(12) COLLATE utf8_unicode_ci NOT NULL,
`LaboratoryName` varchar(45) COLLATE utf8_unicode_ci NOT NULL,
`Constituent` varchar(45) COLLATE utf8_unicode_ci NOT NULL,
`Fraction` enum('Not Reported','NA','Total','Dissolved','TR') COLLATE utf8_unicode_ci
NOT NULL,
`LabSampleType` varchar(12) COLLATE utf8_unicode_ci DEFAULT NULL,
`PercentRecovery` decimal(5,2) DEFAULT NULL,
`RPD` double(5,2) DEFAULT NULL,
PRIMARY KEY (`QCBatchID`,`LaboratoryName`,`Constituent`,`Fraction`),
CONSTRAINT `fk_chem_qc_chem` FOREIGN KEY (`QCBatchID`, `LaboratoryName`, `Constituent`, `Fraction`) REFERENCES `chemistry` (`LaboratoryName`, `QCBatchID`, `Constituent`, `Fraction`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
表 B 是:
CREATE TABLE `chemistry` (
`ChemistryID` int(11) NOT NULL AUTO_INCREMENT,
`StationID` varchar(45) COLLATE utf8_unicode_ci NOT NULL,
`EventStartDateTime` datetime NOT NULL,
`SampleStartDateTime` datetime DEFAULT NULL,
`SampleEndDateTime` datetime DEFAULT NULL,
`SampleType` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
`FieldSampleID` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
`Matrix` varchar(12) COLLATE utf8_unicode_ci DEFAULT NULL,
`FieldQC` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
`GrabComposite` enum('C','G') COLLATE utf8_unicode_ci DEFAULT NULL,
`EventRepresentation` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
`CollectionMethod` varchar(12) COLLATE utf8_unicode_ci DEFAULT NULL,
`LaboratoryName` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
`LabSampleID` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
`ConstituentType` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
`Constituent` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
`CASNumber` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
`Fraction` enum('TR','Dissolved','Total','Not Reported','NA') COLLATE utf8_unicode_ci DEFAULT NULL,
`QCBatchID` varchar(12) COLLATE utf8_unicode_ci DEFAULT NULL,
`SamplePrepMethod` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
`AnalysisType` varchar(5) COLLATE utf8_unicode_ci DEFAULT NULL,
`LabSampleType` varchar(4) COLLATE utf8_unicode_ci DEFAULT NULL,
`AnalyteType` varchar(5) COLLATE utf8_unicode_ci DEFAULT NULL,
`SamplePrepDate` date DEFAULT NULL,
`ReportedValue` double DEFAULT NULL,
`Units` varchar(12) COLLATE utf8_unicode_ci DEFAULT NULL,
`NumericQualifier` enum('>=','<=','>','<','=') COLLATE utf8_unicode_ci DEFAULT NULL,
`DataQualifier` text COLLATE utf8_unicode_ci,
`ReportingLimit` double DEFAULT NULL,
`MethodDetectionLimit` double DEFAULT NULL,
`PercentMoisture` decimal(3,2) DEFAULT NULL,
`MethodReference` varchar(12) COLLATE utf8_unicode_ci DEFAULT NULL,
`MethodNumber` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
`AnalysisDateTime` datetime DEFAULT NULL,
`Dilution` int(3) DEFAULT NULL,
`SampleNotes` longtext COLLATE utf8_unicode_ci,
PRIMARY KEY (`ChemistryID`),
KEY `fk_chemistry_event1` (`StationID`,`EventStartDateTime`),
KEY `fk_chemistry_laboratory1` (`LaboratoryName`),
KEY `ChemistryID` (`ChemistryID`,`StationID`,`EventStartDateTime`),
KEY `fk_chemistry_c_EventRepresentation1` (`EventRepresentation`),
KEY `fk_chemistry_c_collectionmethod1` (`CollectionMethod`),
KEY `fk_chemistry_c_sampletype1` (`SampleType`),
KEY `fk_chemistry_c_matrix` (`Matrix`),
KEY `fk_chemistry_c_methods` (`Constituent`,`ConstituentType`,`Units`,`MethodReference`,`MethodNumber`),
KEY `fk_chemistry_sampleprepmethod1` (`SamplePrepMethod`),
KEY `fk_chemistry_analysistype` (`AnalysisType`),
KEY `fk_chemistry_analytetype` (`AnalyteType`),
KEY `fk_chemistry_labsampletype` (`LabSampleType`),
KEY `ChemistryID_2` (`ChemistryID`,`StationID`),
KEY `LaboratoryName` (`LaboratoryName`,`Constituent`,`Fraction`,`QCBatchID`),
KEY `QCBatchID_4` (`QCBatchID`,`LaboratoryName`,`Constituent`,`Fraction`),
KEY `LaboratoryName_4` (`LaboratoryName`,`QCBatchID`,`Constituent`,`Fraction`),
CONSTRAINT `chemistry_analysistype` FOREIGN KEY (`AnalysisType`) REFERENCES `c_analysistype` (`AnalysisType`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `chemistry_ibfk_10` FOREIGN KEY (`SamplePrepMethod`) REFERENCES `c_sampleprepmethod` (`SamplePrepMethod`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `chemistry_ibfk_11` FOREIGN KEY (`Constituent`, `ConstituentType`, `Units`, `MethodReference`, `MethodNumber`) REFERENCES `c_methods` (`Constituent`, `ConstituentType`, `Units`, `MethodReference`, `MethodNumber`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `chemistry_ibfk_2` FOREIGN KEY (`AnalyteType`) REFERENCES `c_analytetype` (`AnalyteType`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `chemistry_ibfk_3` FOREIGN KEY (`CollectionMethod`) REFERENCES `c_collectionmethod` (`CollectionMethod`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `chemistry_ibfk_4` FOREIGN KEY (`EventRepresentation`) REFERENCES `c_eventrepresentation` (`EventRepresentation`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `chemistry_ibfk_5` FOREIGN KEY (`Matrix`) REFERENCES `c_matrix` (`Matrix`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `chemistry_ibfk_6` FOREIGN KEY (`SampleType`) REFERENCES `c_sampletype` (`SampleType`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `chemistry_ibfk_7` FOREIGN KEY (`StationID`, `EventStartDateTime`) REFERENCES `event` (`StationID`, `EventStartDateTime`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `chemistry_ibfk_8` FOREIGN KEY (`LaboratoryName`) REFERENCES `laboratory` (`LaboratoryName`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `chemistry_ibfk_9` FOREIGN KEY (`LabSampleType`) REFERENCES `c_labsampletype` (`LabSampleType`) ON DELETE NO ACTION ON UPDATE NO ACTION) ENGINE=InnoDB AUTO_INCREMENT=206971 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci