MySql: Simple Problem with Creating Tables & Foreign Keys - sql

I have problems with creating tables & foreign key. (errno:150)
here is a screenshot Updated Image: http://img264.imagevenue.com/img.php?image=14866_db_122_223lo.jpg
Maybe the problem is the database design.
-- -----------------------------------------------------
-- Table `mydb`.`projekt_user`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`projekt_user` (
`UserID` INT NOT NULL ,
`Name` VARCHAR(45) NULL ,
PRIMARY KEY (`UserID`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`projekt_auswahl`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`projekt_auswahl` (
`UserID` INT NOT NULL ,
`projekt_id` INT NOT NULL ,
`projekt_kategorie_id` INT NOT NULL ,
`projekt_attachment` VARCHAR(45) NULL ,
PRIMARY KEY (`UserID`, `projekt_id`, `projekt_kategorie_id`) ,
INDEX `UserID` (`UserID` ASC) ,
CONSTRAINT `UserID`
FOREIGN KEY (`UserID` )
REFERENCES `mydb`.`projekt_user` (`UserID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`projekt_kategorien`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`projekt_kategorien` (
`projekt_kategorie_id` INT NOT NULL ,
`kategorie` VARCHAR(45) NULL ,
PRIMARY KEY (`projekt_kategorie_id`) ,
INDEX `projekt_kategorie_id` (`projekt_kategorie_id` ASC) ,
CONSTRAINT `projekt_kategorie_id`
FOREIGN KEY (`projekt_kategorie_id` )
REFERENCES `mydb`.`projekt_auswahl` (`projekt_kategorie_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`projekte`
-- -----------------------------------------------------
CREATE TABLE `projekte` (
`projekt_id` INT NOT NULL ,
`projektname` VARCHAR(45) NULL ,
`projekt_status` VARCHAR(45) NULL ,
PRIMARY KEY (`projekt_id`) ,
INDEX `projekt_id` (`projekt_id` ASC) ,
CONSTRAINT `projekt_id`
FOREIGN KEY (`projekt_id` )
REFERENCES `mydb`.`projekt_auswahl` (`projekt_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

You have your foreign keys in the wrong direction, and need to load the tables in order (parents before children):
-- -----------------------------------------------------
-- Table `mydb`.`projekt_user`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`projekt_user` (
`UserID` INT NOT NULL ,
`Name` VARCHAR(45) NULL ,
PRIMARY KEY (`UserID`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`projekt_kategorien`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`projekt_kategorien` (
`projekt_kategorie_id` INT NOT NULL ,
`kategorie` VARCHAR(45) NULL ,
PRIMARY KEY (`projekt_kategorie_id`) ,
INDEX `projekt_kategorie_id` (`projekt_kategorie_id` ASC)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`projekte`
-- -----------------------------------------------------
CREATE TABLE `projekte` (
`projekt_id` INT NOT NULL ,
`projektname` VARCHAR(45) NULL ,
`projekt_status` VARCHAR(45) NULL ,
PRIMARY KEY (`projekt_id`) ,
INDEX `projekt_id` (`projekt_id` ASC))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`projekt_auswahl`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `projekt_auswahl` (
`UserID` INT NOT NULL ,
`projekt_id` INT NOT NULL ,
`projekt_kategorie_id` INT NOT NULL ,
`projekt_attachment` VARCHAR(45) NULL ,
PRIMARY KEY (`UserID`, `projekt_id`, `projekt_kategorie_id`) ,
INDEX `UserID` (`UserID` ASC) ,
CONSTRAINT `UserID`
FOREIGN KEY (`UserID` )
REFERENCES `projekt_user` (`UserID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `projekt_kategorie_id`
FOREIGN KEY (`projekt_kategorie_id` )
REFERENCES `projekt_kategorien` (`projekt_kategorie_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `projekt_id`
FOREIGN KEY (`projekt_id` )
REFERENCES `projekte` (`projekt_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Had I paid more attention to the ERD you provided, I'd have seen that the "crows feet" were pointing upwards, when the convention is they always point down (or the left if necessary).

Related

Derby and SQL from Workbench

I've a project at school and i must use a Derby DB.
I'm using eclipse and the SQL Scrapbook to execute my SQL script.
Here is the problem:
My SQL script was generated by Mysql Workbench, we did a schemas and then exported it to a .sql
When i try to execute it in the SQL Scrapbook, I have MANY errors, the sql seems not to be adapted to derby (for example, AUTOINCREMENT is now (START WITH 1, INCREMENT BY 1).
Here is my .sql :
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';
DROP SCHEMA IF EXISTS `mydb` ;
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`User`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`User` ;
CREATE TABLE IF NOT EXISTS `mydb`.`User` (
`id` INT NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(45) NOT NULL ,
`lastname` VARCHAR(45) NOT NULL ,
`email` VARCHAR(45) NOT NULL ,
`adress` VARCHAR(45) NOT NULL ,
`city` VARCHAR(45) NOT NULL ,
`zip` VARCHAR(45) NOT NULL ,
`login` VARCHAR(45) NOT NULL ,
`password` VARCHAR(45) NOT NULL ,
`admin` TINYINT(1) NOT NULL ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;
-- -----------------------------------------------------
-- Table `mydb`.`Movie`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Movie` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Movie` (
`id` INT NOT NULL AUTO_INCREMENT ,
`title` VARCHAR(45) NOT NULL ,
`resume` VARCHAR(500) NULL ,
`genre` VARCHAR(60) NULL ,
`grade` INT(11) NULL ,
`review_pub` VARCHAR(200) NULL ,
`review_gen` VARCHAR(200) NULL ,
`poster` VARCHAR(100) NULL ,
`duration` INT(11) NULL ,
`release_date` VARCHAR(45) NULL ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Projection`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Projection` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Projection` (
`id` INT NOT NULL AUTO_INCREMENT COMMENT ' ' ,
`date` DATE NULL ,
`length` INT(11) NULL ,
`Movie_id` INT NOT NULL ,
`price` DECIMAL(10,0) NULL ,
`location` VARCHAR(45) NOT NULL ,
`place_nbr` INT(11) NOT NULL ,
PRIMARY KEY (`id`) ,
INDEX `fk_Projection_Movie` (`Movie_id` ASC) ,
CONSTRAINT `fk_Projection_Movie`
FOREIGN KEY (`Movie_id` )
REFERENCES `mydb`.`Movie` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`command`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`command` ;
CREATE TABLE IF NOT EXISTS `mydb`.`command` (
`id` INT NOT NULL AUTO_INCREMENT ,
`Projection_id` INT NOT NULL ,
`User_id` INT NOT NULL ,
`paid` TINYINT(1) NOT NULL ,
PRIMARY KEY (`id`) ,
INDEX `fk_command_Projection1` (`Projection_id` ASC) ,
INDEX `fk_command_User1` (`User_id` ASC) ,
CONSTRAINT `fk_command_Projection1`
FOREIGN KEY (`Projection_id` )
REFERENCES `mydb`.`Projection` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_command_User1`
FOREIGN KEY (`User_id` )
REFERENCES `mydb`.`User` (`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;
Thank you
Try using DdlUtils (http://db.apache.org/ddlutils/) to export your schema from your MySQL database, then you can import that same schema to Derby.

Cannot create table due constraint

I am working few days with my project and I stuck on phpmyadmin sql.
I create in workbench ERR Diagram which looks like:
And the code to paste into sql query is:
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';
DROP SCHEMA IF EXISTS `nieruchomosci` ;
CREATE SCHEMA IF NOT EXISTS `nieruchomosci` DEFAULT CHARACTER SET utf8 ;
USE `nieruchomosci` ;
-- -----------------------------------------------------
-- Table `nieruchomosci`.`wojewodztwa`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `nieruchomosci`.`wojewodztwa` ;
CREATE TABLE IF NOT EXISTS `nieruchomosci`.`wojewodztwa` (
`id_wojewodztwa` INT NULL AUTO_INCREMENT ,
`nazwa` VARCHAR(145) NULL ,
PRIMARY KEY (`id_wojewodztwa`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `nieruchomosci`.`material`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `nieruchomosci`.`material` ;
CREATE TABLE IF NOT EXISTS `nieruchomosci`.`material` (
`id_material` INT NULL AUTO_INCREMENT ,
`typ` VARCHAR(45) NULL ,
PRIMARY KEY (`id_material`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `nieruchomosci`.`szczegoly`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `nieruchomosci`.`szczegoly` ;
CREATE TABLE IF NOT EXISTS `nieruchomosci`.`szczegoly` (
`id_szczegoly` INT NULL ,
`typ_nabytku` VARCHAR(45) NULL ,
`nr_budynku` INT NULL ,
`nr_lokalu` INT NULL ,
`pokoje` INT NULL ,
`powierzchnia_mieszkania` INT NULL ,
`powierzchnia_domu` INT NULL ,
`stan_b` INT NULL ,
`stan_l` INT NULL ,
`winda` VARCHAR(45) NULL ,
`garaz` VARCHAR(45) NULL ,
`osiedle` VARCHAR(45) NULL ,
`telefon` VARCHAR(45) NULL ,
`internet` VARCHAR(45) NULL ,
`tv` VARCHAR(45) NULL ,
`domofon` VARCHAR(45) NULL ,
`tereny` VARCHAR(45) NULL ,
`plac_zabaw` VARCHAR(45) NULL ,
`sport` VARCHAR(45) NULL ,
`kino` VARCHAR(45) NULL ,
`basen` VARCHAR(45) NULL ,
PRIMARY KEY (`id_szczegoly`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `nieruchomosci`.`ogloszenie`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `nieruchomosci`.`ogloszenie` ;
CREATE TABLE IF NOT EXISTS `nieruchomosci`.`ogloszenie` (
`id_ogloszenie` INT NULL AUTO_INCREMENT ,
`rok` INT NULL ,
`pietro` INT NULL ,
`ile_pieter` INT NULL ,
`cena` INT NULL ,
`typ_oferty` INT NULL ,
`id_adres` INT NULL ,
`id_material` INT NULL ,
PRIMARY KEY (`id_ogloszenie`) ,
INDEX `fk_ogloszenie_material1` (`id_material` ASC) ,
CONSTRAINT `fk_ogloszenie_material1`
FOREIGN KEY (`id_material` )
REFERENCES `nieruchomosci`.`material` (`id_material` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_ogloszenie_szczegoly1`
FOREIGN KEY (`id_ogloszenie` )
REFERENCES `nieruchomosci`.`szczegoly` (`id_szczegoly` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `nieruchomosci`.`adres`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `nieruchomosci`.`adres` ;
CREATE TABLE IF NOT EXISTS `nieruchomosci`.`adres` (
`id_adres` INT NULL AUTO_INCREMENT ,
`ulica` VARCHAR(45) NULL ,
`miasto` VARCHAR(45) NULL ,
`powiat` VARCHAR(45) NULL ,
`id_wojewodztwa` INT NULL ,
PRIMARY KEY (`id_adres`) ,
INDEX `fk_adres_wojewodztwa` (`id_wojewodztwa` ASC) ,
CONSTRAINT `fk_adres_wojewodztwa`
FOREIGN KEY (`id_wojewodztwa` )
REFERENCES `nieruchomosci`.`wojewodztwa` (`id_wojewodztwa` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_adres_ogloszenie1`
FOREIGN KEY (`id_adres` )
REFERENCES `nieruchomosci`.`ogloszenie` (`id_adres` )
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;
Adres means Address
Ogloszenie means Advertistment
Wojewodztwa means Province
Material means Material
Szczegoly means Details
This simple project is about advertistment which sells houses.
My error is
Executing SQL script in server
ERROR: Error 1005: Can't create table 'nieruchomosci.adres' (errno: 150)
I am total yellow in sql, where did I make mistake?
I think you reference the foreign key at the wrong position.
Instead of having the following in the definition of adres
CONSTRAINT `fk_adres_ogloszenie1`
FOREIGN KEY (`id_adres` )
REFERENCES `nieruchomosci`.`ogloszenie` (`id_adres` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
You should put this in the definition of the ogloszenie table.
Hope that helps.

InnoDB and creating relationships for table - one will not join

This is my database:
-- Host: localhost
-- Generation Time: Feb 04, 2011 at 01:49 PM
-- Server version: 5.0.45
-- PHP Version: 5.2.5
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
--
-- Database: `myepguide`
--
-- --------------------------------------------------------
--
-- Table structure for table `channel1`
--
CREATE TABLE IF NOT EXISTS `channel1` (
`id` mediumint(255) NOT NULL auto_increment,
`channel` varchar(255) default NULL,
PRIMARY KEY (`id`),
KEY `channel` USING BTREE (`channel`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
--
-- Dumping data for table `channel1`
--
INSERT INTO `channel1` (`id`, `channel`) VALUES
(1, 'BBC One'),
(3, '<i>ITV1</i>'),
(2, '<i>ITV2 </i>');
-- --------------------------------------------------------
--
-- Table structure for table `myepguide`
--
CREATE TABLE IF NOT EXISTS `myepguide` (
`id` mediumint(9) NOT NULL auto_increment,
`programme` varchar(255) NOT NULL,
`channel` varchar(255) default NULL,
`airdate` datetime NOT NULL,
`displayair` datetime NOT NULL,
`expiration` datetime NOT NULL,
`episode` varchar(255) default '',
`setreminder` varchar(255) NOT NULL default '<img src="alert.gif" height="16" width="22"> Set Reminder',
PRIMARY KEY (`id`),
KEY `programme1` USING BTREE (`programme`),
KEY `channel2` USING BTREE (`channel`),
KEY `episode` (`episode`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
--
-- Dumping data for table `myepguide`
--
INSERT INTO `myepguide` (`id`, `programme`, `channel`, `airdate`, `displayair`, `expiration`, `episode`, `setreminder`) VALUES
(1, 'Casualty', '<i>BBC One </i>', '2011-05-18 14:30:00', '2011-05-18 14:30:00', '2011-05-18 15:00:00', 'No Fjords in Finland', '<img src="alert.gif" height="16" width="22"> Set Reminder');
-- --------------------------------------------------------
--
-- Table structure for table `episode`
--
CREATE TABLE IF NOT EXISTS `episode` (
`id` mediumint(9) NOT NULL auto_increment,
`episode` varchar(255) default NULL,
PRIMARY KEY (`id`),
KEY `episode` USING BTREE (`episode`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
--
-- Dumping data for table `episode`
--
INSERT INTO `episode` (`id`, `episode`) VALUES
(1, 'No Fjords in Finland'),
(2, 'Casualty 25th Special'),
(3, 'Palimpsest');
-- --------------------------------------------------------
--
-- Table structure for table `programme`
--
CREATE TABLE IF NOT EXISTS `programme` (
`id` mediumint(255) NOT NULL auto_increment,
`programme` varchar(255) default NULL,
PRIMARY KEY (`id`),
KEY `programme1` USING BTREE (`programme`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
--
-- Dumping data for table `programme`
--
INSERT INTO `programme` (`id`, `programme`) VALUES
(1, 'Casualty');
--
-- Constraints for dumped tables
--
--
-- Constraints for table `myepguide`
--
ALTER TABLE `myepguide`
ADD CONSTRAINT `myepguide_ibfk_1` FOREIGN KEY (`programme`) REFERENCES `myepguide` (`programme`) ON UPDATE CASCADE,
ADD CONSTRAINT `myepguide_ibfk_2` FOREIGN KEY (`channel`) REFERENCES `channel1` (`channel`) ON DELETE SET NULL ON UPDATE CASCADE;
I cannot get the 'episode` table to link to that in the table myepguide for some reason, in Phpmyadmin it always says "Relation not added".
Deleting and re-creating it did not work either, so how can I fix this?
All are stored in InnoDB format so I can't understand why this happened.
Any help is appreciated!
As mentioned in the comment, I am not quite sure what are you trying to link here, so for starters you could use this to get an idea of possible relationships.
EDIT
create table TvSeries (
TvSeriesID int not null auto_increment
-- , other fields here
) ENGINE=InnoDB ;
alter table TvSeries
add constraint pk_TvSeries primary key (TvSeriesID) ;
create table Episode (
TvSeriesID int not null
, EpisodeNo int not null
-- , other fields here
) ENGINE=InnoDB ;
alter table Episode
add constraint pk_Episode primary key (TvSeriesID, EpisodeNo)
, add constraint fk1_Episode foreign key (TvSeriesID) references TvSeries (TvSeriesID) ;
create table Channel (
ChannelID int not null
-- , other fields here
) ENGINE=InnoDB ;
alter table Channel
add constraint pk_Channel primary key (ChannelID);
create table Programme (
ChannelID int not null
, StartTime datetime not null
, TvSeriesID int not null
, EpisodeNo int not null
-- , other fields here
) ENGINE=InnoDB ;
alter table Programme
add constraint pk_Programme primary key (ChannelID, StartTime)
, add constraint fk1_Programme foreign key (ChannelID) references Channel (ChannelID)
, add constraint fk2_Programme foreign key (TvSeriesID, EpisodeNo) references Episode (TvSeriesID, EpisodeNo) ;
create table myEpisodeGuide (
TvSeriesID int not null
, EpisodeNo int not null
, ChannelID int not null
, StartTime datetime not null
, SetReminder int not null
-- , other fields here
) ENGINE=InnoDB ;
alter table myEpisodeGuide
add constraint pk_myEpisodeGuide primary key (TvSeriesID, EpisodeNo, ChannelID, StartTime)
, add constraint fk1_myEpisodeGuide foreign key (TvSeriesID, EpisodeNo) references Episode (TvSeriesID, EpisodeNo)
, add constraint fk2_myEpisodeGuide foreign key (ChannelID, StartTime) references Programme (ChannelID, StartTime) ;

Can not create table, errno: 150

What is bad in these queries for creating tables?
CREATE TABLE IF NOT EXISTS `CVM`.`CANDIDATE` (
`ID` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT ,
`FULL_NAME` VARCHAR(64) NOT NULL ,
`HR_INTERVIEW_DATE` DATE NULL ,
`JOB_SEARCH_STATUS` TINYINT(1) NULL ,
`REFERENCES_OPTIONAL` TEXT NULL ,
`AVAILABILITY` TEXT NULL ,
`LOCATION` VARCHAR(200) NULL ,
`TRIP` TEXT NULL ,
`INTERPERSONAL_RISKS` TEXT NULL ,
`TECHNICAL_INTERVIEW_DATE` DATE NULL ,
`EXPERIENCE_IT` TINYINT UNSIGNED NULL ,
`EXPERIENCE_JAVA` TINYINT UNSIGNED NULL ,
`ENGLISH_CHECK_DATE` DATE NULL ,
`ENGLISH_READINESS_DIALOGUE` VARCHAR(100) NULL ,
`ENGLISH_TIME_IMPROVMENT` TINYINT UNSIGNED NULL ,
`ENGLISH_LEVEL_ID` INT NULL ,
`LEVEL_ID` INT NULL ,
`DESIRED_POSITIONS` VARCHAR(100) NULL ,
`HR_COMMENT` TEXT NULL ,
`EXPERT_COMMENT` TEXT NULL ,
`FOREIGN_PASSPORT_ID` INT(11) UNSIGNED NULL ,
`SALARY_ID` INT(11) UNSIGNED NULL ,
`SSE_INFO_ID` INT(11) UNSIGNED NULL ,
`EXPERT_NAME` VARCHAR(100) NULL ,
`HR_NAME` VARCHAR(100) NULL ,
`TEACHER_NAME` VARCHAR(100) NULL ,
PRIMARY KEY (`ID`) ,
INDEX `FK_CANDIDATE_ENGLISH_LEVEL1` (`ENGLISH_LEVEL_ID` ASC) ,
INDEX `fk_CANDIDATE_LEVEL1` (`LEVEL_ID` ASC) ,
INDEX `fk_CANDIDATE_FOREIGN_PASSPORT1` (`FOREIGN_PASSPORT_ID` ASC) ,
INDEX `fk_CANDIDATE_SALARY1` (`SALARY_ID` ASC) ,
INDEX `fk_CANDIDATE_SSE_INFO1` (`SSE_INFO_ID` ASC) ,
CONSTRAINT `FK_CANDIDATE_ENGLISH_LEVEL1`
FOREIGN KEY (`ENGLISH_LEVEL_ID` )
REFERENCES `CVM`.`ENGLISH_LEVEL` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_CANDIDATE_LEVEL1`
FOREIGN KEY (`LEVEL_ID` )
REFERENCES `CVM`.`LEVEL` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_CANDIDATE_FOREIGN_PASSPORT1`
FOREIGN KEY (`FOREIGN_PASSPORT_ID` )
REFERENCES `CVM`.`FOREIGN_PASSPORT` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_CANDIDATE_SALARY1`
FOREIGN KEY (`SALARY_ID` )
REFERENCES `CVM`.`SALARY` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_CANDIDATE_SSE_INFO1`
FOREIGN KEY (`SSE_INFO_ID` )
REFERENCES `CVM`.`SSE_INFO` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `CVM`.`CANDIDATE_HISTORY`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `CVM`.`CANDIDATE_HISTORY` (
`ID` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT ,
`CANDIDATE_ID` INT(11) UNSIGNED NOT NULL ,
PRIMARY KEY (`ID`) ,
INDEX `fk_CANDIDATE_HISTORY_CANDIDATE1` (`CANDIDATE_ID` ASC) ,
CONSTRAINT `fk_CANDIDATE_HISTORY_CANDIDATE1`
FOREIGN KEY (`CANDIDATE_ID` )
REFERENCES `CVM`.`CANDIDATE` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `CVM`.`CANDIDATE_CHANGES`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `CVM`.`CANDIDATE_CHANGES` (
`ID` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT ,
`FIELD` VARCHAR(100) NULL ,
`CHANGED_VALUE` TEXT NULL ,
PRIMARY KEY (`ID`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `CVM`.`CANDIDATE_HISTORY_has_CANDIDATE_CHANGES`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `CVM`.`CANDIDATE_HISTORY_has_CANDIDATE_CHANGES` (
`CANDIDATE_HISTORY_ID` INT(11) UNSIGNED NOT NULL ,
`CANDIDATE_CHANGES_ID` INT(11) UNSIGNED NOT NULL ,
PRIMARY KEY (`CANDIDATE_HISTORY_ID`, `CANDIDATE_CHANGES_ID`) ,
INDEX `fk_CANDIDATE_HISTORY_has_CANDIDATE_CHANGES_CANDIDATE_CHANGES1` (`CANDIDATE_CHANGES_ID` ASC) ,
CONSTRAINT `fk_CANDIDATE_HISTORY_has_CANDIDATE_CHANGES_CANDIDATE_HISTORY1`
FOREIGN KEY (`CANDIDATE_HISTORY_ID` )
REFERENCES `CVM`.`CANDIDATE_HISTORY` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_CANDIDATE_HISTORY_has_CANDIDATE_CHANGES_CANDIDATE_CHANGES1`
FOREIGN KEY (`CANDIDATE_CHANGES_ID` )
REFERENCES `CVM`.`CANDIDATE_CHANGES` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
In SQL workbench its all ok, but using maven-sql-plugin it failed to create table candidate_history_has_candidate_changes. All names are less than 64 symbols. Please, help!
Try to narrow down the possible causes, starting with a simple create table. Try creating this simple table with and without backtick quotes in case the plugin isn't handling them properly.
Check you are logging in as the same user in both cases.
See if there are any leftovers from the tables. CREATE IF NOT EXIST does nothing if the tables already exist. (see asker's comments for more info).
Lots of possible answers here:
http://verysimple.com/2006/10/22/mysql-error-number-1005-cant-create-table-mydbsql-328_45frm-errno-150/

error code 1005 error number 121 on 3 tables what am I doing wrong?

code:
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';
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
-- Table mydb.SEX
CREATE TABLE IF NOT EXISTS `mydb`.`SEX` (
`idSex` INT NOT NULL AUTO_INCREMENT ,
`descrip` VARCHAR(15) NOT NULL ,
PRIMARY KEY (`idSex`) )
ENGINE = InnoDB;
-- Table mydb.TEAMS
CREATE TABLE IF NOT EXISTS `mydb`.`TEAMS` (
`idTeam` INT NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(50) NOT NULL ,
`idSex` INT NOT NULL ,
PRIMARY KEY (`idTeam`) ,
INDEX `ID_SEX` (`idSex` ASC) ,
CONSTRAINT `ID_SEX`
FOREIGN KEY (`idSex` )
REFERENCES `mydb`.`SEX` (`idSex` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- Table mydb.CHAMPIONSHIPS
CREATE TABLE IF NOT EXISTS `mydb`.`CHAMPIONSHIPS` (
`idChampionship` INT NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(100) NOT NULL ,
`totalPlayingDays` INT NULL ,
`startDate` DATE NULL ,
`EndDate` DATE NULL ,
`active` CHAR NULL ,
`idSex` INT NOT NULL ,
PRIMARY KEY (`idChampionship`) ,
INDEX `ID_SEX` (`idSex` ASC) ,
CONSTRAINT `ID_SEX`
FOREIGN KEY (`idSex` )
REFERENCES `mydb`.`SEX` (`idSex` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- Table mydb.TEAMS_PER_CHAMPIONSHIP
CREATE TABLE IF NOT EXISTS `mydb`.`TEAMS_PER_CHAMPIONSHIP` (
`idTeam` INT NOT NULL ,
`idChampionship` INT NOT NULL ,
PRIMARY KEY (`idTeam`, `idChampionship`) ,
INDEX `ID_TEAM` (`idTeam` ASC) ,
INDEX `ID_CHAMP` (`idChampionship` ASC) ,
CONSTRAINT `ID_TEAM`
FOREIGN KEY (`idTeam` )
REFERENCES `mydb`.`TEAMS` (`idTeam` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `ID_CHAMP`
FOREIGN KEY (`idChampionship` )
REFERENCES `mydb`.`CHAMPIONSHIPS` (`idChampionship` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- Table mydb.MATCHES
CREATE TABLE IF NOT EXISTS `mydb`.`MATCHES` (
`idMatch` INT NOT NULL ,
`idChampionship` INT NOT NULL ,
`idTeam` INT NOT NULL ,
`date` TIMESTAMP NULL DEFAULT NULL ,
`gols` INT NULL DEFAULT -1 ,
`isLocal` CHAR NULL DEFAULT 'N' ,
PRIMARY KEY (`idMatch`, `idChampionship`, `idTeam`) ,
INDEX `ID_TEAM_X_CHAMP` (`idChampionship` ASC, `idMatch` ASC) ,
CONSTRAINT `ID_TEAM_X_CHAMP`
FOREIGN KEY (`idChampionship` , `idMatch` )
REFERENCES `mydb`.`TEAMS_PER_CHAMPIONSHIP` (`idChampionship` , `idTeam` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- Table mydb.PLAYERS
CREATE TABLE IF NOT EXISTS `mydb`.`PLAYERS` (
`idPlayer` INT NOT NULL ,
`name` VARCHAR(45) NULL ,
`lastname` VARCHAR(45) NULL ,
`performance` VARCHAR(45) NULL ,
`idTeam` INT NOT NULL ,
`idSex` INT NOT NULL ,
PRIMARY KEY (`idPLayer`) ,
INDEX `ID_TEAM` (`idTeam` ASC) ,
INDEX `ID_SEX` (`idSex` ASC) ,
CONSTRAINT `ID_TEAM`
FOREIGN KEY (`idTeam` )
REFERENCES `mydb`.`TEAMS` (`idTeam` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `ID_SEX`
FOREIGN KEY (`idSex` )
REFERENCES `mydb`.`SEX` (`idSex` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- Table mydb.MASTERS
CREATE TABLE IF NOT EXISTS `mydb`.`MASTERS` (
`idPlayer` INT NOT NULL ,
`idMatch` INT NOT NULL ,
`idChampionship` INT NOT NULL ,
`idteam` INT NOT NULL ,
`gols` INT NOT NULL DEFAULT -1 ,
PRIMARY KEY (`idPlayer`, `idMatch`, `idChampionship`, `idTeam`) ,
INDEX `ID_MATCH` (`idMatch` ASC, `idChampionship` ASC, `idTeam` ASC) ,
INDEX `ID_PLAY` (`idPLayer` ASC) ,
CONSTRAINT `ID_MATCH`
FOREIGN KEY (`idMatch` , `idChampionship` , `idTeam` )
REFERENCES `mydb`.`MATCHES` (`idMatch` , `idChampionship` , `idTeam` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `ID_PLAY`
FOREIGN KEY (`idPlayer` )
REFERENCES `mydb`.`PLAYERS` (`idPlayer` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- Table mydb.SANCTIONS
CREATE TABLE IF NOT EXISTS `mydb`.`SANCTIONS` (
`idSanction` INT NOT NULL ,
`descrip` VARCHAR(100) NOT NULL DEFAULT 'A DEFINIR' ,
PRIMARY KEY (`idSanction`) )
ENGINE = InnoDB;
-- Table mydb.SANCTIONS_PLAYERS
CREATE TABLE IF NOT EXISTS `mydb`.`SANCIONS_PLAYERS` (
`idPlayer` INT NOT NULL ,
`idMatch` INT NOT NULL ,
`idChampionship` INT NOT NULL ,
`idTeam` INT NOT NULL ,
`idSanction` INT NOT NULL ,
PRIMARY KEY (`idPlayer`, `idMatch`, `idChampionship`, `idTeam`) ,
INDEX `ID_PLAY` (`idPlayer` ASC) ,
INDEX `ID_MATCH` (`idMatch` ASC, `idChampionship` ASC, `idTeam` ASC) ,
INDEX `ID_SANC` (`idSanction` ASC) ,
CONSTRAINT `ID_PLAY`
FOREIGN KEY (`idPlayer` )
REFERENCES `mydb`.`PLAYERS` (`idPlayer` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `ID_MATCH`
FOREIGN KEY (`idMatch` , `idChampionship` , `idTeam` )
REFERENCES `mydb`.`MATCHES` (`idMatch` , `idChampionship` , `idTeam` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `ID_SANC`
FOREIGN KEY (`idSanction` )
REFERENCES `mydb`.`SANCTIONS` (`idSanction` )
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;
I noticed this:
CREATE TABLE IF NOT EXISTS `mydb`.`MATCHES` (
`idMatch` INT NOT NULL ,
`idChampionship` INT NOT NULL ,
`idTeam` INT NOT NULL ,
`date` TIMESTAMP NULL DEFAULT NULL ,
`gols` INT NULL DEFAULT -1 ,
`isLocal` CHAR NULL DEFAULT 'N' ,
PRIMARY KEY (`idMatch`, `idChampionship`, `idTeam`) ,
INDEX `ID_TEAM_X_CHAMP` (`idChampionship` ASC, `idMatch` ASC) ,
CONSTRAINT `ID_TEAM_X_CHAMP`
FOREIGN KEY (`idChampionship` , `idMatch` )
REFERENCES `mydb`.`TEAMS_PER_CHAMPIONSHIP` (`idChampionship` , `idTeam` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
FOREIGN KEY (`idChampionship` , `idMatch` )
REFERENCES `mydb`.`TEAMS_PER_CHAMPIONSHIP` (`idChampionship` , `idTeam` )
I think you have your key and references mismatched. This may not solve your entire problem, but it is still something to be fixed :). Let me know if your errors change with that update.
Edit: I found something else.
As listed on MySQL Reference
InnoDB 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 is in contrast to some older versions, in which indexes had to be created explicitly or the creation of foreign key constraints would fail.) index_name, if given, is used as described previously.
CREATE TABLE IF NOT EXISTS `mydb`.`TEAMS_PER_CHAMPIONSHIP` (
`idTeam` INT NOT NULL ,
`idChampionship` INT NOT NULL ,
PRIMARY KEY (`idTeam`, `idChampionship`) ,
INDEX `ID_TEAM` (`idTeam` ASC) ,
INDEX `ID_CHAMP` (`idChampionship` ASC) ,
CONSTRAINT `ID_TEAM`
FOREIGN KEY (`idTeam` )
REFERENCES `mydb`.`TEAMS` (`idTeam` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `ID_CHAMP`
FOREIGN KEY (`idChampionship` )
REFERENCES `mydb`.`CHAMPIONSHIPS` (`idChampionship` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `mydb`.`MATCHES` (
`idMatch` INT NOT NULL ,
`idChampionship` INT NOT NULL ,
`idTeam` INT NOT NULL ,
`date` TIMESTAMP NULL DEFAULT NULL ,
`gols` INT NULL DEFAULT -1 ,
`isLocal` CHAR NULL DEFAULT 'N' ,
PRIMARY KEY (`idMatch`, `idChampionship`, `idTeam`) ,
INDEX `ID_TEAM_X_CHAMP` (`idChampionship` ASC, `idMatch` ASC) ,
CONSTRAINT `ID_TEAM_X_CHAMP`
FOREIGN KEY (`idChampionship` , `idMatch` )
REFERENCES `mydb`.`TEAMS_PER_CHAMPIONSHIP` (`idChampionship` , `idTeam` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Per that reference text, the referenced keys need to be in the same order as on the referenced tabled. You have them in a different order than your primary key.