gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 07:30:10 25 4
gpt4 key购买 nike

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)
);

最佳答案

这可能是因为您正在尝试引用尚未创建的表。请重新排列 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)
);

关于mysql - #1215 - 无法添加外键约束 :/,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51751644/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com