#1215-无法添加外键约束:/

时间:2018-08-08 16:37:25

标签: mysql

CREATE TABLE bill (
  `number` int(255) NOT NULL,
  `patient_id` int(255) DEFAULT NULL,
  `doctor_charge` varchar(100) NOT NULL,
  `medicine_charge` varchar(100) NOT NULL,
  `room_charge` varchar(100) NOT NULL,
  `nursing_charge` varchar(100) NOT NULL,
  `total_amount` varchar(255) NOT NULL,
    FOREIGN KEY (patient_id) REFERENCES patient (ID) ON DELETE RESTRICT ON UPDATE CASCADE,
    PRIMARY KEY (number)
);

CREATE TABLE daily_status (
  `ID` int(255) NOT NULL,
  `patient_id` int(255) DEFAULT NULL,
  `date` date NOT NULL,
  `medication` varchar(100) CHARACTER SET utf8 NOT NULL,
  `body_temp` varchar(100) CHARACTER SET utf8 NOT NULL,
  `blood_pressure` varchar(100) CHARACTER SET utf8 NOT NULL,
  `heart_beat` varchar(100) CHARACTER SET utf8 NOT NULL,
  `diabetes_level` varchar(100) CHARACTER SET utf8 NOT NULL,
  `oxygen_level` varchar(100) CHARACTER SET utf8 NOT NULL,
    FOREIGN KEY (patient_id) REFERENCES patient (ID) ON DELETE RESTRICT ON UPDATE CASCADE,
    PRIMARY KEY (ID)
);

CREATE TABLE `doctors` (
  `id` int(255) NOT NULL,
  `name` varchar(50) CHARACTER SET utf8 NOT NULL,
  `position` varchar(100) CHARACTER SET utf8 NOT NULL,
  `username` varchar(50) CHARACTER SET utf8 NOT NULL,
  `email` varchar(100) CHARACTER SET utf8 NOT NULL,
  `mob` varchar(50) CHARACTER SET utf8 NOT NULL,
  `password` varchar(50) CHARACTER SET utf8 NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


CREATE TABLE `guardian` (
  `id` int(255) NOT NULL,
  `name` varchar(50) CHARACTER SET utf8 NOT NULL,
  `position` varchar(100) CHARACTER SET utf8 NOT NULL,
  `username` varchar(50) CHARACTER SET utf8 NOT NULL,
  `email` varchar(100) CHARACTER SET utf8 NOT NULL,
  `mob` varchar(50) CHARACTER SET utf8 NOT NULL,
  `password` varchar(50) CHARACTER SET utf8 NOT NULL,
  `request` varchar(50) CHARACTER SET utf8 NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `nurses` (
  `id` int(255) NOT NULL,
  `name` varchar(50) CHARACTER SET utf8 NOT NULL,
  `position` varchar(100) CHARACTER SET utf8 NOT NULL,
  `username` varchar(50) CHARACTER SET utf8 NOT NULL,
  `email` varchar(100) CHARACTER SET utf8 NOT NULL,
  `mob` varchar(50) CHARACTER SET utf8 NOT NULL,
  `password` varchar(50) CHARACTER SET utf8 NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE patient (
  `ID` int(255) NOT NULL,
  `guard_id` int(255) DEFAULT NULL,
  `FullName` varchar(50) CHARACTER SET utf8 NOT NULL,
  `DOB` date NOT NULL,
  `sex` varchar(50) CHARACTER SET utf8 NOT NULL,
  `bloodType` varchar(50) CHARACTER SET utf8 NOT NULL,
  `phoneNO` varchar(100) CHARACTER SET utf8 NOT NULL,
  `dateIN` date NOT NULL,
  `dateOut` date NOT NULL,
  `viewstatus` varchar(50) NOT NULL,
  `requeststatus` varchar(50) CHARACTER SET utf8 NOT NULL,
    FOREIGN KEY (guard_id) REFERENCES guardian (id) ON DELETE RESTRICT ON UPDATE CASCADE,
    PRIMARY KEY (ID)
);

CREATE TABLE report (
  `ID` int(255) NOT NULL,
  `patient_id` int(255) DEFAULT NULL,
  `doctors_id` int(255) DEFAULT NULL,
  `upload_id` int(255) DEFAULT NULL,
  `number` int(255) NOT NULL,
  `description` varchar(300) CHARACTER SET utf8 NOT NULL,
  `source` varchar(100) CHARACTER SET utf8 NOT NULL,
  `date` date NOT NULL,
  `type` varchar(100) CHARACTER SET utf8 NOT NULL,
    FOREIGN KEY (patient_id) REFERENCES patient (ID) ON DELETE RESTRICT ON UPDATE CASCADE,
    FOREIGN KEY (doctors_id) REFERENCES doctors (id) ON DELETE RESTRICT ON UPDATE CASCADE,
    FOREIGN KEY (upload_id) REFERENCES upload (id) ON DELETE RESTRICT ON UPDATE CASCADE,
    PRIMARY KEY (ID)
);

  CREATE TABLE `usersidaccess` (
  `id` int(255) NOT NULL AUTO_INCREMENT,
   `name` varchar(150) NOT NULL,
  `position` varchar(15) NOT NULL,
  `username` varchar(100) NOT NULL,
  `email` varchar(50) NOT NULL,
  `mob` bigint(20) NOT NULL,
  `password` varchar(50) NOT NULL,
  `access` varchar(50) NOT NULL,
   PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `upload` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `name` varchar(255) NOT NULL,
  `date` datetime  NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;


ALTER TABLE `doctors`
  ADD PRIMARY KEY (`id`);


ALTER TABLE `guardian`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `nurses`
  ADD PRIMARY KEY (`id`);



ALTER TABLE `bill`
  MODIFY `number` int(255) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;

ALTER TABLE `daily_status`
  MODIFY `ID` int(255) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;


ALTER TABLE `doctors`
  MODIFY `id` int(255) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;


ALTER TABLE `guardian`
  MODIFY `id` int(255) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;


ALTER TABLE `nurses`
  MODIFY `id` int(255) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;


ALTER TABLE `patient`
  MODIFY `ID` int(255) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;


ALTER TABLE `report`
  MODIFY `ID` int(255) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;


CREATE TABLE doctor_patient (
    patient_id INTEGER NOT NULL,
    doctors_id INTEGER NOT NULL,
    FOREIGN KEY (patient_id) REFERENCES patient (ID) ON DELETE RESTRICT ON UPDATE CASCADE,
    FOREIGN KEY (doctors_id) REFERENCES doctors (id) ON DELETE RESTRICT ON UPDATE CASCADE,
    PRIMARY KEY (patient_id, doctors_id)
);


CREATE TABLE nurses_status (
    nurses_id INTEGER NOT NULL,
    status_id INTEGER NOT NULL,
    FOREIGN KEY (nurses_id) REFERENCES nurses (id) ON DELETE RESTRICT ON UPDATE CASCADE,
    FOREIGN KEY (status_id) REFERENCES daily_status (ID) ON DELETE RESTRICT ON UPDATE CASCADE,
    PRIMARY KEY ( nurses_id, status_id)
);

2 个答案:

答案 0 :(得分:1)

这可能是由于您试图引用尚未创建的表而发生的。请重新排列CREATE语句的顺序,以使那些没有任何外键的表排在最前面,然后依次排列其他不会干扰创建外键的表。

下面是解决方案。您的代码排序不正确,并且某些外键字段中的数据类型不同。

CREATE TABLE `doctors` (
  `id` int(255) NOT NULL,
  `name` varchar(50) CHARACTER SET utf8 NOT NULL,
  `position` varchar(100) CHARACTER SET utf8 NOT NULL,
  `username` varchar(50) CHARACTER SET utf8 NOT NULL,
  `email` varchar(100) CHARACTER SET utf8 NOT NULL,
  `mob` varchar(50) CHARACTER SET utf8 NOT NULL,
  `password` varchar(50) CHARACTER SET utf8 NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

ALTER TABLE `doctors`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `doctors`
  MODIFY `id` int(255) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;

CREATE TABLE `guardian` (
  `id` int(255) NOT NULL,
  `name` varchar(50) CHARACTER SET utf8 NOT NULL,
  `position` varchar(100) CHARACTER SET utf8 NOT NULL,
  `username` varchar(50) CHARACTER SET utf8 NOT NULL,
  `email` varchar(100) CHARACTER SET utf8 NOT NULL,
  `mob` varchar(50) CHARACTER SET utf8 NOT NULL,
  `password` varchar(50) CHARACTER SET utf8 NOT NULL,
  `request` varchar(50) CHARACTER SET utf8 NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

ALTER TABLE `guardian`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `guardian`
  MODIFY `id` int(255) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;

CREATE TABLE `nurses` (
  `id` int(255) NOT NULL,
  `name` varchar(50) CHARACTER SET utf8 NOT NULL,
  `position` varchar(100) CHARACTER SET utf8 NOT NULL,
  `username` varchar(50) CHARACTER SET utf8 NOT NULL,
  `email` varchar(100) CHARACTER SET utf8 NOT NULL,
  `mob` varchar(50) CHARACTER SET utf8 NOT NULL,
  `password` varchar(50) CHARACTER SET utf8 NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

ALTER TABLE `nurses`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `nurses`
  MODIFY `id` int(255) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;

  CREATE TABLE `usersidaccess` (
  `id` int(255) NOT NULL AUTO_INCREMENT,
   `name` varchar(150) NOT NULL,
  `position` varchar(15) NOT NULL,
  `username` varchar(100) NOT NULL,
  `email` varchar(50) NOT NULL,
  `mob` bigint(20) NOT NULL,
  `password` varchar(50) NOT NULL,
  `access` varchar(50) NOT NULL,
   PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `upload` (
  `id` int(255) NOT NULL auto_increment,
  `name` varchar(255) NOT NULL,
  `date` datetime  NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;

CREATE TABLE patient (
  `ID` int(255) NOT NULL,
  `guard_id` int(255) DEFAULT NULL,
  `FullName` varchar(50) CHARACTER SET utf8 NOT NULL,
  `DOB` date NOT NULL,
  `sex` varchar(50) CHARACTER SET utf8 NOT NULL,
  `bloodType` varchar(50) CHARACTER SET utf8 NOT NULL,
  `phoneNO` varchar(100) CHARACTER SET utf8 NOT NULL,
  `dateIN` date NOT NULL,
  `dateOut` date NOT NULL,
  `viewstatus` varchar(50) NOT NULL,
  `requeststatus` varchar(50) CHARACTER SET utf8 NOT NULL,
    FOREIGN KEY (guard_id) REFERENCES guardian (id) ON DELETE RESTRICT ON UPDATE CASCADE,
    PRIMARY KEY (ID)
);

ALTER TABLE `patient`
  MODIFY `ID` int(255) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;

CREATE TABLE bill (
  `number` int(255) NOT NULL,
  `patient_id` int(255) DEFAULT NULL,
  `doctor_charge` varchar(100) NOT NULL,
  `medicine_charge` varchar(100) NOT NULL,
  `room_charge` varchar(100) NOT NULL,
  `nursing_charge` varchar(100) NOT NULL,
  `total_amount` varchar(255) NOT NULL,
    FOREIGN KEY (patient_id) REFERENCES patient (ID) ON DELETE RESTRICT ON UPDATE CASCADE,
    PRIMARY KEY (number)
);

ALTER TABLE `bill`
  MODIFY `number` int(255) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;

CREATE TABLE daily_status (
  `ID` int(255) NOT NULL,
  `patient_id` int(255) DEFAULT NULL,
  `date` date NOT NULL,
  `medication` varchar(100) CHARACTER SET utf8 NOT NULL,
  `body_temp` varchar(100) CHARACTER SET utf8 NOT NULL,
  `blood_pressure` varchar(100) CHARACTER SET utf8 NOT NULL,
  `heart_beat` varchar(100) CHARACTER SET utf8 NOT NULL,
  `diabetes_level` varchar(100) CHARACTER SET utf8 NOT NULL,
  `oxygen_level` varchar(100) CHARACTER SET utf8 NOT NULL,
    FOREIGN KEY (patient_id) REFERENCES patient (ID) ON DELETE RESTRICT ON UPDATE CASCADE,
    PRIMARY KEY (ID)
);

ALTER TABLE `daily_status`
  MODIFY `ID` int(255) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;

CREATE TABLE report (
  `ID` int(255) NOT NULL,
  `patient_id` int(255) DEFAULT NULL,
  `doctors_id` int(255) DEFAULT NULL,
  `upload_id` int(255) DEFAULT NULL,
  `number` int(255) NOT NULL,
  `description` varchar(300) CHARACTER SET utf8 NOT NULL,
  `source` varchar(100) CHARACTER SET utf8 NOT NULL,
  `date` date NOT NULL,
  `type` varchar(100) CHARACTER SET utf8 NOT NULL,
    FOREIGN KEY (patient_id) REFERENCES patient (ID) ON DELETE RESTRICT ON UPDATE CASCADE,
    FOREIGN KEY (doctors_id) REFERENCES doctors (id) ON DELETE RESTRICT ON UPDATE CASCADE,
    PRIMARY KEY (ID)
);

ALTER TABLE `report`
  MODIFY `ID` int(255) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;

  ALTER TABLE `report`
    ADD CONSTRAINT upload_fk FOREIGN KEY (upload_id) REFERENCES upload (id) ON DELETE RESTRICT ON UPDATE CASCADE;

CREATE TABLE doctor_patient (
    patient_id INTEGER NOT NULL,
    doctors_id INTEGER NOT NULL,
    FOREIGN KEY (patient_id) REFERENCES patient (ID) ON DELETE RESTRICT ON UPDATE CASCADE,
    FOREIGN KEY (doctors_id) REFERENCES doctors (id) ON DELETE RESTRICT ON UPDATE CASCADE,
    PRIMARY KEY (patient_id, doctors_id)
);


CREATE TABLE nurses_status (
    nurses_id INTEGER NOT NULL,
    status_id INTEGER NOT NULL,
    FOREIGN KEY (nurses_id) REFERENCES nurses (id) ON DELETE RESTRICT ON UPDATE CASCADE,
    FOREIGN KEY (status_id) REFERENCES daily_status (ID) ON DELETE RESTRICT ON UPDATE CASCADE,
    PRIMARY KEY ( nurses_id, status_id)
);

答案 1 :(得分:0)

  • 。在表bill中,您的参考表是patient,当您执行crete表bill时,它不会创建,因此返回 错误。
  • 创建表daily_status时发生了同样的事情
  • 在表reports中,您使用了引用upload表,该表是您稍后创建的,因此也会引发错误

因此已经建议使用@brick的订单表创建语句