can't create table in mysql because of foriegn key constraint - mysql

i am trying to create the codedata table in mysql but get error #1215 - Cannot add foreign key constraint. can someone please help me figure out what is wrong? thanks in advance for your help!
here is the code that doesn't work:
DROP TABLE IF EXISTS `interviewcodes`.`codedata` ;
CREATE TABLE IF NOT EXISTS `interviewcodes`.`codedata` (
`StudyID` INT(11) NOT NULL,
`ParticipantID` INT(11) NOT NULL,
`CoderID` INT(11) NOT NULL,
`CodingMonth` INT(11) NOT NULL,
`CodingDay` INT(11) NOT NULL,
`CodingYear` INT(11) NOT NULL,
`StudyQuestionLabel` VARCHAR(45) NOT NULL,
`StudyQuestionResponse` VARCHAR(245) NULL,
`IWAcode` INT(11) NULL DEFAULT 0,
`CQcode` INT(11) NULL DEFAULT 0,
`CRcode` INT(11) NULL DEFAULT 0,
`PMinusCode` INT(11) NULL DEFAULT 0,
`PPlusCode` INT(11) NULL DEFAULT 0,
`PROcode` INT(11) NULL DEFAULT 0,
`CONcode` INT(11) NULL DEFAULT 0,
`RELcode` INT(11) NULL DEFAULT 0,
`NOAcode` INT(11) NULL DEFAULT 0,
`OTHcode` INT(11) NULL DEFAULT 0,
`TotalScore` INT(11) NULL DEFAULT 0,
`Remark` VARCHAR(5000) NULL DEFAULT NULL,
INDEX `fk_CodeData_Participant1_idx` (`ParticipantID` ASC),
INDEX `fk_CodeData_StudyCoders1_idx` (`CoderID` ASC),
INDEX `fk_codedata_studyquestion1_idx` (`StudyQuestionLabel` ASC),
PRIMARY KEY (`StudyID`, `ParticipantID`, `CoderID`, `StudyQuestionLabel`),
CONSTRAINT `fk_CodeData_Participant1`
FOREIGN KEY (`ParticipantID`)
REFERENCES `interviewcodes`.`participant` (`ParticipantID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_CodeData_StudyCoders1`
FOREIGN KEY (`CoderID`)
REFERENCES `interviewcodes`.`studycoders` (`CoderID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_codedata_studylkup1`
FOREIGN KEY (`StudyID`)
REFERENCES `interviewcodes`.`studylkup` (`StudyID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_codedata_studyquestion1`
FOREIGN KEY (`StudyQuestionLabel`)
REFERENCES `interviewcodes`.`studyquestion` (`StudyQuestionLabel`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
here is the code i used to create the other tables, which ran correctly:
DROP TABLE IF EXISTS `interviewcodes`.`interviewerlkup` ;
CREATE TABLE IF NOT EXISTS `interviewcodes`.`interviewerlkup` (
`InterviewerID` INT(11) NOT NULL AUTO_INCREMENT,
`InterviewerFirstName` VARCHAR(45) NOT NULL,
`InterviewerLastName` VARCHAR(45) NOT NULL,
PRIMARY KEY (`InterviewerID`))
ENGINE = InnoDB
AUTO_INCREMENT = 3
DEFAULT CHARACTER SET = utf8;
DROP TABLE IF EXISTS `interviewcodes`.`studylkup` ;
CREATE TABLE IF NOT EXISTS `interviewcodes`.`studylkup` (
`StudyID` INT(11) NOT NULL AUTO_INCREMENT,
`StudyName` VARCHAR(45) NOT NULL,
`StudyPIFirstName` VARCHAR(45) NULL,
`StudyPILastName` VARCHAR(45) NULL,
`StudyStartMonth` INT(11) NOT NULL,
`StudyStartDay` INT(11) NOT NULL,
`StudyStartYear` INT(11) NOT NULL,
PRIMARY KEY (`StudyID`))
ENGINE = InnoDB
AUTO_INCREMENT = 4
DEFAULT CHARACTER SET = utf8;
DROP TABLE IF EXISTS `interviewcodes`.`studyinterviewers` ;
CREATE TABLE IF NOT EXISTS `interviewcodes`.`studyinterviewers` (
`StudyID` INT(11) NOT NULL,
`InterviewerID` INT(11) NOT NULL,
PRIMARY KEY (`StudyID`, `InterviewerID`),
INDEX `fk_StudyInterviewers_InterviewerLkup1_idx` (`InterviewerID` ASC),
CONSTRAINT `fk_StudyInterviewers_InterviewerLkup1`
FOREIGN KEY (`InterviewerID`)
REFERENCES `interviewcodes`.`interviewerlkup` (`InterviewerID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_StudyInterviewers_StudyLkup1`
FOREIGN KEY (`StudyID`)
REFERENCES `interviewcodes`.`studylkup` (`StudyID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
DROP TABLE IF EXISTS `interviewcodes`.`participant` ;
CREATE TABLE IF NOT EXISTS `interviewcodes`.`participant` (
`ParticipantID` INT(11) NOT NULL AUTO_INCREMENT,
`ParticipantCaseID` VARCHAR(45) NOT NULL,
`StudyID` INT(11) NOT NULL,
`InterviewerID` INT(11) NOT NULL,
`InterviewMonth` INT(11) NULL DEFAULT NULL,
`InterviewDay` INT(11) NULL DEFAULT NULL,
`InterviewYear` INT(11) NULL DEFAULT NULL,
PRIMARY KEY (`ParticipantID`),
INDEX `fk_participant_studyinterviewers1_idx` (`InterviewerID` ASC),
CONSTRAINT `fk_participant_studyinterviewers1`
FOREIGN KEY (`InterviewerID`)
REFERENCES `interviewcodes`.`studyinterviewers` (`InterviewerID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_participant_studylkup1`
FOREIGN KEY (`StudyID`)
REFERENCES `interviewcodes`.`studylkup` (`StudyID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
AUTO_INCREMENT = 6
DEFAULT CHARACTER SET = utf8;
DROP TABLE IF EXISTS `interviewcodes`.`coderlkup` ;
CREATE TABLE IF NOT EXISTS `interviewcodes`.`coderlkup` (
`CoderID` INT(11) NOT NULL AUTO_INCREMENT,
`CoderFirstName` VARCHAR(45) NOT NULL,
`CoderLastName` VARCHAR(45) NOT NULL,
PRIMARY KEY (`CoderID`))
ENGINE = InnoDB
AUTO_INCREMENT = 3
DEFAULT CHARACTER SET = utf8;
DROP TABLE IF EXISTS `interviewcodes`.`studycoders` ;
CREATE TABLE IF NOT EXISTS `interviewcodes`.`studycoders` (
`StudyID` INT(11) NOT NULL,
`CoderID` INT(11) NOT NULL,
PRIMARY KEY (`StudyID`, `CoderID`),
INDEX `fk_StudyCoders_CoderLkup1_idx` (`CoderID` ASC),
CONSTRAINT `fk_StudyCoders_CoderLkup1`
FOREIGN KEY (`CoderID`)
REFERENCES `interviewcodes`.`coderlkup` (`CoderID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_StudyCoders_StudyLkup1`
FOREIGN KEY (`StudyID`)
REFERENCES `interviewcodes`.`studylkup` (`StudyID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
DROP TABLE IF EXISTS `interviewcodes`.`studyquestion` ;
CREATE TABLE IF NOT EXISTS `interviewcodes`.`studyquestion` (
`StudyID` INT(11) NOT NULL,
`StudyQuestionLabel` VARCHAR(45) NOT NULL,
PRIMARY KEY (`StudyID`, `StudyQuestionLabel`),
CONSTRAINT `fk_StudyQuestion_StudyLkup`
FOREIGN KEY (`StudyID`)
REFERENCES `interviewcodes`.`studylkup` (`StudyID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
here is the last part of my code, after i try to create the codedata table:
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;
ALTER IGNORE TABLE `StudyLkup` ADD UNIQUE INDEX(`StudyName`);
ALTER IGNORE TABLE `InterviewerLkup` ADD UNIQUE INDEX(`InterviewerFirstName`, `InterviewerLastName`);
ALTER IGNORE TABLE `CoderLkup` ADD UNIQUE INDEX(`CoderFirstName`, `CoderLastName`);
ALTER IGNORE TABLE `Participant` ADD UNIQUE INDEX(`ParticipantCaseID`, `StudyID`);
ALTER IGNORE TABLE `StudyCoders` ADD UNIQUE INDEX(`StudyID`, `CoderID`);
ALTER IGNORE TABLE `StudyInterviewers` ADD UNIQUE INDEX(`StudyID`, `InterviewerID`);
ALTER IGNORE TABLE `StudyQuestion` ADD UNIQUE INDEX(`StudyID`, `StudyQuestionLabel`);
ALTER IGNORE TABLE `CodeData` ADD UNIQUE INDEX(`StudyID`, `ParticipantID`, `CoderID`, `StudyQuestionLabel`);

It will work if you perform this:
CREATE TABLE IF NOT EXISTS `interviewcodes`.`studyquestion` (
`StudyID` INT(11) NOT NULL,
`StudyQuestionLabel` VARCHAR(45) NOT NULL,
PRIMARY KEY (`StudyID`, `StudyQuestionLabel`),
key(`StudyQuestionLabel`), -- <-------- I added this
CONSTRAINT `fk_StudyQuestion_StudyLkup`
FOREIGN KEY (`StudyID`)
REFERENCES `interviewcodes`.`studylkup` (`StudyID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8;
Reason: Left-most issue on StudyQuestionLabel is lacking.
Though StudyQuestionLabel is in a composite key it your code, it is not left-most.
Note that I created an interviewcodes schema and tested it.
From the Manual Page Using FOREIGN KEY Constraints a quote:
... In the referencing table, there must be an index where the foreign
key columns are listed as the first columns in the same order.

Related

MySQL Workbench Foreign key constraint is incorrectly formed

I have been fiddeling with MySQL Workbench as a learning project and tried to create a table with foreign keys.
The Keys reference other tables in my database.
The ID's which are beeing referenced are all of the same type "INT(11)".
All the ID's are primary Keys and are not null.
Yet I still cant forward Engineer my Database, and the following error is beeing thrown: "ERROR: Error 1005: Can't create table testcrm.role_user (errno: 150 "Foreign key constraint is incorrectly formed")"
-- Table `testcrm`.`role_user`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `testcrm`.`role_user` (
`role_id` INT(11) UNSIGNED NOT NULL,
`user_id` INT(11) UNSIGNED NOT NULL,
PRIMARY KEY (`role_id`, `user_id`),
INDEX `user_id_idx` (`user_id` ASC),
CONSTRAINT `role_user_role_id`
FOREIGN KEY (`role_id`)
REFERENCES `testcrm`.`roles` (`id`)
ON DELETE CASCADE
ON UPDATE RESTRICT,
CONSTRAINT `role_user_user_id`
FOREIGN KEY (`user_id`)
REFERENCES `testcrm`.`users` (`id`)
ON DELETE CASCADE
ON UPDATE RESTRICT)
ENGINE = InnoDB
Edit: Those are my Users and Roles Tables:
-- -----------------------------------------------------
-- Table `testcrm`.`roles`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `testcrm`.`roles` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(191) NULL DEFAULT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_unicode_ci;
-- -----------------------------------------------------
-- Table `testcrm`.`users`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `testcrm`.`users` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(191) NOT NULL,
`email` VARCHAR(191) NOT NULL,
`email_verified_at` TIMESTAMP NULL DEFAULT NULL,
`password` VARCHAR(191) NOT NULL,
`remember_token` VARCHAR(100) NULL DEFAULT NULL,
`created_at` TIMESTAMP NULL DEFAULT NULL,
`updated_at` TIMESTAMP NULL DEFAULT NULL,
UNIQUE INDEX `users_email_unique` (`email` ASC),
PRIMARY KEY (`id`))
ENGINE = InnoDB
AUTO_INCREMENT = 2
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_unicode_ci;

Foreign Key Constraint - (ERROR 1215). Please assist:)

I have looked at other questions on here and can't seem to find the answer I am looking for. I am attempting to make a SQL database with AUTO_INCREMENT set against the ID of each table. I have matched the data types from the foreign ID of a table to the primary key of a table. The errors occur on the following tables (only the ones that have foreign keys): NUMBERS, CUSTOMER, TRUNK, TRUNK_GROUP
The error received on these tables is:
ERROR 1215 (HY000): Cannot add foreign key constraint
Below is the code used. Wonder if anyone has any suggestions?
CREATE DATABASE IF NOT EXISTS `NOAS_DATABASE` DEFAULT CHARACTER SET utf8 ;
USE `NOAS_DATABASE` ;
-- -----------------------------------------------------
-- Table `NOAS_DATABASE`.`IP_ADDRESSES`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `NOAS_DATABASE`.`IP_ADDRESSES` (
`IP_ID` INT(10) NOT NULL AUTO_INCREMENT,
`START_IP_RANGE` LONGBLOB NULL,
`END_IP_RANGE` LONGBLOB NULL,
PRIMARY KEY (`IP_ID`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `NOAS_DATABASE`.`NO_RANGE`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `NOAS_DATABASE`.`NO_RANGE` (
`RANGE_ID` INT(10) NOT NULL AUTO_INCREMENTL,
`START_NO_RANGE` LONGBLOB NULL,
`END_NO_RANGE` LONGBLOB NULL,
PRIMARY KEY (`RANGE_ID`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `NOAS_DATABASE`.`NUMBERS`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `NOAS_DATABASE`.`NUMBERS` (
`NUM_ID` INT(10) NOT NULL AUTO_INCREMENT,
`IP_ID` VARCHAR(20) NULL,
`RANGE_ID` VARCHAR(20) NULL,
`CALL_BARRING_STATUS` TEXT(10) NULL,
`ANONYMOUS_CALL_REJECT` TEXT(20) NULL,
`CALL_DIVERT` TEXT(20) NULL,
`CALL_DIVERT_DEST_NO` LONGBLOB NULL,
PRIMARY KEY (`NUM_ID`),
INDEX `IP_ID_idx` (`IP_ID` ASC) VISIBLE,
INDEX `RANGE_ID_idx` (`RANGE_ID` ASC) VISIBLE,
CONSTRAINT `IP_ID`
FOREIGN KEY (`IP_ID`)
REFERENCES `NOAS_DATABASE`.`IP_ADDRESSES` (`IP_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `RANGE_ID`
FOREIGN KEY (`RANGE_ID`)
REFERENCES `NOAS_DATABASE`.`NO_RANGE` (`RANGE_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `NOAS_DATABASE`.`SERVICE`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `NOAS_DATABASE`.`SERVICE` (
`SERVICE_ID` INT(10) NOT NULL AUTO_INCREMENT,
`SERVICE_STATUS` VARCHAR(20) NULL,
`DOMAIN_NAME` LONGBLOB NULL,
PRIMARY KEY (`SERVICE_ID`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `NOAS_DATABASE`.`CUSTOMER`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `NOAS_DATABASE`.`CUSTOMER` (
`CUSTOMER_ID` INT(10) NOT NULL AUTO_INCREMENT,
`CUST_NETWORK_SET` TEXT(20) NULL,
`BILLING_ID` TEXT(20) NULL,
`LOCATION` TEXT(20) NULL,
`SERVICE_ID` VARCHAR(20) NULL,
PRIMARY KEY (`CUSTOMER_ID`),
INDEX `SERVICE_ID_idx` (`SERVICE_ID` ASC) VISIBLE,
CONSTRAINT `SERVICE_ID`
FOREIGN KEY (`SERVICE_ID`)
REFERENCES `NOAS_DATABASE`.`SERVICE` (`SERVICE_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `NOAS_DATABASE`.`NETWORK_SET`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `NOAS_DATABASE`.`NETWORK_SET` (
`NETWORK_SET_OSS_ID` INT(10) NOT NULL AUTO_INCREMENT,
`PRIORITY_NOAS` TEXT(20) NULL,
`PRIORITY_SBC` TEXT(20) NULL,
PRIMARY KEY (`NETWORK_SET_OSS_ID`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `NOAS_DATABASE`.`TRUNK_GROUP`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `NOAS_DATABASE`.`TRUNK_GROUP` (
`TRUNK_GROUP_ID` INT(10) NOT NULL AUTO_INCREMENT,
`CUSTOMER_ID` VARCHAR(20) NULL,
`TRUNK_ID` VARCHAR(20) NULL,
PRIMARY KEY (`TRUNK_GROUP_ID`),
INDEX `CUSTOMER_ID_idx` (`CUSTOMER_ID` ASC) VISIBLE,
INDEX `TRUNK_ID_idx` (`TRUNK_ID` ASC) VISIBLE,
CONSTRAINT `CUSTOMER_ID`
FOREIGN KEY (`CUSTOMER_ID`)
REFERENCES `NOAS_DATABASE`.`CUSTOMER` (`CUSTOMER_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `TRUNK_ID`
FOREIGN KEY (`TRUNK_ID`)
REFERENCES `NOAS_DATABASE`.`TRUNK` (`TRUNK_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `NOAS_DATABASE`.`TRUNK`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `NOAS_DATABASE`.`TRUNK` (
`TRUNK_ID` INT(10) NOT NULL AUTO_INCREMENT,
`TRUNK_GROUP_ID` VARCHAR(20) NULL,
`NETWORK_SET_OSS_ID` VARCHAR(20) NULL,
`NUM_ID` VARCHAR(20) NULL,
`TRUNK_SERVICE_STATUS` TEXT(20) NULL,
`TRUNK_GROUP_PRIORITY` TEXT(20) NULL,
`TRUNK_CAC_LIMIT` TEXT(20) NULL,
`HANDOVER_FORMAT` TEXT(20) NULL,
PRIMARY KEY (`TRUNK_ID`),
INDEX `NETWORK_SET_OSS_ID_idx` (`NETWORK_SET_OSS_ID` ASC) VISIBLE,
INDEX `NUM_ID_idx` (`NUM_ID` ASC) VISIBLE,
INDEX `TRUNK_GROUP_ID_idx` (`TRUNK_GROUP_ID` ASC) VISIBLE,
CONSTRAINT `NETWORK_SET_OSS_ID`
FOREIGN KEY (`NETWORK_SET_OSS_ID`)
REFERENCES `NOAS_DATABASE`.`NETWORK_SET` (`NETWORK_SET_OSS_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `NUM_ID`
FOREIGN KEY (`NUM_ID`)
REFERENCES `NOAS_DATABASE`.`NUMBERS` (`NUM_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `TRUNK_GROUP_ID`
FOREIGN KEY (`TRUNK_GROUP_ID`)
REFERENCES `NOAS_DATABASE`.`TRUNK_GROUP` (`TRUNK_GROUP_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
There were minor issues in multiple tables. Here're some corrections:
Switch to the right database
USE `NOAS_DATABASE` ;
IP Address table is just fine
CREATE TABLE IF NOT EXISTS `IP_ADDRESSES` (
`IP_ID` INT(10) NOT NULL AUTO_INCREMENT,
`START_IP_RANGE` LONGBLOB NULL,
`END_IP_RANGE` LONGBLOB NULL,
PRIMARY KEY (`IP_ID`))
ENGINE = InnoDB;
No Range table
There was a type in AUTO_INCREMENT as commentor Dan mentioned. Below is the corrected version.
CREATE TABLE IF NOT EXISTS `NO_RANGE` (
`RANGE_ID` INT(10) NOT NULL AUTO_INCREMENT, -- fixed typo
`START_NO_RANGE` LONGBLOB NULL,
`END_NO_RANGE` LONGBLOB NULL,
PRIMARY KEY (`RANGE_ID`))
ENGINE = InnoDB;
Numbers had datatype issues
IP_ID and Range_ID should be INT. That's been corrected as follows.
CREATE TABLE IF NOT EXISTS `NUMBERS` (
`NUM_ID` INT(10) NOT NULL AUTO_INCREMENT,
`IP_ID` INT(10) NULL, -- fixed datatype
`RANGE_ID` INT(10) NULL, -- fixed datatype
`CALL_BARRING_STATUS` TEXT(10) NULL,
`ANONYMOUS_CALL_REJECT` TEXT(20) NULL,
`CALL_DIVERT` TEXT(20) NULL,
`CALL_DIVERT_DEST_NO` LONGBLOB NULL,
PRIMARY KEY (`NUM_ID`),
INDEX `IP_ID_idx` (`IP_ID` ASC),
INDEX `RANGE_ID_idx` (`RANGE_ID` ASC),
CONSTRAINT `IP_ID`
FOREIGN KEY (`IP_ID`)
REFERENCES `IP_ADDRESSES` (`IP_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `RANGE_ID`
FOREIGN KEY (`RANGE_ID`)
REFERENCES `NO_RANGE` (`RANGE_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Service table is good
CREATE TABLE IF NOT EXISTS `SERVICE` (
`SERVICE_ID` INT(10) NOT NULL AUTO_INCREMENT,
`SERVICE_STATUS` VARCHAR(20) NULL,
`DOMAIN_NAME` LONGBLOB NULL,
PRIMARY KEY (`SERVICE_ID`))
ENGINE = InnoDB;
Customer table had datatype issue
Service_ID should be an INT. Corrected below.
CREATE TABLE IF NOT EXISTS `CUSTOMER` (
`CUSTOMER_ID` INT(10) NOT NULL AUTO_INCREMENT,
`CUST_NETWORK_SET` TEXT(20) NULL,
`BILLING_ID` TEXT(20) NULL,
`LOCATION` TEXT(20) NULL,
`SERVICE_ID` INT(10) NULL, -- fixed datatype
PRIMARY KEY (`CUSTOMER_ID`),
INDEX `SERVICE_ID_idx` (`SERVICE_ID` ASC) ,
CONSTRAINT `SERVICE_ID`
FOREIGN KEY (`SERVICE_ID`)
REFERENCES `SERVICE` (`SERVICE_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Network set is good
CREATE TABLE IF NOT EXISTS `NETWORK_SET` (
`NETWORK_SET_OSS_ID` INT(10) NOT NULL AUTO_INCREMENT,
`PRIORITY_NOAS` TEXT(20) NULL,
`PRIORITY_SBC` TEXT(20) NULL,
PRIMARY KEY (`NETWORK_SET_OSS_ID`))
ENGINE = InnoDB;
Trunk group had reference to a table that wasn't yet created
Trunk table was not created yet and Trunk_Group was referring to it. Removed that reference. Consider the possibility of removing trunk_id from trunk_group field.
CREATE TABLE IF NOT EXISTS `TRUNK_GROUP` (
`TRUNK_GROUP_ID` INT(10) NOT NULL AUTO_INCREMENT,
`CUSTOMER_ID` INT(10) NULL,
`TRUNK_ID` INT(10) NULL, -- consider removing this field
PRIMARY KEY (`TRUNK_GROUP_ID`),
INDEX `CUSTOMER_ID_idx` (`CUSTOMER_ID` ASC) ,
INDEX `TRUNK_ID_idx` (`TRUNK_ID` ASC) ,
CONSTRAINT `CUSTOMER_ID`
FOREIGN KEY (`CUSTOMER_ID`)
REFERENCES `CUSTOMER` (`CUSTOMER_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
-- removed trunk_id reference to trunk
)
ENGINE = InnoDB;
Trunk table's datatypes were fixed
Trunk_Group_ID and Network_Set_OSS_ID were switched to INT.
CREATE TABLE IF NOT EXISTS `TRUNK` (
`TRUNK_ID` INT(10) NOT NULL AUTO_INCREMENT,
`TRUNK_GROUP_ID` INT(10) NULL, -- fixed datatype
`NETWORK_SET_OSS_ID` INT(10) NULL, -- fixed datatype
`NUM_ID` INT(10) NULL,
`TRUNK_SERVICE_STATUS` TEXT(20) NULL,
`TRUNK_GROUP_PRIORITY` TEXT(20) NULL,
`TRUNK_CAC_LIMIT` TEXT(20) NULL,
`HANDOVER_FORMAT` TEXT(20) NULL,
PRIMARY KEY (`TRUNK_ID`),
INDEX `NETWORK_SET_OSS_ID_idx` (`NETWORK_SET_OSS_ID` ASC) ,
INDEX `NUM_ID_idx` (`NUM_ID` ASC) ,
INDEX `TRUNK_GROUP_ID_idx` (`TRUNK_GROUP_ID` ASC),
CONSTRAINT `NETWORK_SET_OSS_ID`
FOREIGN KEY (`NETWORK_SET_OSS_ID`)
REFERENCES `NETWORK_SET` (`NETWORK_SET_OSS_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `NUM_ID`
FOREIGN KEY (`NUM_ID`)
REFERENCES `NUMBERS` (`NUM_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `TRUNK_GROUP_ID`
FOREIGN KEY (`TRUNK_GROUP_ID`)
REFERENCES `TRUNK_GROUP` (`TRUNK_GROUP_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Avoid circular references
Trunk group and trunk were referring to each other. I'd recommend that you have the relationship for Trunk such that Trunk belongs to a Trunk group and just keep it at that. I also removed the visible keyword.
I ran this on MySQL 5.7 and it worked well.

MySQL Workbench Error 1005 cant create table

So I am very new to MySQL but I would like to think I that grasp a decent bit of it so far. I am struggling with the creation of the table PROJECT. I'm almost positive after my own research that it has something to do with my keys in the table. But I do not understand where the problem is or how to fix it.
The SQL queries below creates all tables for the project. I'm not sure of the problem because I only get the error code for the project for now. Any insight and advice on what causes this and how to rectify this in the code would be greatly appreciated.
CREATE TABLE IF NOT EXISTS `ebrasi1db`.`employee` (
`ssn` VARCHAR(10) NOT NULL,
`fname` VARCHAR(45) NULL DEFAULT NULL,
`minit` VARCHAR(1) NULL DEFAULT NULL,
`lname` VARCHAR(45) NULL DEFAULT NULL,
`bdate` DATE NULL DEFAULT NULL,
`address` VARCHAR(45) NULL DEFAULT NULL,
`sex` VARCHAR(1) NULL DEFAULT NULL,
`salary` INT(11) NULL DEFAULT NULL,
`superssn` VARCHAR(10) NULL DEFAULT NULL,
`dno` INT(11) NULL DEFAULT NULL,
PRIMARY KEY (`ssn`),
INDEX `superssn_idx` (`superssn` ASC),
INDEX `dno_idx` (`dno` ASC),
CONSTRAINT `superssn`
FOREIGN KEY (`superssn`)
REFERENCES `ebrasi1db`.`employee` (`ssn`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `dno`
FOREIGN KEY (`dno`)
REFERENCES `ebrasi1db`.`department` (`dnumber`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `ebrasi1db`.`department`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `ebrasi1db`.`department` (
`dnumber` INT(11) NOT NULL,
`dname` VARCHAR(45) NULL DEFAULT NULL,
`mgrssn` VARCHAR(10) NULL DEFAULT NULL,
`mgrstartdate` DATE NULL DEFAULT NULL,
PRIMARY KEY (`dnumber`),
INDEX `mgrssn_idx` (`mgrssn` ASC),
CONSTRAINT `mgrssn`
FOREIGN KEY (`mgrssn`)
REFERENCES `ebrasi1db`.`employee` (`ssn`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `ebrasi1db`.`dept_locations`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `ebrasi1db`.`dept_locations` (
`dnumber` INT(11) NOT NULL,
`dlocation` VARCHAR(45) NOT NULL,
PRIMARY KEY (`dnumber`, `dlocation`),
CONSTRAINT `dnumber`
FOREIGN KEY (`dnumber`)
REFERENCES `ebrasi1db`.`department` (`dnumber`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `ebrasi1db`.`project`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `ebrasi1db`.`project` (
`pnumber` INT(11) NOT NULL,
`pname` VARCHAR(45) NULL,
`plocation` VARCHAR(45) NULL,
`dnum` INT(11) NOT NULL,
PRIMARY KEY (`pnumber`),
INDEX `dnum_idx` (`dnum` ASC),
INDEX `plocation_idx` (`plocation` ASC),
CONSTRAINT `dnum`
FOREIGN KEY (`dnum`)
REFERENCES `ebrasi1db`.`department` (`dnumber`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `plocation`
FOREIGN KEY (`plocation`)
REFERENCES `ebrasi1db`.`dept_locations` (`dlocation`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `ebrasi1db`.`works_on`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `ebrasi1db`.`works_on` (
`essn` VARCHAR(10) NOT NULL,
`pno` INT NOT NULL,
`hours` DECIMAL(5,2) NULL,
PRIMARY KEY (`essn`, `pno`),
INDEX `pno_idx` (`pno` ASC),
CONSTRAINT `works_on_essn`
FOREIGN KEY (`essn`)
REFERENCES `ebrasi1db`.`employee` (`ssn`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `pno`
FOREIGN KEY (`pno`)
REFERENCES `ebrasi1db`.`project` (`pnumber`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `ebrasi1db`.`dependent`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `ebrasi1db`.`dependent` (
`essn` VARCHAR(10) NOT NULL,
`dependent_name` VARCHAR(45) NOT NULL,
`sex` VARCHAR(1) NULL,
`bdate` DATE NULL,
`relation` VARCHAR(45) NULL,
PRIMARY KEY (`essn`, `dependent_name`),
CONSTRAINT `dependent_essn`
FOREIGN KEY (`essn`)
REFERENCES `ebrasi1db`.`employee` (`ssn`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Your problem lies essentially in the fact that you are trying to create a constraint to a non key column (because the referenced table has a double key).
In your table project you have:
CONSTRAINT `plocation`
FOREIGN KEY (`plocation`)
REFERENCES `dept_locations` (`dlocation`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
dept_locations.dlocation is not a key ALONE (primary key), therefore the reason you are not able to create that constraint.
You need to make the constraint for both keys from the referenced table like this:
CONSTRAINT `plocation`
FOREIGN KEY (`pnumber`, `plocation`)
REFERENCES `dept_locations` (`dnumber`, `dlocation`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
In this case you won't need the first constraint dnum as it is already referenced in dept_locations table.
Also note that you tables department and employee will not be created because one reference the other so you need to create the tables first without the constraints then apply the constraints like:
ALTER TABLE `employee` add
CONSTRAINT `dno`
FOREIGN KEY (`dno`)
REFERENCES `department` (`dnumber`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
Same for the department table and its constraint to employee
Normally you get the Error 1005 when it unable to create the foreign keys. When you are creating a foreign key constraint the parent table should be available in the database.
In your case 'employee' table has a FK to 'department' table but you are trying to create the 'employee' table before creating the 'department' table. Also the 'department' table has a FK back to 'employee' table. Therefore you are not able to create the 'department' table first. To solve this you may create the 'department' table first without FK to 'employee' table. Then create the 'employee' table and then alter the 'department' table with FK.
Here is the modified code for first two tables and follow the same pattern for rest if you get the same error.
CREATE TABLE IF NOT EXISTS `ebrasi1db`.`department` (
`dnumber` INT(11) NOT NULL,
`dname` VARCHAR(45) NULL DEFAULT NULL,
`mgrssn` VARCHAR(10) NULL DEFAULT NULL,
`mgrstartdate` DATE NULL DEFAULT NULL,
PRIMARY KEY (`dnumber`),
INDEX `mgrssn_idx` (`mgrssn` ASC))
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `ebrasi1db`.`employee` (
`ssn` VARCHAR(10) NOT NULL,
`fname` VARCHAR(45) NULL DEFAULT NULL,
`minit` VARCHAR(1) NULL DEFAULT NULL,
`lname` VARCHAR(45) NULL DEFAULT NULL,
`bdate` DATE NULL DEFAULT NULL,
`address` VARCHAR(45) NULL DEFAULT NULL,
`sex` VARCHAR(1) NULL DEFAULT NULL,
`salary` INT(11) NULL DEFAULT NULL,
`superssn` VARCHAR(10) NULL DEFAULT NULL,
`dno` INT(11) NULL DEFAULT NULL,
PRIMARY KEY (`ssn`),
INDEX `superssn_idx` (`superssn` ASC),
INDEX `dno_idx` (`dno` ASC),
CONSTRAINT `superssn`
FOREIGN KEY (`superssn`)
REFERENCES `ebrasi1db`.`employee` (`ssn`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `dno`
FOREIGN KEY (`dno`)
REFERENCES `ebrasi1db`.`department` (`dnumber`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
alter table department add
CONSTRAINT `mgrssn`
FOREIGN KEY (`mgrssn`)
REFERENCES `ebrasi1db`.`employee` (`ssn`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;

Error "Cannot add foreign key constraint"

I am suffering the same "Cannot add foreign key constraint" that other folks around here.
The table client_partners contains a relationship between users. The error is raised at creating client_partners.
I have checked that users.id have the same type than client_partners.clientid and client_partners.partnerid: INT UNSIGNED NOT NULL.
The configuration options are:
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';
Users table definition:
-- -----------------------------------------------------
-- Table `users`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `users` ;
CREATE TABLE IF NOT EXISTS `users` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`username` VARCHAR(50) NOT NULL,
`password` VARCHAR(200) NOT NULL,
`salt` VARCHAR(100) NULL,
`firstname` VARCHAR(50) NOT NULL,
`lastname` VARCHAR(50) NOT NULL,
`email` VARCHAR(50) NOT NULL,
`role` VARCHAR(10) NOT NULL DEFAULT 'supplier',
`destination` VARCHAR(10) NULL,
`supplierId` INT UNSIGNED NULL,
`created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE INDEX `username_UNIQUE` (`username` ASC),
INDEX `fk_users_suppliers_idx` (`supplierId` ASC),
INDEX `fk_users_roles_idx` (`role` ASC),
INDEX `fk_users_destinations1_idx` (`destination` ASC),
CONSTRAINT `fk_users_suppliers`
FOREIGN KEY (`supplierId`)
REFERENCES `suppliers` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_users_roles`
FOREIGN KEY (`role`)
REFERENCES `roles` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_users_destinations1`
FOREIGN KEY (`destination`)
REFERENCES `destinations` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
Client_partners table definition:
-- -----------------------------------------------------
-- Table `client_partners`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `client_partners` ;
CREATE TABLE IF NOT EXISTS `client_partners` (
`clientid` INT UNSIGNED NOT NULL,
`partnerid` INT UNSIGNED NOT NULL,
PRIMARY KEY (`clientid`, `partnerid`),
CONSTRAINT `fk_client_partners_1`
FOREIGN KEY (`clientid` , `partnerid`)
REFERENCES `users` (`id` , `id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
The datatypes (except autoincrement) are the same. The referencing table (users) is created first. What am I missing?
CONSTRAINT `fk_client_partners_1`
FOREIGN KEY (`clientid` , `partnerid`)
REFERENCES `users` (`id` , `id`)
here you are referencing to id of table users twice..it is not possible. try by removing one and create
If you want to reference to another foreign key you need to create it by using another name.

MySQL Error 1451

Command:
DELETE FROM `meetings`.`meetingparticipants` WHERE `meetingparticipants`.`idParticipants` =503 AND `meetingparticipants`.`idMeetings` =83
I know I have a simple error in my database. I am attempting to delete a row from the meeting participants table and receiving the following error:
#1451 - Cannot delete or update a parent row: a foreign key constraint fails (`meetings`.`invoices`, CONSTRAINT `fk_meetingid` FOREIGN KEY (`fk_meetingid`) REFERENCES `meetingparticipants` (`idMeetings`) ON DELETE NO ACTION ON UPDATE NO ACTION)
Any ideas? I think I need a foreign key relationship between the participant table and the meeting-participant table with updates, but I rather confirm or see if there are any suggestions??
CREATE TABLE IF NOT EXISTS `invoices` (
`idinvoices` int(11) NOT NULL AUTO_INCREMENT,
`fk_invoiceType` int(11) DEFAULT NULL,
`DocNum` varchar(10) DEFAULT NULL,
`DZ` varchar(9) DEFAULT NULL,
`Amount` decimal(10,2) DEFAULT NULL,
`fk_meetingid` int(11) DEFAULT NULL,
`fk_participantid` int(11) DEFAULT NULL,
`invoiceNotes` varchar(255) DEFAULT NULL,
`invoiceDocuments` blob,
PRIMARY KEY (`idinvoices`),
UNIQUE KEY `DOC_Num` (`DocNum`),
KEY `fk_invoiceType` (`fk_invoiceType`),
KEY `fk_meetingid` (`fk_meetingid`),
KEY `fk_participantid` (`fk_participantid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1014 ;
-- --------------------------------------------------------
--
-- Table structure for table `invoicestatus`
--
CREATE TABLE IF NOT EXISTS `invoicestatus` (
`idinvoiceStatus` int(11) NOT NULL AUTO_INCREMENT,
`invoiceStatus` varchar(45) NOT NULL,
PRIMARY KEY (`idinvoiceStatus`),
UNIQUE KEY `invoiceStatus_UNIQUE` (`invoiceStatus`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=23 ;
-- --------------------------------------------------------
--
-- Table structure for table `invoicestatushistory`
--
CREATE TABLE IF NOT EXISTS `invoicestatushistory` (
`fk_invoiceStatus` int(11) DEFAULT NULL,
`statusStamp` datetime DEFAULT NULL,
`fk_idinvoices` int(11) NOT NULL,
`idinvoiceStatusHistory` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`idinvoiceStatusHistory`,`fk_idinvoices`),
KEY `invoiceStatusHistory` (`fk_invoiceStatus`),
KEY `invoiceid` (`fk_idinvoices`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1015 ;
-- --------------------------------------------------------
--
-- Table structure for table `invoicetypes`
--
CREATE TABLE IF NOT EXISTS `invoicetypes` (
`idinvoiceTypes` int(11) NOT NULL AUTO_INCREMENT,
`invoiceType` varchar(45) DEFAULT NULL,
PRIMARY KEY (`idinvoiceTypes`),
UNIQUE KEY `invoiceType_UNIQUE` (`invoiceType`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=21 ;
-- --------------------------------------------------------
--
-- Table structure for table `meetingparticipants`
--
CREATE TABLE IF NOT EXISTS `meetingparticipants` (
`idParticipants` int(11) NOT NULL,
`idMeetings` int(11) NOT NULL,
`invited` tinyint(1) DEFAULT NULL,
`attended` tinyint(1) DEFAULT NULL,
`travelCostsReimbursed` tinyint(1) DEFAULT NULL,
`ParticipantFeeEligible` tinyint(1) DEFAULT NULL,
`ParticipantFeeProcessed` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`idParticipants`,`idMeetings`),
KEY `idParticipants` (`idParticipants`),
KEY `idMeetings` (`idMeetings`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Table structure for table `meetings`
--
CREATE TABLE IF NOT EXISTS `meetings` (
`idmeetings` int(11) NOT NULL AUTO_INCREMENT,
`meetingName` varchar(45) NOT NULL,
`beginDate` date DEFAULT NULL,
`endDate` date DEFAULT NULL,
`location` varchar(45) DEFAULT NULL,
`meetingNotes` varchar(255) DEFAULT NULL,
`meetingDocuments` blob,
`fk_programID` int(110) DEFAULT NULL,
PRIMARY KEY (`idmeetings`),
UNIQUE KEY `meetingName_UNIQUE` (`meetingName`),
KEY `programid` (`fk_programID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=85 ;
-- --------------------------------------------------------
--
-- Table structure for table `participants`
--
CREATE TABLE IF NOT EXISTS `participants` (
`idparticipants` int(11) NOT NULL AUTO_INCREMENT,
`firstName` varchar(45) DEFAULT NULL,
`lastName` varchar(45) NOT NULL,
`preferredName` varchar(75) DEFAULT NULL,
`email` varchar(45) DEFAULT NULL,
`affiliation` varchar(45) DEFAULT NULL,
`foreignNational` tinyint(1) DEFAULT NULL,
`grantPaid` tinyint(1) DEFAULT NULL,
`bannerid` varchar(9) DEFAULT NULL,
`participantNotes` varchar(255) DEFAULT NULL,
`participantDocuments` blob,
`visaType` int(11) DEFAULT NULL,
PRIMARY KEY (`idparticipants`),
UNIQUE KEY `bannerid_UNIQUE` (`bannerid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=507 ;
-- --------------------------------------------------------
--
-- Table structure for table `programs`
--
CREATE TABLE IF NOT EXISTS `programs` (
`idprograms` int(11) NOT NULL AUTO_INCREMENT,
`ProgramName` varchar(45) NOT NULL,
PRIMARY KEY (`idprograms`),
UNIQUE KEY `ProgramName_UNIQUE` (`ProgramName`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=21 ;
-- --------------------------------------------------------
--
-- Table structure for table `users`
--
CREATE TABLE IF NOT EXISTS `users` (
`userID` int(11) NOT NULL AUTO_INCREMENT,
`userName` varchar(25) NOT NULL,
`userPassword` varchar(10) NOT NULL DEFAULT 'password',
`userLevel` int(11) NOT NULL,
PRIMARY KEY (`userID`),
UNIQUE KEY `userName` (`userName`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
-- --------------------------------------------------------
--
-- Table structure for table `visatypes`
--
CREATE TABLE IF NOT EXISTS `visatypes` (
`idvisaTypes` int(11) NOT NULL AUTO_INCREMENT,
`visaType` varchar(45) DEFAULT NULL,
`paymentInstructions` varchar(500) DEFAULT NULL,
PRIMARY KEY (`idvisaTypes`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=27 ;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `invoices`
--
ALTER TABLE `invoices`
ADD CONSTRAINT `fk_invoiceType` FOREIGN KEY (`fk_invoiceType`) REFERENCES `invoicetypes` (`idinvoiceTypes`) ON DELETE NO ACTION ON UPDATE NO ACTION,
ADD CONSTRAINT `fk_meetingid` FOREIGN KEY (`fk_meetingid`) REFERENCES `meetingparticipants` (`idMeetings`) ON DELETE NO ACTION ON UPDATE NO ACTION,
ADD CONSTRAINT `fk_participantid` FOREIGN KEY (`fk_participantid`) REFERENCES `meetingparticipants` (`idParticipants`) ON DELETE NO ACTION ON UPDATE NO ACTION;
--
-- Constraints for table `invoicestatushistory`
--
ALTER TABLE `invoicestatushistory`
ADD CONSTRAINT `invoiceid` FOREIGN KEY (`fk_idinvoices`) REFERENCES `invoices` (`idinvoices`) ON DELETE NO ACTION ON UPDATE CASCADE,
ADD CONSTRAINT `invoiceStatusHistory` FOREIGN KEY (`fk_invoiceStatus`) REFERENCES `invoicestatus` (`idinvoiceStatus`) ON DELETE NO ACTION ON UPDATE CASCADE;
--
-- Constraints for table `meetings`
--
ALTER TABLE `meetings`
ADD CONSTRAINT `programid` FOREIGN KEY (`fk_programID`) REFERENCES `programs` (`idprograms`) ON DELETE NO ACTION ON UPDATE NO ACTION;
You have a foreign key constraint on the invoices table that is named "fk_meetingid", but references the meetingparticipants table, which is an association table. It would seem to me that if an invoice were linked to a meeting that it should be linked directly to the meetings table.
From the manual:
http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
NO ACTION: A keyword from standard SQL. In MySQL, equivalent to
RESTRICT. InnoDB rejects the delete or update operation for the parent
table if there is a related foreign key value in the referenced table.
Some database systems have deferred checks, and NO ACTION is a
deferred check. In MySQL, foreign key constraints are checked
immediately, so NO ACTION is the same as RESTRICT.
If I understand your schema, (which is unlikely after looking at it for 30 seconds), you probably want to use ON DELETE CASCADE so that a participants invoices are deleted when you delete the participant. If you need to keep the invoices but delete the participant then choose ON DELETE SET NULL