所以我查看了所有内容,所有内容都设置为正确的数据类型,所有内容都被正确检查,但我有一些外键问题。 处方表不能使用错误1005和错误:150
这是脚本。
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
-- -----------------------------------------------------
-- Schema tks15
-- -----------------------------------------------------
DROP SCHEMA IF EXISTS `tks15` ;
-- -----------------------------------------------------
-- Schema tks15
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `tks15` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `tks15` ;
-- -----------------------------------------------------
-- Table `tks15`.`patient`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `tks15`.`patient` ;
CREATE TABLE IF NOT EXISTS `tks15`.`patient` (
`trt_id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '',
`trt_name` VARCHAR(255) NULL COMMENT '',
`trt_price` DECIMAL(5,2) NOT NULL COMMENT '',
`trt_notes` VARCHAR(255) NULL COMMENT '',
PRIMARY KEY (`trt_id`) COMMENT '')
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `tks15`.`medication`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `tks15`.`medication` ;
CREATE TABLE IF NOT EXISTS `tks15`.`medication` (
`med_id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '',
`med_name` VARCHAR(100) NOT NULL COMMENT '',
`med_price` DECIMAL(5,2) NOT NULL COMMENT '',
`med_shelf_life` VARCHAR(45) NOT NULL COMMENT '',
`med_notes` VARCHAR(255) NULL COMMENT '',
PRIMARY KEY (`med_id`) COMMENT '')
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `tks15`.`prescription`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `tks15`.`prescription` ;
CREATE TABLE IF NOT EXISTS `tks15`.`prescription` (
`pre_id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '',
`pat_id` SMALLINT UNSIGNED NOT NULL COMMENT '',
`med_id` SMALLINT UNSIGNED NOT NULL COMMENT '',
`pre_date` DATE NOT NULL COMMENT '',
`pre_dosage` VARCHAR(255) NOT NULL COMMENT '',
`pre_num_refills` VARCHAR(3) NOT NULL COMMENT '',
`pre_notes` VARCHAR(255) NULL COMMENT '',
PRIMARY KEY (`pre_id`) COMMENT '',
INDEX `fk_prescription_patient1_idx` (`pat_id` ASC) COMMENT '',
INDEX `fk_prescription_medication1_idx` (`med_id` ASC) COMMENT '',
CONSTRAINT `fk_prescription_patient1`
FOREIGN KEY (`pat_id`)
REFERENCES `tks15`.`patient` (`pat_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_prescription_medication1`
FOREIGN KEY (`med_id`)
REFERENCES `tks15`.`medication` (`med_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `tks15`.`patient`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `tks15`.`patient` ;
CREATE TABLE IF NOT EXISTS `tks15`.`patient` (
`trt_id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '',
`trt_name` VARCHAR(255) NULL COMMENT '',
`trt_price` DECIMAL(5,2) NOT NULL COMMENT '',
`trt_notes` VARCHAR(255) NULL COMMENT '',
PRIMARY KEY (`trt_id`) COMMENT '')
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `tks15`.`physician`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `tks15`.`physician` ;
CREATE TABLE IF NOT EXISTS `tks15`.`physician` (
`phy_id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '',
`phy_specialty` VARCHAR(25) NOT NULL COMMENT '',
`phy_fname` VARCHAR(15) NOT NULL COMMENT '',
`phy_lname` VARCHAR(30) NOT NULL COMMENT '',
`phy_street` VARCHAR(30) NOT NULL COMMENT '',
`phy_city` VARCHAR(20) NOT NULL COMMENT '',
`phy_state` VARCHAR(2) NOT NULL COMMENT '',
`phy_zip` INT UNSIGNED NOT NULL COMMENT '',
`phy_phone` BIGINT UNSIGNED NOT NULL COMMENT '',
`phy_fax` BIGINT UNSIGNED NOT NULL COMMENT '',
`phy_email` VARCHAR(100) NOT NULL COMMENT '',
`phy_url` VARCHAR(100) NOT NULL COMMENT '',
`phy_notes` VARCHAR(255) NULL COMMENT '',
PRIMARY KEY (`phy_id`) COMMENT '')
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `tks15`.`patient_treatment`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `tks15`.`patient_treatment` ;
CREATE TABLE IF NOT EXISTS `tks15`.`patient_treatment` (
`ptr_id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '',
`pat_id` SMALLINT UNSIGNED NOT NULL COMMENT '',
`phy_id` SMALLINT UNSIGNED NOT NULL COMMENT '',
`trt_id` SMALLINT UNSIGNED NOT NULL COMMENT '',
`ptr_date` DATE NOT NULL COMMENT '',
`ptr_start` TIME NOT NULL COMMENT '',
`ptr_end` TIME NOT NULL COMMENT '',
`ptr_results` VARCHAR(255) NOT NULL COMMENT '',
`ptr_notes` VARCHAR(255) NULL COMMENT '',
PRIMARY KEY (`ptr_id`) COMMENT '',
INDEX `fk_patient_treatment_patient_idx` (`trt_id` ASC) COMMENT '',
INDEX `fk_patient_treatment_physician1_idx` (`phy_id` ASC) COMMENT '',
INDEX `fk_patient_treatment_patient1_idx` (`pat_id` ASC) COMMENT '',
CONSTRAINT `fk_patient_treatment_patient`
FOREIGN KEY (`trt_id`)
REFERENCES `tks15`.`patient` (`trt_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_patient_treatment_physician1`
FOREIGN KEY (`phy_id`)
REFERENCES `tks15`.`physician` (`phy_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_patient_treatment_patient1`
FOREIGN KEY (`pat_id`)
REFERENCES `tks15`.`patient` (`pat_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `tks15`.`administration_lu`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `tks15`.`administration_lu` ;
CREATE TABLE IF NOT EXISTS `tks15`.`administration_lu` (
`pre_id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '',
`ptr_id` SMALLINT UNSIGNED NOT NULL COMMENT '',
PRIMARY KEY (`pre_id`, `ptr_id`) COMMENT '',
INDEX `fk_administration_lu_patient_treatment1_idx` (`ptr_id` ASC) COMMENT '',
CONSTRAINT `fk_administration_lu_prescription1`
FOREIGN KEY (`pre_id`)
REFERENCES `tks15`.`prescription` (`pre_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_administration_lu_patient_treatment1`
FOREIGN KEY (`ptr_id`)
REFERENCES `tks15`.`patient_treatment` (`ptr_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
-- -----------------------------------------------------
-- Data for table `tks15`.`patient`
-- -----------------------------------------------------
START TRANSACTION;
USE `tks15`;
INSERT INTO `tks15`.`patient` (`pat_id`, `pat_ssn`, `pat_fname`, `pat_lname`, `pat_street`, `pat_city`, `pat_state`, `pat_zip`, `pat_phone`, `pat_email`, `pat_dob`, `pat_gender`, `pat_notes`) VALUES (DEFAULT, 789456123, 'Olivia', 'Pope', '123 Thomas Jefferson Way', 'District of Columbia', 'DC', 10001, 7896554123, 'thefixer@opa.com', '1982-02-23', 'f', NULL);
INSERT INTO `tks15`.`patient` (`pat_id`, `pat_ssn`, `pat_fname`, `pat_lname`, `pat_street`, `pat_city`, `pat_state`, `pat_zip`, `pat_phone`, `pat_email`, `pat_dob`, `pat_gender`, `pat_notes`) VALUES (DEFAULT, 123456789, 'Phil', 'Coulson', '345 Playground Drive', 'New York', 'NY', 60001, 8506664512, 'phil@shield.net', '1977-01-11', 'm', NULL);
INSERT INTO `tks15`.`patient` (`pat_id`, `pat_ssn`, `pat_fname`, `pat_lname`, `pat_street`, `pat_city`, `pat_state`, `pat_zip`, `pat_phone`, `pat_email`, `pat_dob`, `pat_gender`, `pat_notes`) VALUES (DEFAULT, 987452134, 'Steve ', 'Rodgers', '1269 America Road', 'Arlington', 'VA', 54666, 8506978546, 'steve.rodgers@aol.com', '1939-07-14', 'm', NULL);
INSERT INTO `tks15`.`patient` (`pat_id`, `pat_ssn`, `pat_fname`, `pat_lname`, `pat_street`, `pat_city`, `pat_state`, `pat_zip`, `pat_phone`, `pat_email`, `pat_dob`, `pat_gender`, `pat_notes`) VALUES (DEFAULT, 546732146, 'Tony ', 'Stark', '800 Stark Industries Way', 'New York', 'NY', 60002, 8506548745, 'starkt@starkindustries.com', '1980-05-26', 'm', NULL);
INSERT INTO `tks15`.`patient` (`pat_id`, `pat_ssn`, `pat_fname`, `pat_lname`, `pat_street`, `pat_city`, `pat_state`, `pat_zip`, `pat_phone`, `pat_email`, `pat_dob`, `pat_gender`, `pat_notes`) VALUES (DEFAULT, 753654982, 'Peter ', 'Parker', '400 Oak Road', 'Queens', 'NY', 60005, 8506547892, 'parkerp@gmail.com', '1985-05-25', 'm', NULL);
COMMIT;
-- -----------------------------------------------------
-- Data for table `tks15`.`medication`
-- -----------------------------------------------------
START TRANSACTION;
USE `tks15`;
INSERT INTO `tks15`.`medication` (`med_id`, `med_name`, `med_price`, `med_shelf_life`, `med_notes`) VALUES (DEFAULT, 'Focalin XR', 80.99, '365 days', NULL);
INSERT INTO `tks15`.`medication` (`med_id`, `med_name`, `med_price`, `med_shelf_life`, `med_notes`) VALUES (DEFAULT, 'Sertraline', 45.99, '365 Days', NULL);
INSERT INTO `tks15`.`medication` (`med_id`, `med_name`, `med_price`, `med_shelf_life`, `med_notes`) VALUES (DEFAULT, 'Pamprin', 15.99, '90 Days', NULL);
INSERT INTO `tks15`.`medication` (`med_id`, `med_name`, `med_price`, `med_shelf_life`, `med_notes`) VALUES (DEFAULT, 'Oxycodone', 108.99, '30 Days', NULL);
INSERT INTO `tks15`.`medication` (`med_id`, `med_name`, `med_price`, `med_shelf_life`, `med_notes`) VALUES (DEFAULT, 'Dilaudid ', 35.99, '365 Days', NULL);
COMMIT;
-- -----------------------------------------------------
-- Data for table `tks15`.`prescription`
-- -----------------------------------------------------
START TRANSACTION;
USE `tks15`;
INSERT INTO `tks15`.`prescription` (`pre_id`, `pat_id`, `med_id`, `pre_date`, `pre_dosage`, `pre_num_refills`, `pre_notes`) VALUES (DEFAULT, DEFAULT, DEFAULT, '2015-11-30', '30 mg', '0', NULL);
INSERT INTO `tks15`.`prescription` (`pre_id`, `pat_id`, `med_id`, `pre_date`, `pre_dosage`, `pre_num_refills`, `pre_notes`) VALUES (DEFAULT, DEFAULT, DEFAULT, '2015-04-25', '45 mg', '6', NULL);
INSERT INTO `tks15`.`prescription` (`pre_id`, `pat_id`, `med_id`, `pre_date`, `pre_dosage`, `pre_num_refills`, `pre_notes`) VALUES (DEFAULT, DEFAULT, DEFAULT, '2014-12-23', '30 mg', '0', NULL);
INSERT INTO `tks15`.`prescription` (`pre_id`, `pat_id`, `med_id`, `pre_date`, `pre_dosage`, `pre_num_refills`, `pre_notes`) VALUES (DEFAULT, DEFAULT, DEFAULT, '2014-10-31', '60 mg', '2', NULL);
INSERT INTO `tks15`.`prescription` (`pre_id`, `pat_id`, `med_id`, `pre_date`, `pre_dosage`, `pre_num_refills`, `pre_notes`) VALUES (DEFAULT, DEFAULT, DEFAULT, '2014-01-15', '150 mg', '4', NULL);
COMMIT;
-- -----------------------------------------------------
-- Data for table `tks15`.`patient`
-- -----------------------------------------------------
START TRANSACTION;
USE `tks15`;
INSERT INTO `tks15`.`patient` (`trt_id`, `trt_name`, `trt_price`, `trt_notes`) VALUES (DEFAULT, 'Lobotomy', 365.23, NULL);
INSERT INTO `tks15`.`patient` (`trt_id`, `trt_name`, `trt_price`, `trt_notes`) VALUES (DEFAULT, 'Breast Enlargement', 1056.99, NULL);
INSERT INTO `tks15`.`patient` (`trt_id`, `trt_name`, `trt_price`, `trt_notes`) VALUES (DEFAULT, 'Colonoscopy', 408.32, NULL);
INSERT INTO `tks15`.`patient` (`trt_id`, `trt_name`, `trt_price`, `trt_notes`) VALUES (DEFAULT, 'Penile Implantation', 356.99, NULL);
INSERT INTO `tks15`.`patient` (`trt_id`, `trt_name`, `trt_price`, `trt_notes`) VALUES (DEFAULT, 'Radiation Ablation', 596.85, NULL);
COMMIT;
-- -----------------------------------------------------
-- Data for table `tks15`.`physician`
-- -----------------------------------------------------
START TRANSACTION;
USE `tks15`;
INSERT INTO `tks15`.`physician` (`phy_id`, `phy_specialty`, `phy_fname`, `phy_lname`, `phy_street`, `phy_city`, `phy_state`, `phy_zip`, `phy_phone`, `phy_fax`, `phy_email`, `phy_url`, `phy_notes`) VALUES (DEFAULT, 'Andrologists', 'Charles ', 'Xavier', '789 Higher Learning Way', 'Essex', 'VA', 31225, 6543235846, 6543238795, 'xavierc@ihl.edu', 'ihl.edu', NULL);
INSERT INTO `tks15`.`physician` (`phy_id`, `phy_specialty`, `phy_fname`, `phy_lname`, `phy_street`, `phy_city`, `phy_state`, `phy_zip`, `phy_phone`, `phy_fax`, `phy_email`, `phy_url`, `phy_notes`) VALUES (DEFAULT, 'Endocrinologists', 'Stephen ', 'Strange', '25 Bleeker Street', 'Greenwich', 'NY', 32001, 2314568746, 2314561234, 'stranges@occultmed.com', 'occultmed.com', NULL);
INSERT INTO `tks15`.`physician` (`phy_id`, `phy_specialty`, `phy_fname`, `phy_lname`, `phy_street`, `phy_city`, `phy_state`, `phy_zip`, `phy_phone`, `phy_fax`, `phy_email`, `phy_url`, `phy_notes`) VALUES (DEFAULT, 'Nuclear Medicine', 'Moira ', 'McTaggert', '789 Higher Learning Way', 'Essex', 'VA', 31225, 6543234597, 6543235687, 'McTaggertm@ihl.edu', 'ihl.edu', NULL);
INSERT INTO `tks15`.`physician` (`phy_id`, `phy_specialty`, `phy_fname`, `phy_lname`, `phy_street`, `phy_city`, `phy_state`, `phy_zip`, `phy_phone`, `phy_fax`, `phy_email`, `phy_url`, `phy_notes`) VALUES (DEFAULT, 'Anesthesaologists', 'Donald', 'Blake', '5648 Asgardian Avenue', 'Newark', 'NJ', 32006, 2154338975, 2154335646, 'blaked@pumc.org', 'pumc.org', NULL);
INSERT INTO `tks15`.`physician` (`phy_id`, `phy_specialty`, `phy_fname`, `phy_lname`, `phy_street`, `phy_city`, `phy_state`, `phy_zip`, `phy_phone`, `phy_fax`, `phy_email`, `phy_url`, `phy_notes`) VALUES (DEFAULT, 'Tropical Medicine', 'Claire', 'Temple', '4368 Fist Cage Circle', 'Bronx', 'NY', 32004, 5693214598, 5693214897, 'templec@gmail.com', 'none', NULL);
COMMIT;
-- -----------------------------------------------------
-- Data for table `tks15`.`patient_treatment`
-- -----------------------------------------------------
START TRANSACTION;
USE `tks15`;
INSERT INTO `tks15`.`patient_treatment` (`ptr_id`, `pat_id`, `phy_id`, `trt_id`, `ptr_date`, `ptr_start`, `ptr_end`, `ptr_results`, `ptr_notes`) VALUES (DEFAULT, DEFAULT, DEFAULT, DEFAULT, '2005-11-29', '09:00:00', '12:20:46', 'Success', NULL);
INSERT INTO `tks15`.`patient_treatment` (`ptr_id`, `pat_id`, `phy_id`, `trt_id`, `ptr_date`, `ptr_start`, `ptr_end`, `ptr_results`, `ptr_notes`) VALUES (DEFAULT, DEFAULT, DEFAULT, DEFAULT, '2009-01-23', '08:00:00', '09:10:12', 'Success', NULL);
INSERT INTO `tks15`.`patient_treatment` (`ptr_id`, `pat_id`, `phy_id`, `trt_id`, `ptr_date`, `ptr_start`, `ptr_end`, `ptr_results`, `ptr_notes`) VALUES (DEFAULT, DEFAULT, DEFAULT, DEFAULT, '2008-08-23', '07:00:00', '13:18:11', 'Failure', NULL);
INSERT INTO `tks15`.`patient_treatment` (`ptr_id`, `pat_id`, `phy_id`, `trt_id`, `ptr_date`, `ptr_start`, `ptr_end`, `ptr_results`, `ptr_notes`) VALUES (DEFAULT, DEFAULT, DEFAULT, DEFAULT, '2015-12-02', '10:15:23', '16:45:56', 'Failure', NULL);
INSERT INTO `tks15`.`patient_treatment` (`ptr_id`, `pat_id`, `phy_id`, `trt_id`, `ptr_date`, `ptr_start`, `ptr_end`, `ptr_results`, `ptr_notes`) VALUES (DEFAULT, DEFAULT, DEFAULT, DEFAULT, '2011-03-16', '11:30:01', '02:30:12', 'Success', NULL);
COMMIT;
答案 0 :(得分:1)
prescription
表上的约束有:
CONSTRAINT `fk_prescription_patient1`
FOREIGN KEY (`pat_id`)
REFERENCES `tks15`.`patient` (`pat_id`)
这是指pat_id
表中不存在的patient
列。你可能意味着那里的主键实际上叫做trt_id
。