Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8136773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:51:55+00:00 2026-06-06T10:51:55+00:00

I’m migrating a database from MySQL 5 to SQL Server 2008. I’m using the

  • 0

I’m migrating a database from MySQL 5 to SQL Server 2008. I’m using the SQL Server Migration Assistant, and it’s giving me an error that I don’t understand.

I have tables that look like this:

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)

In MySQL, the four fields I’ve indicated in both tables form the primary key in Table B and a foreign key in Table A; I don’t want any new records inserted into table B unless there’s a match on those fields in Table A. Works fine in MySQL, but the Migration Assistant is giving me the following error:

M2SS0048: Foreign Key does not contains all the columns of Primary/Unique Key

The grammar error aside, I can’t figure out what this error means. Is it that I’m allowing null values for those fields in Table A? Or that they do not have to be unique in Table A? I tried using an autoincrementing integer as a primary key for Table B (instead of the compound key), but that didn’t seem to help.

Any ideas or suggestions would be appreciated.

EDIT: To be clear, my foreign key is on Table B. I put records into Table A first, then put records into Table B. But I don’t want to put records into Table B that don’t match any in Table A.

SECOND EDIT: I’ve been asked to show the code for this problem. As I told the questioner, I’ve been using a simple example to ask the question; my tables are actually more complicated, and I thought a simplified version would be easier to answer. But here are the actual tables, if anyone would like to see what I’m really doing. The actual fields I’m linking on are QCBatchID, LaboratoryName, Constituent, and Fraction (I used generic ones in the above example).

Table A is:

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

Table B is:

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  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T10:51:56+00:00Added an answer on June 6, 2026 at 10:51 am

    I’ll keep only the relevant columns from the 2 tables:

    Table 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,
      --- more columns
    
      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 ;
    

    Table B:

    CREATE TABLE chemistry (
      ChemistryID     int(11) NOT NULL AUTO_INCREMENT,
      --- more columns
      LaboratoryName  varchar(45) COLLATE utf8_unicode_ci  DEFAULT NULL,
      --- more columns
      Constituent     varchar(45) 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,
      --- more columns
    
    PRIMARY KEY (ChemistryID),
    
    KEY fk_chemistry_laboratory1 (LaboratoryName),
    --- more KEYs
    KEY LaboratoryName (LaboratoryName, Constituent, Fraction, QCBatchID),
    KEY QCBatchID_4 (QCBatchID, LaboratoryName, Constituent, Fraction),
          --- this key should have been needed for your FK reference
    
    KEY LaboratoryName_4 (LaboratoryName , QCBatchID, Constituent, Fraction),
          --- this key was needed for your FK reference
    
    --- CONSTRAINTS 
    
    ) ENGINE=InnoDB 
      AUTO_INCREMENT=206971 
      DEFAULT CHARSET=utf8 
      COLLATE=utf8_unicode_ci ; 
    

    For the record, this a FOREIGN KEY from table A (chemistry_qc) to table B (chemistry)


    There are two errors in your FOREIGN KEY constraint. The first was done by you (and assisted by MySQL). You have the two referencing and the referenced compound key with different ordering:

    CONSTRAINT fk_chem_qc_chem 
        FOREIGN KEY (QCBatchID, LaboratoryName, Constituent, Fraction) 
          REFERENCES chemistry 
                    (LaboratoryName, QCBatchID, Constituent, Fraction) 
                 ---    |||             |||   
                 --- *** wrong order here *** ---
    

    It should be:

    CONSTRAINT fk_chem_qc_chem 
        FOREIGN KEY (QCBatchID, LaboratoryName, Constituent, Fraction) 
          REFERENCES chemistry 
                    (QCBatchID, LaboratoryName, Constituent, Fraction) 
    

    MySQL should have complained when you tried to add an FK where a VARCHAR(45) was referencing a VARCHAR(12) but it didn’t.


    The second error is that your FK is referencing a key that is neither PRIMARY nor UNIQUE. Another assistance by MySQL to screw things by allowing an FK to a non-unique column-compound.

    So, you should have either (case-1) declared that key as UNIQUE in table chemistry or (case-2) your FK should be the other way around, from table B to A (which already has a PRIMARY KEY on this compound).

    Which of the two is correct, depends on the relationships of your data:

    Does every chemistry have 0 or 1 chemistry_qc? That’s case-1 (and you have a 1:1 relationship or to be more exact a 0..1 :: 1).

    Does every chemistry_qc have 0 or more chemistry? That’s case-2 (and you have a common 1:n relationship, a 1 :: 0..n in the above notation).


    Finally, it should be clear that what caused the migration error was that the wizard you used, was (correctly) expecting a FOREIGN KEY constraint to reference a PRIMARY or a UNIQUE compound.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.