• <small id='Yv6Q0'></small><noframes id='Yv6Q0'>

      <bdo id='Yv6Q0'></bdo><ul id='Yv6Q0'></ul>
    <legend id='Yv6Q0'><style id='Yv6Q0'><dir id='Yv6Q0'><q id='Yv6Q0'></q></dir></style></legend>

      1. <i id='Yv6Q0'><tr id='Yv6Q0'><dt id='Yv6Q0'><q id='Yv6Q0'><span id='Yv6Q0'><b id='Yv6Q0'><form id='Yv6Q0'><ins id='Yv6Q0'></ins><ul id='Yv6Q0'></ul><sub id='Yv6Q0'></sub></form><legend id='Yv6Q0'></legend><bdo id='Yv6Q0'><pre id='Yv6Q0'><center id='Yv6Q0'></center></pre></bdo></b><th id='Yv6Q0'></th></span></q></dt></tr></i><div id='Yv6Q0'><tfoot id='Yv6Q0'></tfoot><dl id='Yv6Q0'><fieldset id='Yv6Q0'></fieldset></dl></div>
        <tfoot id='Yv6Q0'></tfoot>

        MySQL 无法添加外键约束

        时间:2023-08-17
          <tfoot id='HvsMa'></tfoot>
        1. <small id='HvsMa'></small><noframes id='HvsMa'>

          • <i id='HvsMa'><tr id='HvsMa'><dt id='HvsMa'><q id='HvsMa'><span id='HvsMa'><b id='HvsMa'><form id='HvsMa'><ins id='HvsMa'></ins><ul id='HvsMa'></ul><sub id='HvsMa'></sub></form><legend id='HvsMa'></legend><bdo id='HvsMa'><pre id='HvsMa'><center id='HvsMa'></center></pre></bdo></b><th id='HvsMa'></th></span></q></dt></tr></i><div id='HvsMa'><tfoot id='HvsMa'></tfoot><dl id='HvsMa'><fieldset id='HvsMa'></fieldset></dl></div>
          • <legend id='HvsMa'><style id='HvsMa'><dir id='HvsMa'><q id='HvsMa'></q></dir></style></legend>
                  <bdo id='HvsMa'></bdo><ul id='HvsMa'></ul>
                    <tbody id='HvsMa'></tbody>

                  本文介绍了MySQL 无法添加外键约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  所以我试图将外键约束作为项目要求添加到我的数据库中,它第一次或两次在不同的表上工作,但是我有两个表在尝试添加外键时出现错误约束.我得到的错误信息是:

                  So I'm trying to add Foreign Key constraints to my database as a project requirement and it worked the first time or two on different tables, but I have two tables on which I get an error when trying to add the Foreign Key Constraints. The error message that I get is:

                  ERROR 1215 (HY000): 无法添加外键约束

                  ERROR 1215 (HY000): Cannot add foreign key constraint

                  这是我用来创建表的 SQL,两个有问题的表是 PatientAppointment.

                  This is the SQL I'm using to create the tables, the two offending tables are Patient and Appointment.

                  SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
                  SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=1;
                  SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
                  
                  CREATE SCHEMA IF NOT EXISTS `doctorsoffice` DEFAULT CHARACTER SET utf8 ;
                  USE `doctorsoffice` ;
                  
                  -- -----------------------------------------------------
                  -- Table `doctorsoffice`.`doctor`
                  -- -----------------------------------------------------
                  DROP TABLE IF EXISTS `doctorsoffice`.`doctor` ;
                  
                  CREATE  TABLE IF NOT EXISTS `doctorsoffice`.`doctor` (
                    `DoctorID` INT(11) NOT NULL AUTO_INCREMENT ,
                    `FName` VARCHAR(20) NULL DEFAULT NULL ,
                    `LName` VARCHAR(20) NULL DEFAULT NULL ,
                    `Gender` VARCHAR(1) NULL DEFAULT NULL ,
                    `Specialty` VARCHAR(40) NOT NULL DEFAULT 'General Practitioner' ,
                    UNIQUE INDEX `DoctorID` (`DoctorID` ASC) ,
                    PRIMARY KEY (`DoctorID`) )
                  ENGINE = InnoDB
                  DEFAULT CHARACTER SET = utf8;
                  
                  
                  -- -----------------------------------------------------
                  -- Table `doctorsoffice`.`medicalhistory`
                  -- -----------------------------------------------------
                  DROP TABLE IF EXISTS `doctorsoffice`.`medicalhistory` ;
                  
                  CREATE  TABLE IF NOT EXISTS `doctorsoffice`.`medicalhistory` (
                    `MedicalHistoryID` INT(11) NOT NULL AUTO_INCREMENT ,
                    `Allergies` TEXT NULL DEFAULT NULL ,
                    `Medications` TEXT NULL DEFAULT NULL ,
                    `ExistingConditions` TEXT NULL DEFAULT NULL ,
                    `Misc` TEXT NULL DEFAULT NULL ,
                    UNIQUE INDEX `MedicalHistoryID` (`MedicalHistoryID` ASC) ,
                    PRIMARY KEY (`MedicalHistoryID`) )
                  ENGINE = InnoDB
                  DEFAULT CHARACTER SET = utf8;
                  
                  
                  -- -----------------------------------------------------
                  -- Table `doctorsoffice`.`Patient`
                  -- -----------------------------------------------------
                  DROP TABLE IF EXISTS `doctorsoffice`.`Patient` ;
                  
                  CREATE  TABLE IF NOT EXISTS `doctorsoffice`.`Patient` (
                    `PatientID` INT unsigned NOT NULL AUTO_INCREMENT ,
                    `FName` VARCHAR(30) NULL ,
                    `LName` VARCHAR(45) NULL ,
                    `Gender` CHAR NULL ,
                    `DOB` DATE NULL ,
                    `SSN` DOUBLE NULL ,
                    `MedicalHistory` smallint(5) unsigned NOT NULL,
                    `PrimaryPhysician` smallint(5) unsigned NOT NULL,
                    PRIMARY KEY (`PatientID`) ,
                    UNIQUE INDEX `PatientID_UNIQUE` (`PatientID` ASC) ,
                    CONSTRAINT `FK_MedicalHistory`
                      FOREIGN KEY (`MEdicalHistory` )
                      REFERENCES `doctorsoffice`.`medicalhistory` (`MedicalHistoryID` )
                      ON DELETE CASCADE
                      ON UPDATE CASCADE,
                    CONSTRAINT `FK_PrimaryPhysician`
                      FOREIGN KEY (`PrimaryPhysician` )
                      REFERENCES `doctorsoffice`.`doctor` (`DoctorID` )
                      ON DELETE CASCADE
                      ON UPDATE CASCADE)
                  ENGINE = InnoDB;
                  
                  
                  -- -----------------------------------------------------
                  -- Table `doctorsoffice`.`Appointment`
                  -- -----------------------------------------------------
                  DROP TABLE IF EXISTS `doctorsoffice`.`Appointment` ;
                  
                  CREATE  TABLE IF NOT EXISTS `doctorsoffice`.`Appointment` (
                    `AppointmentID` smallint(5) unsigned NOT NULL AUTO_INCREMENT ,
                    `Date` DATE NULL ,
                    `Time` TIME NULL ,
                    `Patient` smallint(5) unsigned NOT NULL,
                    `Doctor` smallint(5) unsigned NOT NULL,
                    PRIMARY KEY (`AppointmentID`) ,
                    UNIQUE INDEX `AppointmentID_UNIQUE` (`AppointmentID` ASC) ,
                    CONSTRAINT `FK_Patient`
                      FOREIGN KEY (`Patient` )
                      REFERENCES `doctorsoffice`.`Patient` (`PatientID` )
                      ON DELETE CASCADE
                      ON UPDATE CASCADE,
                    CONSTRAINT `FK_Doctor`
                      FOREIGN KEY (`Doctor` )
                      REFERENCES `doctorsoffice`.`doctor` (`DoctorID` )
                      ON DELETE CASCADE
                      ON UPDATE CASCADE)
                  ENGINE = InnoDB;
                  
                  
                  -- -----------------------------------------------------
                  -- Table `doctorsoffice`.`InsuranceCompany`
                  -- -----------------------------------------------------
                  DROP TABLE IF EXISTS `doctorsoffice`.`InsuranceCompany` ;
                  
                  CREATE  TABLE IF NOT EXISTS `doctorsoffice`.`InsuranceCompany` (
                    `InsuranceID` smallint(5) NOT NULL AUTO_INCREMENT ,
                    `Name` VARCHAR(50) NULL ,
                    `Phone` DOUBLE NULL ,
                    PRIMARY KEY (`InsuranceID`) ,
                    UNIQUE INDEX `InsuranceID_UNIQUE` (`InsuranceID` ASC) )
                  ENGINE = InnoDB;
                  
                  
                  -- -----------------------------------------------------
                  -- Table `doctorsoffice`.`PatientInsurance`
                  -- -----------------------------------------------------
                  DROP TABLE IF EXISTS `doctorsoffice`.`PatientInsurance` ;
                  
                  CREATE  TABLE IF NOT EXISTS `doctorsoffice`.`PatientInsurance` (
                    `PolicyHolder` smallint(5) NOT NULL ,
                    `InsuranceCompany` smallint(5) NOT NULL ,
                    `CoPay` INT NOT NULL DEFAULT 5 ,
                    `PolicyNumber` smallint(5) NOT NULL AUTO_INCREMENT ,
                    PRIMARY KEY (`PolicyNumber`) ,
                    UNIQUE INDEX `PolicyNumber_UNIQUE` (`PolicyNumber` ASC) ,
                    CONSTRAINT `FK_PolicyHolder`
                      FOREIGN KEY (`PolicyHolder` )
                      REFERENCES `doctorsoffice`.`Patient` (`PatientID` )
                      ON DELETE CASCADE
                      ON UPDATE CASCADE,
                    CONSTRAINT `FK_InsuranceCompany`
                      FOREIGN KEY (`InsuranceCompany` )
                      REFERENCES `doctorsoffice`.`InsuranceCompany` (`InsuranceID` )
                      ON DELETE CASCADE
                      ON UPDATE CASCADE)
                  ENGINE = InnoDB;
                  
                  USE `doctorsoffice` ;
                  
                  
                  SET SQL_MODE=@OLD_SQL_MODE;
                  SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
                  SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
                  

                  推荐答案

                  要查找特定错误,请运行:

                  To find the specific error run this:

                  SHOW ENGINE INNODB STATUS;
                  

                  并查看 LATEST FOREIGN KEY ERROR 部分.

                  子列的数据类型必须与父列完全匹配.例如,由于 medicalhistory.MedicalHistoryID 是一个 INTPatient.MedicalHistory 也需要是一个 INT,而不是一个 SMALLINT.

                  The data type for the child column must match the parent column exactly. For example, since medicalhistory.MedicalHistoryID is an INT, Patient.MedicalHistory also needs to be an INT, not a SMALLINT.

                  此外,您应该在运行 DDL 之前运行查询 set foreign_key_checks=0,这样您就可以按任意顺序创建表,而无需在相关子表之前创建所有父表.

                  Also, you should run the query set foreign_key_checks=0 before running the DDL so you can create the tables in an arbitrary order rather than needing to create all parent tables before the relevant child tables.

                  这篇关于MySQL 无法添加外键约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何使用一个 SQL 查询获得多个计数? 下一篇:如何在mysql中拆分名称字符串?

                  相关文章

                  最新文章

                • <tfoot id='8ic0p'></tfoot>
                  <i id='8ic0p'><tr id='8ic0p'><dt id='8ic0p'><q id='8ic0p'><span id='8ic0p'><b id='8ic0p'><form id='8ic0p'><ins id='8ic0p'></ins><ul id='8ic0p'></ul><sub id='8ic0p'></sub></form><legend id='8ic0p'></legend><bdo id='8ic0p'><pre id='8ic0p'><center id='8ic0p'></center></pre></bdo></b><th id='8ic0p'></th></span></q></dt></tr></i><div id='8ic0p'><tfoot id='8ic0p'></tfoot><dl id='8ic0p'><fieldset id='8ic0p'></fieldset></dl></div>
                  1. <legend id='8ic0p'><style id='8ic0p'><dir id='8ic0p'><q id='8ic0p'></q></dir></style></legend>
                      <bdo id='8ic0p'></bdo><ul id='8ic0p'></ul>

                    <small id='8ic0p'></small><noframes id='8ic0p'>