MySQL Workbench Error 1005 cant create table - mysql

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;

Related

I cant add MySQL foreign key constraint

I have three tables user, department, and department_hod
user has a department_id, which is the primary key of department, also departement has hod which is the primary key of user.
But i am getting an error when adding the foreign key constraint of username in department_hod table,
Please help
--
-- Create a user table
--
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`username` varchar(50) NOT NULL,
`password` char(80) NOT NULL,
`emp_id` int(11) NOT NULL,
`designation` varchar(50) NOT NULL,
`department_id` int(11) NOT NULL,
`status` varchar(2) NOT NULL DEFAULT 'A',
`email_id` varchar(50) NOT NULL,
`account_status` varchar(2) NOT NULL DEFAULT 'U',
`validity_date` TIMESTAMP,
`deactivation_date` TIMESTAMP ,
`deactivated_by` varchar(50),
`deactivation_remarks` varchar(1000),
`creation_date` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`updation_date` TIMESTAMP,
`created_by` varchar(50),
`updated_by` varchar(50),
CONSTRAINT `PK_USER_ID` PRIMARY KEY (`id`,`username`),
KEY `FK_DEPARTMENT_IDX_01` (`department_id`),
CONSTRAINT `FK_DEPARTMENT_ID_01` FOREIGN KEY (`department_id`)
REFERENCES `department` (`id`)
ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
--
-- Create a department table
--
CREATE TABLE `department` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`status` varchar(2) NOT NULL DEFAULT 'A',
`creation_date` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`updation_date` TIMESTAMP,
`created_by` varchar(50),
`updated_by` varchar(50),
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
--
-- Add department_hod table
--
CREATE TABLE `department_hod` (
`department_id` int(11) NOT NULL,
`username` varchar(50) NOT NULL,
PRIMARY KEY (`username`,`department_id`),
KEY `FK_DEPARTMENT_idx_02` (`department_id`),
CONSTRAINT `FK_DEPARTMENT_id_02` FOREIGN KEY (`department_id`)
REFERENCES `department` (`id`)
ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `FK_USER_01` FOREIGN KEY (`username`)
REFERENCES `user` (`username`)
ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
SET FOREIGN_KEY_CHECKS = 1;
error is here
CONSTRAINT `FK_USER_01` FOREIGN KEY (`username`)
REFERENCES `user` (`username`)
ON DELETE NO ACTION ON UPDATE NO ACTION
Error is
Error Code: 1215. Cannot add foreign key constraint
Always provide complete error message. In a half of choices you may find the solution in it.
For your code the error message is
Failed to add the foreign key constraint. Missing index for constraint 'FK_USER_01' in the referenced table 'user'
So the problem can be fixed by absent index creation:
CREATE INDEX idx_user_username ON user (username);
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=1f0fd24a8eb9cb5692b9d35dd1903045
can you please tell me that is it necessary to use index here? as it is only used for speeding up, do i have to use it beacuse of two primary keys in my user table? – shah-123
MySQL 8.0 Reference Manual / ... / FOREIGN KEY Constraints / Conditions and Restrictions
MySQL requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan. In the referencing table, there must be an index where the foreign key columns are listed as the first columns in the same order. Such an index is created on the referencing table automatically if it does not exist. This index might be silently dropped later if you create another index that can be used to enforce the foreign key constraint. index_name, if given, is used as described previously.

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.

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

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.

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.

#1005 - Can't create table

i'm trying to create some tables in my db but my phpmyadmin is displying the error 1005 - Can't create table, on table tbapp_por_tbpais, below is mysql, cam someone help me ?
Table tbApp
CREATE TABLE IF NOT EXISTS `tbApp` (
`app_cod` INT NOT NULL,
`app_nome` VARCHAR(45) NULL,
`app_cod_do_app` VARCHAR(45) NULL,
`app_pac_fit_qtde` VARCHAR(10) NULL,
`app_pac_flex_qtde` VARCHAR(45) NULL,
`app_pac_mega_qtde` VARCHAR(45) NULL,
`app_trial` VARCHAR(1) NULL,
`app_trial_dias` VARCHAR(4) NULL,
`app_pac_fit_preco` VARCHAR(45) NULL,
`app_pac_flex_preco` VARCHAR(45) NULL,
`app_pac_mega_preço` VARCHAR(45) NULL,
PRIMARY KEY (`app_cod`))
ENGINE = InnoDB;
Table cupomDesc
CREATE TABLE IF NOT EXISTS `cupomDesc` (
`cupom_cod` INT NOT NULL,
`cupom_cod_desc` VARCHAR(45) NULL,
`cupom_valido_ate` DATETIME NULL,
`cupom_exp_percent` VARCHAR(45) NULL,
PRIMARY KEY (`cupom_cod`))
ENGINE = InnoDB;
Table tela
CREATE TABLE IF NOT EXISTS `tela` (
`tela_cod` INT NOT NULL,
`tela_nome` VARCHAR(45) NULL,
PRIMARY KEY (`tela_cod`))
ENGINE = InnoDB;
Table tbHelptips
CREATE TABLE IF NOT EXISTS `tbHelptips` (
`help_cod` INT NOT NULL,
`help_texto` VARCHAR(45) NULL,
`tela_tela_cod` INT NOT NULL,
PRIMARY KEY (`help_cod`),
INDEX `fk_tbHelptips_tela1_idx` (`tela_tela_cod` ASC),
CONSTRAINT `fk_tbHelptips_tela1`
FOREIGN KEY (`tela_tela_cod`)
REFERENCES `tela` (`tela_cod`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Table tbApp_por_tbPais
CREATE TABLE IF NOT EXISTS `tbApp_por_tbPais` (
`tbApp_app_cod` INT NOT NULL,
`tbPais_id` INT(11) NOT NULL,
PRIMARY KEY (`tbApp_app_cod`, `tbPais_id`),
INDEX `fk_tbApp_has_tbPais_tbPais1_idx` (`tbPais_id` ASC),
INDEX `fk_tbApp_has_tbPais_tbApp1_idx` (`tbApp_app_cod` ASC),
CONSTRAINT `fk_tbApp_has_tbPais_tbApp1`
FOREIGN KEY (`tbApp_app_cod`)
REFERENCES `tbApp` (`app_cod`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_tbApp_has_tbPais_tbPais1`
FOREIGN KEY (`tbPais_id`)
REFERENCES `tbPais` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Table tbRevenda_has_tbApp
CREATE TABLE IF NOT EXISTS `tbRevenda_has_tbApp` (
`tbRevenda_id` INT(11) NOT NULL,
`tbApp_app_cod` INT NOT NULL,
`tbApp_Qtde` VARCHAR(45) NULL,
PRIMARY KEY (`tbRevenda_id`, `tbApp_app_cod`),
INDEX `fk_tbRevenda_has_tbApp_tbApp1_idx` (`tbApp_app_cod` ASC),
INDEX `fk_tbRevenda_has_tbApp_tbRevenda1_idx` (`tbRevenda_id` ASC),
CONSTRAINT `fk_tbRevenda_has_tbApp_tbRevenda1`
FOREIGN KEY (`tbRevenda_id`)
REFERENCES `tbRevenda` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_tbRevenda_has_tbApp_tbApp1`
FOREIGN KEY (`tbApp_app_cod`)
REFERENCES `tbApp` (`app_cod`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Table mvx_serial
CREATE TABLE IF NOT EXISTS `mvx_serial` (
`mvx_cod` INT NOT NULL,
`mvx_num_serial` VARCHAR(45) NULL,
`tbPais_id` INT(11) NOT NULL,
PRIMARY KEY (`mvx_cod`),
INDEX `fk_mvx_serial_tbPais1_idx` (`tbPais_id` ASC),
CONSTRAINT `fk_mvx_serial_tbPais1`
FOREIGN KEY (`tbPais_id`)
REFERENCES `tbPais` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Table cambio
CREATE TABLE IF NOT EXISTS `cambio` (
`combio_cod` INT NOT NULL,
`cambio_valor` VARCHAR(45) NULL,
PRIMARY KEY (`combio_cod`))
ENGINE = InnoDB;
Table historico
CREATE TABLE IF NOT EXISTS `historico` (
`hist_cod` INT NOT NULL,
`hist_geracao_dt_ativacao` VARCHAR(45) NULL,
`hist_metodo_de_download` VARCHAR(45) NULL,
`hist_licencas_qtde` VARCHAR(45) NULL,
`hist_ip` VARCHAR(45) NULL,
`hist_alias` VARCHAR(45) NULL,
`tbApp_app_cod` INT NOT NULL,
`cupomDesc_cupom_cod` INT NULL,
`mvx_serial_mvx_cod` INT NOT NULL,
`tbRevenda_id` INT(11) NOT NULL,
PRIMARY KEY (`hist_cod`),
INDEX `fk_historico_tbApp1_idx` (`tbApp_app_cod` ASC),
INDEX `fk_historico_cupomDesc1_idx` (`cupomDesc_cupom_cod` ASC),
INDEX `fk_historico_mvx_serial1_idx` (`mvx_serial_mvx_cod` ASC),
INDEX `fk_historico_tbRevenda1_idx` (`tbRevenda_id` ASC),
CONSTRAINT `fk_historico_tbApp1`
FOREIGN KEY (`tbApp_app_cod`)
REFERENCES `tbApp` (`app_cod`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_historico_cupomDesc1`
FOREIGN KEY (`cupomDesc_cupom_cod`)
REFERENCES `cupomDesc` (`cupom_cod`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_historico_mvx_serial1`
FOREIGN KEY (`mvx_serial_mvx_cod`)
REFERENCES `mvx_serial` (`mvx_cod`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_historico_tbRevenda1`
FOREIGN KEY (`tbRevenda_id`)
REFERENCES `tbRevenda` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
The error seems to be on foreign key constraint
CREATE TABLE IF NOT EXISTS `tbApp_por_tbPais` (
...
CONSTRAINT `fk_tbApp_has_tbPais_tbPais1`
FOREIGN KEY (`tbPais_id`)
REFERENCES `tbPais` (`id`)
I do not see a table called tbPais.
AS the error essage, for this table tbApp_por_tbPais using foreign key ref tbPais.id,but you don't have the this table(tbPais).
CREATE TABLE IF NOT EXISTS `tbApp_por_tbPais` (
`tbApp_app_cod` INT NOT NULL,
`tbPais_id` INT(11) NOT NULL,
PRIMARY KEY (`tbApp_app_cod`, `tbPais_id`),
INDEX `fk_tbApp_has_tbPais_tbPais1_idx` (`tbPais_id` ASC),
INDEX `fk_tbApp_has_tbPais_tbApp1_idx` (`tbApp_app_cod` ASC),
CONSTRAINT `fk_tbApp_has_tbPais_tbApp1`
FOREIGN KEY (`tbApp_app_cod`)
REFERENCES `tbApp` (`app_cod`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_tbApp_has_tbPais_tbPais1`
FOREIGN KEY (`tbPais_id`)
REFERENCES `tbPais` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
And lost table tbRevenda also.
CREATE TABLE IF NOT EXISTS `tbPais` (
`id` INT NOT NULL PRIMARY key
)
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `tbRevenda` (
`id` INT(11) NOT NULL PRIMARY KEY
)
ENGINE = InnoDB;
Try to type in your database name before table name ...
CREATE TABLE IF NOT EXISTS `databasename.cambio` (