0

I am a MySQL noob, very green. I downloaded the following code but got Foreign key constraint is incorrectly formed. I have been online for several hours trying to read and see if I can fix it with no luck. Any help will be appreciated. Here's a link to the whole database: https://mega.nz/file/Y55QCDQY#qm50oJiM5qs21HNgRQ3vYeFWYcymlOWf0UqOjpOGYug

   CREATE DATABASE /*!32312 IF NOT EXISTS*/`blood_bank` /*!40100 DEFAULT CHARACTER SET latin1 */;

USE `blood_bank`;


DROP TABLE IF EXISTS `blood_contact`;

CREATE TABLE `blood_contact` (
  `blood_contact_id` int(100) NOT NULL AUTO_INCREMENT,
  `contact_fk` int(100) DEFAULT NULL,
  `blood_fk` int(100) DEFAULT NULL,
  PRIMARY KEY (`blood_contact_id`),
  KEY `contact_fk` (`contact_fk`),
  KEY `blood_fk` (`blood_fk`),
  CONSTRAINT `blood_contact_ibfk_1` FOREIGN KEY (`contact_fk`) REFERENCES `contact` (`contact_id`) ON UPDATE CASCADE,
  CONSTRAINT `blood_contact_ibfk_2` FOREIGN KEY (`blood_fk`) REFERENCES `blood_group` (`blood_id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;




DROP TABLE IF EXISTS `blood_group`;

CREATE TABLE `blood_group` (
  `blood_id` int(100) NOT NULL AUTO_INCREMENT,
  `blood_group` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`blood_id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;

/*Data for the table `blood_group` */

insert  into `blood_group`(`blood_id`,`blood_group`) values 
(3,'sd'),
(5,'D#');



DROP TABLE IF EXISTS `contact`;

CREATE TABLE `contact` (
  `contact_id` int(100) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) DEFAULT NULL,
  `email` varchar(100) DEFAULT NULL,
  `address` varchar(200) DEFAULT NULL,
  `phone` varchar(100) DEFAULT NULL,
  `message` varchar(200) DEFAULT NULL,
  `member_fk` int(100) DEFAULT NULL,
  PRIMARY KEY (`contact_id`),
  KEY `member_fk` (`member_fk`),
  CONSTRAINT `contact_ibfk_1` FOREIGN KEY (`member_fk`) REFERENCES `member` (`member_id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Kuti
  • 1
  • The FOREIGN KEY constraints can't be created if the tables they reference do not already exist. `blood_contact` needs the `CREATE TABLE` statements for `contact` and `blood_group` to be executed _before_ it can be created. And it looks like `contact` also needs `member` to be created first. Rearrange the `CREATE TABLE` statements in your file accordingly and see if that gets you farther. – Michael Berkowski Jan 28 '21 at 21:20
  • 1
    Welcome to Stack Overflow by the way. I will add that the community here is not likely to go and download code from external sites with the rare exception of github. So it is best to include the most minimally complete relevant code needed to demonstrate your issue and don't bother with external links. – Michael Berkowski Jan 28 '21 at 21:25

0 Answers0