Some foreign key doesn t work - mysql

I am trying to create a data base withe relation ship. So with PHPMYADMIN, I use the conceptor mode only for my relationship. So I see that my differents table are linked. But when I export my data base, I see that relationships are broken except two or three. I dont know why. I tryed to with sql code but its the same.
relationship who works :
after export
that I want :
before export.
In this picture it is all the relationships that I want.
but if I do this code
ALTER TABLE `personnel`
ADD CONSTRAINT `personnel_ibfk_1` FOREIGN KEY (`idDroit`) REFERENCES `droit` (`id`);
I have no error but in the conceptor mode there is no link between tables.
here my sql code : (it s a french table , sorry ...)
-- phpMyAdmin SQL Dump
-- version 4.1.14
-- http://www.phpmyadmin.net
--
-- Client : 127.0.0.1
-- Généré le : Sam 07 Octobre 2017 à 10:14
-- Version du serveur : 5.6.17
-- Version de PHP : 5.5.12
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET #OLD_CHARACTER_SET_RESULTS=##CHARACTER_SET_RESULTS */;
/*!40101 SET #OLD_COLLATION_CONNECTION=##COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Base de données : `projdb`
--
-- --------------------------------------------------------
--
-- Structure de la table `camps`
--
CREATE TABLE IF NOT EXISTS `camps` (
`idinfrastructure` int(11) NOT NULL,
`capacite` int(11) NOT NULL,
UNIQUE KEY `idinfrastructure` (`idinfrastructure`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Structure de la table `centrale`
--
CREATE TABLE IF NOT EXISTS `centrale` (
`idinfrastructure` int(11) NOT NULL,
UNIQUE KEY `idinfrastructure` (`idinfrastructure`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Structure de la table `commande`
--
CREATE TABLE IF NOT EXISTS `commande` (
`id` int(11) NOT NULL,
`idCentrale` int(11) NOT NULL,
`date` datetime NOT NULL,
`etat` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Structure de la table `commande_produit`
--
CREATE TABLE IF NOT EXISTS `commande_produit` (
`idCommande` int(11) NOT NULL,
`idProduit` int(11) NOT NULL,
`quantite` int(11) NOT NULL,
KEY `idCommande` (`idCommande`),
KEY `idProduit` (`idProduit`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Structure de la table `droit`
--
CREATE TABLE IF NOT EXISTS `droit` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`niveauPrivillege` int(11) NOT NULL,
`nom` varchar(50) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `niveauPrivillege` (`niveauPrivillege`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
-- --------------------------------------------------------
--
-- Structure de la table `infrastructure`
--
CREATE TABLE IF NOT EXISTS `infrastructure` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`nom` varchar(100) NOT NULL,
`latitude` varchar(100) NOT NULL,
`longitude` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
-- --------------------------------------------------------
--
-- Structure de la table `personnel`
--
CREATE TABLE IF NOT EXISTS `personnel` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`idDroit` int(11) NOT NULL,
`nom` varchar(50) NOT NULL,
`prenom` varchar(50) NOT NULL,
`date_nais` date NOT NULL,
`adresse` varchar(300) NOT NULL,
`telephone` varchar(35) CHARACTER SET utf32 NOT NULL,
`mail` varchar(50) NOT NULL,
`nationalite` int(11) NOT NULL,
`pwd` text NOT NULL,
PRIMARY KEY (`id`),
KEY `idDroit` (`idDroit`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
-- --------------------------------------------------------
--
-- Structure de la table `produit`
--
CREATE TABLE IF NOT EXISTS `produit` (
`id` int(11) NOT NULL,
`nom_Produit` varchar(100) NOT NULL,
`prix` float NOT NULL,
`description` varchar(200) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Structure de la table `refugie`
--
CREATE TABLE IF NOT EXISTS `refugie` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`nom` varchar(50) NOT NULL,
`prenom` varchar(50) NOT NULL,
`date_nais` date NOT NULL,
`grpe_sanguin` varchar(10) NOT NULL,
`photo` varchar(150) NOT NULL,
`nationalite` varchar(20) NOT NULL,
`retrouve` tinyint(1) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
-- --------------------------------------------------------
--
-- Structure de la table `sejour`
--
CREATE TABLE IF NOT EXISTS `sejour` (
`idRefugie` int(11) NOT NULL,
`idCamps` int(11) NOT NULL,
`date_arr` datetime NOT NULL,
`date_dep` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Structure de la table `stock`
--
CREATE TABLE IF NOT EXISTS `stock` (
`idProduit` int(11) NOT NULL,
`idInfrastructure` int(11) NOT NULL,
`quantite` int(11) NOT NULL,
KEY `idInfrastructure` (`idInfrastructure`),
KEY `idProduit` (`idProduit`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Structure de la table `travaille`
--
CREATE TABLE IF NOT EXISTS `travaille` (
`idPersonnel` int(11) NOT NULL,
`idInfrastructure` int(11) NOT NULL,
`date_arr` datetime NOT NULL,
`date_dep` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Contraintes pour les tables exportées
--
--
-- Contraintes pour la table `commande_produit`
--
ALTER TABLE `commande_produit`
ADD CONSTRAINT `commande_produit_ibfk_2` FOREIGN KEY (`idProduit`) REFERENCES `produit` (`id`),
ADD CONSTRAINT `commande_produit_ibfk_1` FOREIGN KEY (`idCommande`) REFERENCES `commande` (`id`);
--
-- Contraintes pour la table `stock`
--
ALTER TABLE `stock`
ADD CONSTRAINT `stock_ibfk_1` FOREIGN KEY (`idProduit`) REFERENCES `produit` (`id`);
--
-- Contraintes pour la table `personnel`
--
ALTER TABLE `personnel`
ADD CONSTRAINT `personnel_ibfk_1` FOREIGN KEY (`idDroit`) REFERENCES `droit` (`id`);
/*!40101 SET CHARACTER_SET_CLIENT=#OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=#OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=#OLD_COLLATION_CONNECTION */;
thanks for your help.
thomas

Related

SQL query is executed on web hosting with error but on localhost it runs without a problem

I want to upload my project online. Recently I've bought web hosting service and I've already checked the version of my and their server - 10.1.38-MariaDB. The following query runs without a problem on my localhost database server, but when I tried to run it online I'm getting the following error, why?
1005 - Can't create table psyclade_project.companies (errno: 150 "Foreign key constraint is incorrectly formed")
Obviously the problem is the foreign keys, how could I fix it?
The query is:
-- --------------------------------------------------------
-- Host: 127.0.0.1
-- Server version: 10.1.38-MariaDB - mariadb.org binary distribution
-- Server OS: Win64
-- HeidiSQL Version: 10.2.0.5607
-- --------------------------------------------------------
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET NAMES utf8 */;
/*!50503 SET NAMES utf8mb4 */;
/*!40014 SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
-- Dumping structure for table dev.companies
CREATE TABLE IF NOT EXISTS `companies` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`owner_id` int(11) DEFAULT NULL,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `UNIQ_8244AA3A5E237E06` (`name`),
UNIQUE KEY `UNIQ_8244AA3A7E3C61F9` (`owner_id`),
CONSTRAINT `FK_8244AA3A7E3C61F9` FOREIGN KEY (`owner_id`) REFERENCES `users` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-- Data exporting was unselected.
-- Dumping structure for table dev.jobs
CREATE TABLE IF NOT EXISTS `jobs` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`author_id` int(11) DEFAULT NULL,
`title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`content` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`createdOn` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `IDX_A8936DC5F675F31B` (`author_id`),
CONSTRAINT `FK_A8936DC5F675F31B` FOREIGN KEY (`author_id`) REFERENCES `users` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-- Data exporting was unselected.
-- Dumping structure for table dev.roles
CREATE TABLE IF NOT EXISTS `roles` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `UNIQ_B63E2EC75E237E06` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-- Data exporting was unselected.
-- Dumping structure for table dev.schools
CREATE TABLE IF NOT EXISTS `schools` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `UNIQ_47443BD55E237E06` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-- Data exporting was unselected.
-- Dumping structure for table dev.users
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`school_id` int(11) DEFAULT NULL,
`username` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`full_name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`ages` int(10) unsigned DEFAULT NULL,
`password` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `UNIQ_1483A5E9F85E0677` (`username`),
UNIQUE KEY `UNIQ_1483A5E9E7927C74` (`email`),
KEY `IDX_1483A5E9C32A47EE` (`school_id`),
CONSTRAINT `FK_1483A5E9C32A47EE` FOREIGN KEY (`school_id`) REFERENCES `schools` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-- Data exporting was unselected.
-- Dumping structure for table dev.users_roles
CREATE TABLE IF NOT EXISTS `users_roles` (
`user_id` int(11) NOT NULL,
`role_id` int(11) NOT NULL,
PRIMARY KEY (`user_id`,`role_id`),
KEY `IDX_51498A8EA76ED395` (`user_id`),
KEY `IDX_51498A8ED60322AC` (`role_id`),
CONSTRAINT `FK_51498A8EA76ED395` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`),
CONSTRAINT `FK_51498A8ED60322AC` FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-- Data exporting was unselected.
/*!40101 SET SQL_MODE=IFNULL(#OLD_SQL_MODE, '') */;
/*!40014 SET FOREIGN_KEY_CHECKS=IF(#OLD_FOREIGN_KEY_CHECKS IS NULL, 1, #OLD_FOREIGN_KEY_CHECKS) */;
/*!40101 SET CHARACTER_SET_CLIENT=#OLD_CHARACTER_SET_CLIENT */;
If you want to execute this dump using the SQL tab in phpMyAdmin, you have to uncheck the "Enable foreign key checks" box, which you will find near the "Go" button.

#1822 - Failed to add the foreign key constraint [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
MySQL gives me this error:
Error Code: #1822 - Failed to add the foreign key constraint. Missing index for constraint '' in the referenced table 'produtos'
This is my database:
-- phpMyAdmin SQL Dump
-- version 4.7.7
-- https://www.phpmyadmin.net/
--
-- Host: localhost:3306
-- Generation Time: 10-Jun-2018 às 22:31
-- Versão do servidor: 5.6.39
-- PHP Version: 5.6.30
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET #OLD_CHARACTER_SET_RESULTS=##CHARACTER_SET_RESULTS */;
/*!40101 SET #OLD_COLLATION_CONNECTION=##COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
--
--
-- --------------------------------------------------------
--
-- Estrutura da tabela `fornecedores`
--
CREATE TABLE `fornecedores` (
`Id_Forn` int(9) NOT NULL,
`Nome` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`Empresa` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`Descrição` mediumtext COLLATE utf8_unicode_ci NOT NULL,
`Contacto` int(9) NOT NULL,
`Referencia` int(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-- --------------------------------------------------------
--
-- Estrutura da tabela `funcionários`
--
CREATE TABLE `funcionários` (
`ID_Func` int(9) NOT NULL,
`First_name` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`Last_name` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`Email` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`User` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`PassWord` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
`Contacto` int(9) NOT NULL,
`NIF` int(9) NOT NULL,
`Morada` mediumtext COLLATE utf8_unicode_ci NOT NULL,
`Vencimento` int(9) NOT NULL,
`Cargo` int(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ROW_FORMAT=COMPACT;
--
-- Extraindo dados da tabela `funcionários`
--
-- --------------------------------------------------------
--
-- Estrutura da tabela `newsletter`
--
CREATE TABLE `newsletter` (
`Email` varchar(100) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-- --------------------------------------------------------
--
-- Estrutura da tabela `produtos`
--
CREATE TABLE `produtos` (
`Referencia` int(20) NOT NULL,
`Tipo` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`Nome` varchar(150) COLLATE utf8_unicode_ci NOT NULL,
`Descricao` varchar(10000) COLLATE utf8_unicode_ci NOT NULL,
`Imagens` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
`Stock` int(3) NOT NULL,
`Numero_Serie` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`Preco` int(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Extraindo dados da tabela `produtos`
--
-- --------------------------------------------------------
--
-- Estrutura da tabela `utilizadores`
--
CREATE TABLE `utilizadores` (
`ID_Uti` int(9) NOT NULL,
`First_name` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`Las_name` varchar(30) COLLATE utf8_unicode_ci DEFAULT NULL,
`Email` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`Contacto` int(9) NOT NULL,
`PassWord` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
`Newsletter` int(1) DEFAULT NULL,
`Morada` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
`Codigo_Postal` varchar(8) COLLATE utf8_unicode_ci DEFAULT NULL,
`Conselho` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`NIF` int(9) DEFAULT NULL,
`Ativo` int(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Extraindo dados da tabela `utilizadores`
--
-- --------------------------------------------------------
--
-- Estrutura da tabela `vendas`
--
CREATE TABLE `vendas` (
`ID_Vendas` int(9) NOT NULL,
`ID_Uti` int(9) NOT NULL,
`Referencia` int(20) NOT NULL,
`Data` date NOT NULL,
`Estado` int(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `fornecedores`
--
ALTER TABLE `fornecedores`
ADD PRIMARY KEY (`Id_Forn`),
ADD UNIQUE KEY `Contacto` (`Contacto`),
ADD UNIQUE KEY `Referencia` (`Referencia`),
ADD FOREIGN KEY (`Referencia`) REFERENCES `produtos` (`Referencia`) ;
--
-- Indexes for table `funcionários`
--
ALTER TABLE `funcionários`
ADD PRIMARY KEY (`ID_Func`),
ADD UNIQUE KEY `Email` (`Email`),
ADD UNIQUE KEY `Contacto` (`Contacto`);
--
-- Indexes for table `newsletter`
--
ALTER TABLE `newsletter`
ADD PRIMARY KEY (`Email`);
--
-- Indexes for table `produtos`
--
ALTER TABLE `produtos`
ADD PRIMARY KEY (`Referencia`),
ADD UNIQUE KEY `Nome` (`Nome`);
--
-- Indexes for table `utilizadores`
--
ALTER TABLE `utilizadores`
ADD PRIMARY KEY (`ID_Uti`),
ADD UNIQUE KEY `Email` (`Email`);
--
-- Indexes for table `vendas`
--
ALTER TABLE `vendas`
ADD PRIMARY KEY (`ID_Vendas`),
ADD UNIQUE KEY `ID_Uti` (`ID_Uti`),
ADD UNIQUE KEY `Referencia` (`Referencia`),
ADD FOREIGN KEY (`ID_Uti`) REFERENCES `utilizadores` (`ID_Uti`),
ADD FOREIGN KEY (`Referencia`) REFERENCES `produtos` (`Referencia`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `fornecedores`
--
ALTER TABLE `fornecedores`
MODIFY `Id_Forn` int(9) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table `funcionários`
--
ALTER TABLE `funcionários`
MODIFY `ID_Func` int(9) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=15582;
--
-- AUTO_INCREMENT for table `produtos`
--
ALTER TABLE `produtos`
MODIFY `Referencia` int(20) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=96455;
--
-- AUTO_INCREMENT for table `vendas`
--
ALTER TABLE `vendas`
MODIFY `ID_Vendas` int(9) NOT NULL AUTO_INCREMENT;
COMMIT;
I can't understand where the error is.
Foreign keys need to point to a primary key or unique constraint. However, the script you are using tries to create a foreign key when the referenced column is not (yet) a primary key.
See the error below?
ALTER TABLE `fornecedores`
ADD PRIMARY KEY (`Id_Forn`),
ADD UNIQUE KEY `Contacto` (`Contacto`),
ADD UNIQUE KEY `Referencia` (`Referencia`),
ADD FOREIGN KEY (`Referencia`) REFERENCES `produtos` (`Referencia`) ;
At this point the column Referencia is not yet a primary key on produtos. It's made a primary key a few lines later in your script.
Solution? Just rearrange your script to create all primary keys first, and foreign keys second. Easy.

mysql update parent updates all column values in child

I have the following tables:
-users(not important here)
-students
-courses
-enrollments
the students and courses table have a "active column"
the enrollments table have a "course_active" and "student_active" column.
I am trying to set my DB in such a way that when I change active for student or course this will cascade to enrollments.
Unfortunately I am seeing that when I update an active value on "students/courses"
the whole column of "student active" or "course active" changes.
I would truly appreciate your input and assistance on this matter.
link to a fiddle for convenience:
http://sqlfiddle.com/#!9/8e2ce
here is my SQL:
-- phpMyAdmin SQL Dump
-- version 4.8.0.1
-- https://www.phpmyadmin.net/
--
-- Host: localhost
-- Generation Time: Apr 20, 2018 at 08:32 AM
-- Server version: 10.1.28-MariaDB
-- PHP Version: 7.1.10
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET #OLD_CHARACTER_SET_RESULTS=##CHARACTER_SET_RESULTS */;
/*!40101 SET #OLD_COLLATION_CONNECTION=##COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `schoolcrm2`
--
-- --------------------------------------------------------
--
-- Table structure for table `courses`
--
CREATE TABLE `courses` (
`id` int(5) NOT NULL,
`name` varchar(50) NOT NULL,
`description` text NOT NULL,
`start_date` date NOT NULL,
`end_date` date NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`active` tinyint(1) NOT NULL DEFAULT '1',
`added_by` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Table structure for table `enrollments`
--
CREATE TABLE `enrollments` (
`id` int(5) NOT NULL,
`student_id` int(11) NOT NULL,
`course_id` int(11) NOT NULL,
`user_id` int(11) DEFAULT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`student_active` int(1) NOT NULL DEFAULT '1',
`course_active` int(1) NOT NULL DEFAULT '1',
`added_by` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Table structure for table `students`
--
CREATE TABLE `students` (
`id` int(5) NOT NULL,
`email` varchar(40) NOT NULL,
`name` varchar(30) NOT NULL,
`phone` varchar(30) NOT NULL,
`password` varchar(255) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`active` tinyint(1) NOT NULL DEFAULT '1',
`added_by` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Table structure for table `users`
--
CREATE TABLE `users` (
`id` int(5) NOT NULL,
`email` varchar(50) NOT NULL,
`name` varchar(50) NOT NULL,
`phone` varchar(30) NOT NULL,
`password` varchar(255) NOT NULL,
`role` enum('1','2','3') DEFAULT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`active` tinyint(1) NOT NULL DEFAULT '1',
`added_by` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `courses`
--
ALTER TABLE `courses`
ADD PRIMARY KEY (`id`);
--
-- Indexes for table `enrollments`
--
ALTER TABLE `enrollments`
ADD PRIMARY KEY (`id`);
--
-- Indexes for table `students`
--
ALTER TABLE `students`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `email` (`email`);
--
-- Indexes for table `users`
--
ALTER TABLE `users`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `email` (`email`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `courses`
--
ALTER TABLE `courses`
MODIFY `id` int(5) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table `enrollments`
--
ALTER TABLE `enrollments`
MODIFY `id` int(5) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table `students`
--
ALTER TABLE `students`
MODIFY `id` int(5) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table `users`
--
ALTER TABLE `users`
MODIFY `id` int(5) NOT NULL AUTO_INCREMENT;
ALTER TABLE `enrollments`
ADD CONSTRAINT `enrollments_to_courses` FOREIGN KEY (`course_active`) REFERENCES `courses` (`is_active`) ON UPDATE CASCADE,
ADD CONSTRAINT `enrollments_to_students` FOREIGN KEY (`student_active`) REFERENCES `students` (`is_active`) ON UPDATE CASCADE;
COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT=#OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=#OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=#OLD_COLLATION_CONNECTION */;

foreign key and mysql with hibernate

I am using Hibernate to create table in mySql database.
I have two table "conge" and "employee" and I have a manytoone relationship. the "conge" table have a foreign key of the employee id.
I notice that after inserting exactly two row in my table "conge" I get a foreign key problem. I have used hibernate and also the phpmyadmin to insert and I have always the same problem after inserting two row.
this is my error
Cannot add or update a child row: a foreign key constraint fails (concretepage.Conge, CONSTRAINT FKk1p9i6bbic92cgaad05k4smsg FOREIGN KEY (id) REFERENCES EMPLOYE (id))
Thanks,
Update
I have exported the database this is the sql file
-- phpMyAdmin SQL Dump
-- version 4.7.0
-- https://www.phpmyadmin.net/
--
-- Host: localhost
-- Generation Time: Jul 06, 2017 at 11:50 AM
-- Server version: 10.1.24-MariaDB
-- PHP Version: 7.1.6
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET #OLD_CHARACTER_SET_RESULTS=##CHARACTER_SET_RESULTS */;
/*!40101 SET #OLD_COLLATION_CONNECTION=##COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `concretepage`
--
-- --------------------------------------------------------
--
-- Table structure for table `Conge`
--
CREATE TABLE `Conge` (
`id` int(11) NOT NULL,
`date` datetime DEFAULT CURRENT_TIMESTAMP,
`dateDebut` date DEFAULT NULL,
`dateFin` date DEFAULT NULL,
`numero` int(11) NOT NULL,
`status` varchar(255) DEFAULT NULL,
`employe_id` int(11) DEFAULT NULL,
`typeConge_id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `Conge`
--
INSERT INTO `Conge` (`id`, `date`, `dateDebut`, `dateFin`, `numero`, `status`, `employe_id`, `typeConge_id`) VALUES
(1, '2017-07-06 10:46:30', NULL, NULL, 0, NULL, NULL, NULL),
(2, '2017-07-06 10:46:34', NULL, NULL, 0, NULL, NULL, NULL);
-- --------------------------------------------------------
--
-- Table structure for table `EMPLOYE`
--
CREATE TABLE `EMPLOYE` (
`id` int(11) NOT NULL,
`nom` varchar(255) DEFAULT NULL,
`post_id` int(11) DEFAULT NULL,
`userInfo_login` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `EMPLOYE`
--
INSERT INTO `EMPLOYE` (`id`, `nom`, `post_id`, `userInfo_login`) VALUES
(1, 'foulen', NULL, 'user'),
(2, 'falten', NULL, 'user');
-- --------------------------------------------------------
--
-- Table structure for table `POST`
--
CREATE TABLE `POST` (
`id` int(11) NOT NULL,
`code` varchar(255) DEFAULT NULL,
`intituler` varchar(255) DEFAULT NULL,
`employe_id` int(11) DEFAULT NULL,
`id_superieur` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Table structure for table `TypeConge`
--
CREATE TABLE `TypeConge` (
`id` int(11) NOT NULL,
`code` int(11) NOT NULL,
`intituler` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `TypeConge`
--
INSERT INTO `TypeConge` (`id`, `code`, `intituler`) VALUES
(1, 5757, 'Annuel'),
(2, 2, 'Maladie');
-- --------------------------------------------------------
--
-- Table structure for table `users`
--
CREATE TABLE `users` (
`login` varchar(255) NOT NULL,
`country` varchar(255) DEFAULT NULL,
`enabled` smallint(6) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
`role` varchar(255) DEFAULT NULL,
`employe_id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `users`
--
INSERT INTO `users` (`login`, `country`, `enabled`, `password`, `role`, `employe_id`) VALUES
('admin', NULL, 1, '$2a$10$OlJFlkoM9/nCAK1DUhcE7OvitoDHDip8GuoDt5NrSqWgV5aP7tMeC', 'ROLE_ADMIN', NULL),
('user', NULL, 1, '$2a$10$OlJFlkoM9/nCAK1DUhcE7OvitoDHDip8GuoDt5NrSqWgV5aP7tMeC', 'ROLE_USER', 1);
--
-- Indexes for dumped tables
--
--
-- Indexes for table `Conge`
--
ALTER TABLE `Conge`
ADD PRIMARY KEY (`id`),
ADD KEY `FKov9s99mmo220hv4d8ppeobut` (`employe_id`),
ADD KEY `FKocvumeoahniu5uvsjlwq5mvnp` (`typeConge_id`);
--
-- Indexes for table `EMPLOYE`
--
ALTER TABLE `EMPLOYE`
ADD PRIMARY KEY (`id`),
ADD KEY `FKpe9llqbqsni2xqg1vms2h716j` (`post_id`),
ADD KEY `FKioxxmg7s2j18x2fo7ahnclmcd` (`userInfo_login`);
--
-- Indexes for table `POST`
--
ALTER TABLE `POST`
ADD PRIMARY KEY (`id`),
ADD KEY `FKl6st4h0ujkdiun27r7tak7t7n` (`employe_id`),
ADD KEY `FK3x5ro8omrg46k9jaihue19o8q` (`id_superieur`);
--
-- Indexes for table `TypeConge`
--
ALTER TABLE `TypeConge`
ADD PRIMARY KEY (`id`);
--
-- Indexes for table `users`
--
ALTER TABLE `users`
ADD PRIMARY KEY (`login`),
ADD KEY `FK82xfucsr861ymb3t5sp2hulo3` (`employe_id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `Conge`
--
ALTER TABLE `Conge`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;
--
-- AUTO_INCREMENT for table `EMPLOYE`
--
ALTER TABLE `EMPLOYE`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;
--
-- AUTO_INCREMENT for table `POST`
--
ALTER TABLE `POST`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table `TypeConge`
--
ALTER TABLE `TypeConge`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `Conge`
--
ALTER TABLE `Conge`
ADD CONSTRAINT `FKk1p9i6bbic92cgaad05k4smsg` FOREIGN KEY (`id`) REFERENCES `EMPLOYE` (`id`),
ADD CONSTRAINT `FKlm34pn7fv5rcolnjgjyjnuj9l` FOREIGN KEY (`id`) REFERENCES `TypeConge` (`id`),
ADD CONSTRAINT `FKocvumeoahniu5uvsjlwq5mvnp` FOREIGN KEY (`typeConge_id`) REFERENCES `TypeConge` (`id`),
ADD CONSTRAINT `FKov9s99mmo220hv4d8ppeobut` FOREIGN KEY (`employe_id`) REFERENCES `EMPLOYE` (`id`);
--
-- Constraints for table `EMPLOYE`
--
ALTER TABLE `EMPLOYE`
ADD CONSTRAINT `FKioxxmg7s2j18x2fo7ahnclmcd` FOREIGN KEY (`userInfo_login`) REFERENCES `users` (`login`),
ADD CONSTRAINT `FKpe9llqbqsni2xqg1vms2h716j` FOREIGN KEY (`post_id`) REFERENCES `POST` (`id`);
--
-- Constraints for table `POST`
--
ALTER TABLE `POST`
ADD CONSTRAINT `FK3x5ro8omrg46k9jaihue19o8q` FOREIGN KEY (`id_superieur`) REFERENCES `POST` (`id`),
ADD CONSTRAINT `FKl6st4h0ujkdiun27r7tak7t7n` FOREIGN KEY (`employe_id`) REFERENCES `EMPLOYE` (`id`);
--
-- Constraints for table `users`
--
ALTER TABLE `users`
ADD CONSTRAINT `FK82xfucsr861ymb3t5sp2hulo3` FOREIGN KEY (`employe_id`) REFERENCES `EMPLOYE` (`id`);
COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT=#OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=#OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=#OLD_COLLATION_CONNECTION */;
Because the foreign key is on column ID not employee_id ... look : FOREIGN KEY (id) REFERENCES EMPLOYE (id)) ... and you have only two rows in Employee table

Getting a Cannot add Foreign Key Constraint MySQL

I am having some trouble with phpMyAdmin and MySQL. All of the tables load just fine except for the order table. No matter if I do it all at once, or one table at a time, I get a #1215 - Cannot add foreign key constraint.
This happens for the Orders table only and the Customer_Number attribute. What in the world am I missing here. Thanks in advance.
-- phpMyAdmin SQL Dump
-- version 4.2.11
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Apr 19, 2015 at 01:22 AM
-- Server version: 5.6.21
-- PHP Version: 5.6.3
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
SET foreign_key_checks=0;
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET #OLD_CHARACTER_SET_RESULTS=##CHARACTER_SET_RESULTS */;
/*!40101 SET #OLD_COLLATION_CONNECTION=##COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
-- Database: `popcorn`
-- --------------------------------------------------------
-- Table structure for table `customer`
CREATE TABLE IF NOT EXISTS `customer` (
`Scout_Number` int(10) NOT NULL,
`Customer_Number` int(10) NOT NULL,
`Fname` varchar(15) NOT NULL,
`Lname` varchar(15) NOT NULL,
`House_Number` int(7) NOT NULL,
`Street` varchar(15) NOT NULL,
`City` varchar(15) NOT NULL,
`State` char(2) NOT NULL,
`Zip` int(5) NOT NULL,
`Phone` int(10) NOT NULL,
`Email` varchar(30) NOT NULL,
PRIMARY KEY (Scout_Number, Customer_Number)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
-- Table structure for table `den`
CREATE TABLE IF NOT EXISTS `den` (
`Den_Number` int(3) NOT NULL,
`User_Name` varchar(8) NOT NULL,
`Fname` varchar(15) NOT NULL,
`Lname` varchar(15) NOT NULL,
`Phone` int(10) NOT NULL,
`Email` varchar(30) NOT NULL,
`Den_City` varchar(15) NOT NULL,
`Sales_Goal` int(10) NOT NULL,
`Den_Sales_Total` decimal(10,2) NOT NULL,
`Den_State` char(2) NOT NULL,
PRIMARY KEY (Den_Number)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- Dumping data for table `den`
-- Table structure for table `order`
CREATE TABLE IF NOT EXISTS `order` (
`Order_Number` int(10) NOT NULL,
`Customer_Number` int(10) NOT NULL,
`Donation` decimal(5,2) NOT NULL,
`Order_Total` decimal(5,2) NOT NULL,
`Payment_Status` char(1) NOT NULL,
`Payment_Type` varchar(10) NOT NULL,
`Date` date NOT NULL,
`Delivery_Status` char(1) NOT NULL,
PRIMARY KEY (Order_Number),
FOREIGN KEY (Customer_Number) REFERENCES customer(Customer_Number)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
-- Table structure for table `order_product`
CREATE TABLE IF NOT EXISTS `order_product` (
`Order_Number` int(10) NOT NULL,
`Product_Number` int(10) NOT NULL,
PRIMARY KEY (Order_Number, Product_Number)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
-- Table structure for table `product`
CREATE TABLE IF NOT EXISTS `product` (
`Product_Number` int(10) NOT NULL,
`Product_Name` varchar(15) NOT NULL,
`Description` text NOT NULL,
`Image` blob NOT NULL,
`Price` decimal(5,2) NOT NULL,
PRIMARY KEY (Product_Number)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
-- Table structure for table `scout`
CREATE TABLE IF NOT EXISTS `scout` (
`Scout_Number` int(10) NOT NULL,
`User_Name` char(8) NOT NULL,
`Fname` varchar(15) NOT NULL,
`Lname` varchar(15) NOT NULL,
`Sales_Goal` decimal(10,2) NOT NULL,
`Prize_Progress` int(10) NOT NULL,
`Den_Number` int(3) NOT NULL,
PRIMARY KEY (Scout_Number),
FOREIGN KEY (Den_Number) REFERENCES den(Den_Number)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET CHARACTER_SET_CLIENT=#OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=#OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=#OLD_COLLATION_CONNECTION */;
Columns referenced in a foreign key have to be indexed. You don't have an index on Customer_Number in the customer table. Either change the order of the columns in the composite primary key to(Customer_Number, Scout_Number)or add an additional key just on theCustomer_Number` column.
Note, however, that having a foreign key pointing to a non-unique column is a MySQL extension to SQL, and likely to be a bad idea. See Can a foreign key reference a non-unique index?. I wonder why the primary key of the customer table isn't just Customer_Number.