foreign key and mysql with hibernate - mysql

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

Related

how to gather data from all the six tabels in mysql

I tried Inner join, however i m not able to figure out what is wrong i m doing.
How can I query data from all the 6 tables in the blue box.
The studentprofile table is what is the link between the other 5 tables in the blue region.
I ran this query and this showed me all the data.
select * from users, roles_assigned,studentprofile,schoolwithusers;
Problem I am facing is that I need only filted data for user who are students and school name and student profile
Please Help.
here is my sql code.
--
-- Database: `onlinemarksheets`
--
-- --------------------------------------------------------
--
-- Table structure for table `exams`
--
CREATE TABLE `exams` (
`id` int(11) NOT NULL,
`examtye_id` int(11) DEFAULT NULL,
`schooluser_id` int(11) DEFAULT NULL,
`duration_to` date DEFAULT NULL,
`duration_from` date DEFAULT NULL,
`year` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Table structure for table `examtype`
--
CREATE TABLE `examtype` (
`id` int(11) NOT NULL,
`type` enum('Mid-Term','Half-yearly','Yearly') DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `examtype`
--
INSERT INTO `examtype` (`id`, `type`) VALUES
(1, 'Mid-Term'),
(2, 'Half-yearly'),
(3, 'Yearly');
-- --------------------------------------------------------
--
-- Table structure for table `marks`
--
CREATE TABLE `marks` (
`id` int(11) NOT NULL,
`exam_id` int(11) DEFAULT NULL,
`name` varchar(45) DEFAULT NULL,
`marks_obtained` int(11) DEFAULT NULL,
`marks_total` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Table structure for table `roles`
--
CREATE TABLE `roles` (
`id` int(11) NOT NULL,
`type` enum('Admin','Teacher','Student') DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `roles`
--
INSERT INTO `roles` (`id`, `type`) VALUES
(1, 'Admin'),
(2, 'Teacher'),
(3, 'Student');
-- --------------------------------------------------------
--
-- Table structure for table `roles_assigned`
--
CREATE TABLE `roles_assigned` (
`id` int(11) NOT NULL,
`role_id` int(11) DEFAULT NULL,
`user_id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `roles_assigned`
--
INSERT INTO `roles_assigned` (`id`, `role_id`, `user_id`) VALUES
(1, 1, 1),
(2, 2, 2),
(3, 3, 3),
(4, 3, 4),
(5, 3, 5),
(6, 3, 6),
(7, 1, 1),
(8, 2, 2),
(9, 3, 3),
(10, 3, 4),
(11, 3, 5),
(12, 3, 6);
-- --------------------------------------------------------
--
-- Table structure for table `schools`
--
CREATE TABLE `schools` (
`id` int(11) NOT NULL,
`schoolname` varchar(45) DEFAULT NULL,
`school_email` varchar(45) DEFAULT NULL,
`school_phone` varchar(45) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `schools`
--
INSERT INTO `schools` (`id`, `schoolname`, `school_email`, `school_phone`) VALUES
(1, 'D.A.V Public school', 'info#davschool.com', '789456123'),
(2, 'saraswati Public school', 'info#saraswatischool.com', '9998887774'),
(3, 'S.G.R.R Public School', 'Info#sgrr.com', '54245645125'),
(4, 'Sun Valley Public', 'info#sunvalley.com', '23423423424'),
(5, 'Marshal Public school', 'info#marshalschool.com', '23482728347');
-- --------------------------------------------------------
--
-- Table structure for table `schoolwithusers`
--
CREATE TABLE `schoolwithusers` (
`id` int(11) NOT NULL,
`school_id` int(11) DEFAULT NULL,
`studentprofile_id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `schoolwithusers`
--
INSERT INTO `schoolwithusers` (`id`, `school_id`, `studentprofile_id`) VALUES
(1, 1, 3),
(2, 3, 4);
-- --------------------------------------------------------
--
-- Table structure for table `studentprofile`
--
CREATE TABLE `studentprofile` (
`id` int(11) NOT NULL,
`user_id_fk` int(11) DEFAULT NULL,
`rollno` int(11) DEFAULT NULL,
`dob` date DEFAULT NULL,
`attendence` int(11) DEFAULT NULL,
`class` int(11) DEFAULT NULL,
`section` enum('A','B') DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `studentprofile`
--
INSERT INTO `studentprofile` (`id`, `user_id_fk`, `rollno`, `dob`, `attendence`, `class`, `section`) VALUES
(1, 3, 22, '2022-01-21', 60, 1, 'A'),
(2, 4, 21, '2012-01-04', 45, 1, 'A'),
(3, 4, 1, '2012-01-04', 100, 3, 'B'),
(4, 5, 30, '2007-04-26', 45, 3, 'B'),
(5, 6, 2, '2022-01-19', 50, 6, 'B');
-- --------------------------------------------------------
--
-- Table structure for table `users`
--
CREATE TABLE `users` (
`id` int(11) NOT NULL,
`fname` varchar(45) DEFAULT NULL,
`lname` varchar(45) DEFAULT NULL,
`email` varchar(45) DEFAULT NULL,
`password` varchar(45) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `users`
--
INSERT INTO `users` (`id`, `fname`, `lname`, `email`, `password`) VALUES
(1, 'Shashank', 'Naithani', 'shashank8036#gmail.com', 'lol123'),
(2, 'Kuldeep', 'Negi', 'negikuldeep#gmail.com', 'lop123'),
(3, 'Arpit', 'Thakut', 'Aptha#gmail.com', 'arp123'),
(4, 'Ankit', 'Barthwal', 'ankitbarth#gmail.com', 'ankit123'),
(5, 'Mukesh', 'Thakur', 'sasdb#gmail.com', 'sha123'),
(6, 'Arjun', 'Negi', 'sasdb#gmail.com', 'sha123');
--
-- Indexes for dumped tables
--
--
-- Indexes for table `exams`
--
ALTER TABLE `exams`
ADD PRIMARY KEY (`id`),
ADD KEY `examtye_id` (`examtye_id`) USING BTREE,
ADD KEY `schooluser_id` (`schooluser_id`);
--
-- Indexes for table `examtype`
--
ALTER TABLE `examtype`
ADD PRIMARY KEY (`id`);
--
-- Indexes for table `marks`
--
ALTER TABLE `marks`
ADD PRIMARY KEY (`id`),
ADD KEY `exam_id` (`exam_id`);
--
-- Indexes for table `roles`
--
ALTER TABLE `roles`
ADD PRIMARY KEY (`id`);
--
-- Indexes for table `roles_assigned`
--
ALTER TABLE `roles_assigned`
ADD PRIMARY KEY (`id`),
ADD KEY `role_id` (`role_id`) USING BTREE,
ADD KEY `user_id` (`user_id`) USING BTREE;
--
-- Indexes for table `schools`
--
ALTER TABLE `schools`
ADD PRIMARY KEY (`id`);
--
-- Indexes for table `schoolwithusers`
--
ALTER TABLE `schoolwithusers`
ADD PRIMARY KEY (`id`),
ADD KEY `school_id` (`school_id`) USING BTREE,
ADD KEY `studentprofile_id` (`studentprofile_id`);
--
-- Indexes for table `studentprofile`
--
ALTER TABLE `studentprofile`
ADD PRIMARY KEY (`id`),
ADD KEY `user_id_idx` (`user_id_fk`);
--
-- Indexes for table `users`
--
ALTER TABLE `users`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `exams`
--
ALTER TABLE `exams`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table `examtype`
--
ALTER TABLE `examtype`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;
--
-- AUTO_INCREMENT for table `marks`
--
ALTER TABLE `marks`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table `roles`
--
ALTER TABLE `roles`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;
--
-- AUTO_INCREMENT for table `roles_assigned`
--
ALTER TABLE `roles_assigned`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=13;
--
-- AUTO_INCREMENT for table `schools`
--
ALTER TABLE `schools`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6;
--
-- AUTO_INCREMENT for table `schoolwithusers`
--
ALTER TABLE `schoolwithusers`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;
--
-- AUTO_INCREMENT for table `studentprofile`
--
ALTER TABLE `studentprofile`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6;
--
-- AUTO_INCREMENT for table `users`
--
ALTER TABLE `users`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=7;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `exams`
--
ALTER TABLE `exams`
ADD CONSTRAINT `examtye_id` FOREIGN KEY (`examtye_id`) REFERENCES `examtype` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
ADD CONSTRAINT `schooluser_id` FOREIGN KEY (`schooluser_id`) REFERENCES `schoolwithusers` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;
--
-- Constraints for table `marks`
--
ALTER TABLE `marks`
ADD CONSTRAINT `exam_id` FOREIGN KEY (`exam_id`) REFERENCES `exams` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;
--
-- Constraints for table `roles_assigned`
--
ALTER TABLE `roles_assigned`
ADD CONSTRAINT `role_id` FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
ADD CONSTRAINT `user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;
--
-- Constraints for table `schoolwithusers`
--
ALTER TABLE `schoolwithusers`
ADD CONSTRAINT `school_id` FOREIGN KEY (`school_id`) REFERENCES `schools` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
ADD CONSTRAINT `studentprofile_id` FOREIGN KEY (`studentprofile_id`) REFERENCES `studentprofile` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;
--
-- Constraints for table `studentprofile`
--
ALTER TABLE `studentprofile`
ADD CONSTRAINT `user_id_fk` FOREIGN KEY (`user_id_fk`) REFERENCES `users` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;
COMMIT;
You have to join each table on the primary key/foreign key. Some of the sample data is not quite correct because you have duplicate combinations of student_id and roles_id values in the roles_assigned table. Be sure to list the column names you need instead of *.
select *
from users
INNER JOIN roles_assigned ON users.id = roles_assigned.user_id
INNER JOIN roles ON roles.id = roles_assigned.role_id
INNER JOIN studentprofile on studentprofile.id = users.id
INNER JOIN schoolwithusers on schoolwithusers.studentprofile_id =
studentprofile.id
INNER JOIN schools ON schools.id = schoolwithusers.school_id
WHERE roles.type = 'Student';

converting MySQL syntax to PostgreSQL

So I designed a Bank database in MySQL, and now I want to convert some syntax to PostgreSQL
this is my account and branch table :
CREATE TABLE `account` (
`accountnumber` int(80) NOT NULL,
`balance` text COLLATE utf8_bin NOT NULL,
`date` date NOT NULL,
`time` time NOT NULL,
`branchID` int(25) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
CREATE TABLE `branch` (
`branchID` int(25) NOT NULL,
`bName` varchar(50) COLLATE utf8_bin NOT NULL,
`bCity` varchar(50) COLLATE utf8_bin NOT NULL,
`assests` bigint(20) NOT NULL,
`mainbankID` int(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
I converted it already, but I have problems with these parts:
ALTER TABLE `account`
ADD PRIMARY KEY (`accountnumber`),
ADD KEY `branchID` (`branchID`);
ALTER TABLE `account`
ADD CONSTRAINT `account_ibfk_1` FOREIGN KEY (`branchID`) REFERENCES `branch` (`branchID`);
for example, I tried to write the first one like this, and I got errors:
ALTER TABLE account
ADD PRIMARY KEY (accountnumber),
ADD KEY branchID (branchID);
and I got this error :
type "branchid" does not exist
and here's my piece of code that I converted and working in PostgreSQL :
--
-- Database: `bank`
--
-- --------------------------------------------------------
--
-- Table structure for table account
--
CREATE TABLE account (
accountnumber INTEGER NOT NULL,
balance text NOT NULL,
date date NOT NULL,
time time NOT NULL,
branchID INTEGER NOT NULL
) ;
--
-- Dumping data for table `account`
--
INSERT INTO account (accountnumber, balance, date, time, branchID) VALUES
(1132, 'توضیحات ترازنامه', '2021-07-12', '00:00:03', 516),
(1792, 'توضیحات ترازنامه', '2020-05-29', '11:17:27', 516),
(5130, 'توضیحات ترازنامه', '2016-02-23', '13:18:00', 123),
(7123, 'توضیحات ترازنامه', '2011-11-16', '10:25:00', 124),
(8210, 'توضیحات ترازنامه', '2019-01-11', '16:20:06', 215);
-- --------------------------------------------------------
--
-- Table structure for table `borrower`
--
CREATE TABLE borrower (
customerID INTEGER NOT NULL,
loanID INTEGER NOT NULL
) ;
--
-- Dumping data for table `borrower`
--
INSERT INTO borrower (customerID, loanID) VALUES
(7951, 357),
(1089, 357);
-- --------------------------------------------------------
--
-- Table structure for table `branch`
--
CREATE TABLE branch (
branchID INTEGER NOT NULL,
bName varchar(50) NOT NULL,
bCity varchar(50) NOT NULL,
assests bigint NOT NULL,
mainbankID INTEGER NOT NULL
) ;
--
-- Dumping data for table `branch`
--
INSERT INTO branch (branchID, bName, bCity, assests, mainbankID) VALUES
(123, 'سعادت اباد', 'تهران', 250000000, 1111),
(124, 'تجریش', 'همدان ', 5842000, 2222),
(215, 'خیابان قیام', 'یزد', 700000000, 2222),
(391, 'نقش جهان', 'اصفهان', 20100000, 1111),
(516, 'میرداماد', 'تهران', 953200000, 1111);
-- --------------------------------------------------------
--
-- Table structure for table `customer`
--
CREATE TABLE customer (
customerID INTEGER NOT NULL,
CName varchar(78) NOT NULL,
cphonenumber INTEGER NOT NULL,
cCity varchar(50) NOT NULL,
caddress text NOT NULL,
cage INTEGER NOT NULL
) ;
--
-- Dumping data for table `customer`
--
INSERT INTO customer (customerID, CName, cphonenumber, cCity, caddress, cage) VALUES
(412, 'معین سپهری', 387496, 'تهران', 'تهران شهرک غرب', 27),
(1089, 'سحر مقدم', 3254896, 'اصفهان', 'اصفهان پل خواجوو', 32),
(7951, 'ددا رضوی', 36232323, 'تهران', 'تهران شهرک اندیشه مجتمع رز', 26),
(463241, 'محمد فلاح', 3456213, 'یزد', 'یزد خیابان کاشانی کوچه علوی', 50);
-- --------------------------------------------------------
--
-- Table structure for table `depositor`
--
CREATE TABLE depositor (
customerID INTEGER NOT NULL,
accountnumber INTEGER NOT NULL
) ;
--
-- Dumping data for table `depositor`
--
INSERT INTO depositor (customerID, accountnumber) VALUES
(7951, 7123),
(463241, 5130),
(412, 1132),
(463241, 1132),
(1089, 1132);
-- --------------------------------------------------------
--
-- Table structure for table `employee`
--
CREATE TABLE employee (
EmployeeID INTEGER NOT NULL,
Employeework varchar(100) NOT NULL,
Employeename varchar(25) NOT NULL,
aphonenumber INTEGER NOT NULL,
Employmentyear smallint NOT NULL,
eaddress text NOT NULL,
esalary INTEGER NOT NULL,
branchID INTEGER NOT NULL
) ;
--
-- Dumping data for table `employee`
--
CREATE TABLE mainbank (
mainbankID INTEGER NOT NULL,
mainname varchar(25) NOT NULL,
constructionyear smallint NOT NULL,
budget INTEGER NOT NULL
) ;
--
-- Dumping data for table `mainbank`
--
INSERT INTO mainbank (mainbankID, mainname, constructionyear, budget) VALUES
(1111, 'saderat', 1925, 20000000),
(2222, 'melli', 1912, 100000000);
-- --------------------------------------------------------
--
-- Table structure for table `managment`
--
CREATE TABLE managment (
managmentID INTEGER NOT NULL,
managename varchar(25) NOT NULL,
mphonenumber INTEGER NOT NULL,
manageaddress text NOT NULL,
manageage INTEGER NOT NULL,
manageedu varchar(100) NOT NULL,
msalary INTEGER NOT NULL,
employmentyear smallint NOT NULL,
branchID INTEGER NOT NULL
) ;
--
-- Dumping data for table `managment`
--
INSERT INTO managment (managmentID, managename, mphonenumber, manageaddress, manageage, manageedu, msalary, employmentyear, branchID) VALUES
(1030, 'akbar fallah', 2146258, 'tehran pol sadr', 59, 'Master of Banking', 2100000000, 2011, 516),
(1046, 'mohammad alavi', 352146, 'yazd blv jomhorii', 63, 'Master of Banking', 18200000, 2016, 215);
-- --------------------------------------------------------
--
-- Table structure for table `safebox`
--
CREATE TABLE safebox (
safeboxID INTEGER NOT NULL,
price INTEGER NOT NULL,
date date NOT NULL,
time time(0) NOT NULL,
branchID INTEGER NOT NULL
) ;
--
-- Dumping data for table `safebox`
--
INSERT INTO safebox (safeboxID, price, date, time, branchID) VALUES
(162, 75000000, '2020-11-07', '20:03:41', 123),
(888, 28000000, '2020-04-22', '10:00:00', 391),
(1642, 160000, '2019-01-25', '10:25:04', 215);
-- --------------------------------------------------------
--
-- Table structure for table `servise`
--
CREATE TABLE servise (
serviseID INTEGER NOT NULL,
sname varchar(25) NOT NULL,
Employmentyear smallint NOT NULL,
sage INTEGER NOT NULL,
sphone INTEGER NOT NULL,
saddress text NOT NULL,
ssalary INTEGER NOT NULL,
branchID INTEGER NOT NULL
) ;
--
-- Dumping data for table `servise`
--
INSERT INTO servise (serviseID, sname, Employmentyear, sage, sphone, saddress, ssalary, branchID) VALUES
(13333, 'iman', 1999, 49, 213620, 'tehran andishe', 30000, 516),
(15555, 'hesam', 2015, 38, 216201, 'tehran tajrish', 300000, 124);
and this is my whole code in MySQL:
-- phpMyAdmin SQL Dump
-- version 4.8.5
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Jan 23, 2022 at 11:37 PM
-- Server version: 10.1.38-MariaDB
-- PHP Version: 7.3.3
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: `bank`
--
-- --------------------------------------------------------
--
-- Table structure for table `account`
--
CREATE TABLE `account` (
`accountnumber` int(80) NOT NULL,
`balance` text COLLATE utf8_bin NOT NULL,
`date` date NOT NULL,
`time` time NOT NULL,
`branchID` int(25) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
--
-- Dumping data for table `account`
--
INSERT INTO `account` (`accountnumber`, `balance`, `date`, `time`, `branchID`) VALUES
(1132, 'توضیحات ترازنامه', '2021-07-12', '00:00:03', 516),
(1792, 'توضیحات ترازنامه', '2020-05-29', '11:17:27', 516),
(5130, 'توضیحات ترازنامه', '2016-02-23', '13:18:00', 123),
(7123, 'توضیحات ترازنامه', '2011-11-16', '10:25:00', 124),
(8210, 'توضیحات ترازنامه', '2019-01-11', '16:20:06', 215);
-- --------------------------------------------------------
--
-- Table structure for table `borrower`
--
CREATE TABLE `borrower` (
`customerID` int(50) NOT NULL,
`loanID` int(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
--
-- Dumping data for table `borrower`
--
INSERT INTO `borrower` (`customerID`, `loanID`) VALUES
(7951, 357),
(1089, 357);
-- --------------------------------------------------------
--
-- Table structure for table `branch`
--
CREATE TABLE `branch` (
`branchID` int(25) NOT NULL,
`bName` varchar(50) COLLATE utf8_bin NOT NULL,
`bCity` varchar(50) COLLATE utf8_bin NOT NULL,
`assests` bigint(20) NOT NULL,
`mainbankID` int(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
--
-- Dumping data for table `branch`
--
INSERT INTO `branch` (`branchID`, `bName`, `bCity`, `assests`, `mainbankID`) VALUES
(123, 'سعادت اباد', 'تهران', 250000000, 1111),
(124, 'تجریش', 'همدان ', 5842000, 2222),
(215, 'خیابان قیام', 'یزد', 700000000, 2222),
(391, 'نقش جهان', 'اصفهان', 20100000, 1111),
(516, 'میرداماد', 'تهران', 953200000, 1111);
-- --------------------------------------------------------
--
-- Table structure for table `customer`
--
CREATE TABLE `customer` (
`customerID` int(50) NOT NULL,
`CName` varchar(78) COLLATE utf8_bin NOT NULL,
`cphone number` int(11) NOT NULL,
`cCity` varchar(50) COLLATE utf8_bin NOT NULL,
`caddress` text COLLATE utf8_bin NOT NULL,
`cage` int(3) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
--
-- Dumping data for table `customer`
--
INSERT INTO `customer` (`customerID`, `CName`, `cphone number`, `cCity`, `caddress`, `cage`) VALUES
(412, 'معین سپهری', 387496, 'تهران', 'تهران شهرک غرب', 27),
(1089, 'سحر مقدم', 3254896, 'اصفهان', 'اصفهان پل خواجوو', 32),
(7951, 'ددا رضوی', 36232323, 'تهران', 'تهران شهرک اندیشه مجتمع رز', 26),
(463241, 'محمد فلاح', 3456213, 'یزد', 'یزد خیابان کاشانی کوچه علوی', 50);
-- --------------------------------------------------------
--
-- Table structure for table `depositor`
--
CREATE TABLE `depositor` (
`customerID` int(50) NOT NULL,
`accountnumber` int(80) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
--
-- Dumping data for table `depositor`
--
INSERT INTO `depositor` (`customerID`, `accountnumber`) VALUES
(7951, 7123),
(463241, 5130),
(412, 1132),
(463241, 1132),
(1089, 1132);
-- --------------------------------------------------------
--
-- Table structure for table `employee`
--
CREATE TABLE `employee` (
`EmployeeID` int(10) NOT NULL,
`Employeework` varchar(100) COLLATE utf8_bin NOT NULL,
`Employeename` varchar(25) COLLATE utf8_bin NOT NULL,
`aphonenumber` int(11) NOT NULL,
`Employmentyear` year(4) NOT NULL,
`eaddress` text COLLATE utf8_bin NOT NULL,
`esalary` int(20) NOT NULL,
`branchID` int(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
--
-- Dumping data for table `employee`
--
INSERT INTO `employee` (`EmployeeID`, `Employeework`, `Employeename`, `aphonenumber`, `Employmentyear`, `eaddress`, `esalary`, `branchID`) VALUES
(1551, 'Official', 'ali akbari', 912512123, 1941, 'tehran andarzgoo', 1400000000, 123),
(1881, 'Official', 'nazi imanii', 91352412, 1998, 'yazd kashani', 80000000, 215);
-- --------------------------------------------------------
--
-- Table structure for table `loan`
--
CREATE TABLE `loan` (
`loanID` int(50) NOT NULL,
`amount` int(50) NOT NULL,
`branchID` int(25) NOT NULL,
`startdate` date NOT NULL,
`enddate` date NOT NULL,
`time` time NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
--
-- Dumping data for table `loan`
--
INSERT INTO `loan` (`loanID`, `amount`, `branchID`, `startdate`, `enddate`, `time`) VALUES
(357, 300000, 516, '2020-01-02', '2022-01-12', '10:00:00'),
(412, 1230000, 215, '1998-08-25', '2021-11-25', '09:00:00'),
(863, 5400000, 516, '1996-08-13', '2020-12-30', '07:14:00');
-- --------------------------------------------------------
--
-- Table structure for table `mainbank`
--
CREATE TABLE `mainbank` (
`mainbankID` int(10) NOT NULL,
`mainname` varchar(25) COLLATE utf8_bin NOT NULL,
`constructionyear` year(4) NOT NULL,
`budget` int(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
--
-- Dumping data for table `mainbank`
--
INSERT INTO `mainbank` (`mainbankID`, `mainname`, `constructionyear`, `budget`) VALUES
(1111, 'saderat', 1925, 20000000),
(2222, 'melli', 1912, 100000000);
-- --------------------------------------------------------
--
-- Table structure for table `managment`
--
CREATE TABLE `managment` (
`managmentID` int(10) NOT NULL,
`managename` varchar(25) COLLATE utf8_bin NOT NULL,
`mphonenumber` int(11) NOT NULL,
`manageaddress` text COLLATE utf8_bin NOT NULL,
`manageage` int(3) NOT NULL,
`manageedu` varchar(100) COLLATE utf8_bin NOT NULL,
`msalary` int(20) NOT NULL,
`employmentyear` year(4) NOT NULL,
`branchID` int(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
--
-- Dumping data for table `managment`
--
INSERT INTO `managment` (`managmentID`, `managename`, `mphonenumber`, `manageaddress`, `manageage`, `manageedu`, `msalary`, `employmentyear`, `branchID`) VALUES
(1030, 'akbar fallah', 2146258, 'tehran pol sadr', 59, 'Master of Banking', 2100000000, 2011, 516),
(1046, 'mohammad alavi', 352146, 'yazd blv jomhorii', 63, 'Master of Banking', 18200000, 2016, 215);
-- --------------------------------------------------------
--
-- Table structure for table `safebox`
--
CREATE TABLE `safebox` (
`safeboxID` int(10) NOT NULL,
`price` int(20) NOT NULL,
`date` date NOT NULL,
`time` time NOT NULL,
`branchID` int(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
--
-- Dumping data for table `safebox`
--
INSERT INTO `safebox` (`safeboxID`, `price`, `date`, `time`, `branchID`) VALUES
(162, 75000000, '2020-11-07', '20:03:41', 123),
(888, 28000000, '2020-04-22', '10:00:00', 391),
(1642, 160000, '2019-01-25', '10:25:04', 215);
-- --------------------------------------------------------
--
-- Table structure for table `servise`
--
CREATE TABLE `servise` (
`serviseID` int(10) NOT NULL,
`sname` varchar(25) COLLATE utf8_bin NOT NULL,
`Employmentyear` year(4) NOT NULL,
`sage` int(3) NOT NULL,
`sphone` int(11) NOT NULL,
`saddress` text COLLATE utf8_bin NOT NULL,
`ssalary` int(20) NOT NULL,
`branchID` int(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
--
-- Dumping data for table `servise`
--
INSERT INTO `servise` (`serviseID`, `sname`, `Employmentyear`, `sage`, `sphone`, `saddress`, `ssalary`, `branchID`) VALUES
(13333, 'iman', 1999, 49, 213620, 'tehran andishe', 30000, 516),
(15555, 'hesam', 2015, 38, 216201, 'tehran tajrish', 300000, 124);
--
-- Indexes for dumped tables
--
--
-- Indexes for table `account`
--
ALTER TABLE `account`
ADD PRIMARY KEY (`accountnumber`),
ADD KEY `branchID` (`branchID`);
--
-- Indexes for table `borrower`
--
ALTER TABLE `borrower`
ADD KEY `customerID` (`customerID`),
ADD KEY `loanID` (`loanID`);
--
-- Indexes for table `branch`
--
ALTER TABLE `branch`
ADD PRIMARY KEY (`branchID`);
--
-- Indexes for table `customer`
--
ALTER TABLE `customer`
ADD PRIMARY KEY (`customerID`);
--
-- Indexes for table `depositor`
--
ALTER TABLE `depositor`
ADD KEY `account number` (`accountnumber`),
ADD KEY `customerID` (`customerID`);
--
-- Indexes for table `employee`
--
ALTER TABLE `employee`
ADD PRIMARY KEY (`EmployeeID`),
ADD KEY `branchID` (`branchID`);
--
-- Indexes for table `loan`
--
ALTER TABLE `loan`
ADD PRIMARY KEY (`loanID`),
ADD KEY `branchID` (`branchID`);
--
-- Indexes for table `mainbank`
--
ALTER TABLE `mainbank`
ADD PRIMARY KEY (`mainbankID`);
--
-- Indexes for table `managment`
--
ALTER TABLE `managment`
ADD PRIMARY KEY (`managmentID`),
ADD KEY `branchID` (`branchID`);
--
-- Indexes for table `safebox`
--
ALTER TABLE `safebox`
ADD PRIMARY KEY (`safeboxID`),
ADD KEY `branchID` (`branchID`);
--
-- Indexes for table `servise`
--
ALTER TABLE `servise`
ADD PRIMARY KEY (`serviseID`),
ADD KEY `branchID` (`branchID`);
--
-- Constraints for dumped tables
--
--
-- Constraints for table `account`
--
ALTER TABLE `account`
ADD CONSTRAINT `account_ibfk_1` FOREIGN KEY (`branchID`) REFERENCES `branch` (`branchID`);
--
-- Constraints for table `borrower`
--
ALTER TABLE `borrower`
ADD CONSTRAINT `borrower_ibfk_1` FOREIGN KEY (`customerID`) REFERENCES `customer` (`customerID`),
ADD CONSTRAINT `borrower_ibfk_2` FOREIGN KEY (`loanID`) REFERENCES `loan` (`loanID`);
--
-- Constraints for table `depositor`
--
ALTER TABLE `depositor`
ADD CONSTRAINT `depositor_ibfk_1` FOREIGN KEY (`accountnumber`) REFERENCES `account` (`accountnumber`),
ADD CONSTRAINT `depositor_ibfk_2` FOREIGN KEY (`customerID`) REFERENCES `customer` (`customerID`);
--
-- Constraints for table `employee`
--
ALTER TABLE `employee`
ADD CONSTRAINT `employee_ibfk_1` FOREIGN KEY (`branchID`) REFERENCES `branch` (`branchID`);
--
-- Constraints for table `loan`
--
ALTER TABLE `loan`
ADD CONSTRAINT `loan_ibfk_1` FOREIGN KEY (`branchID`) REFERENCES `branch` (`branchID`);
--
-- Constraints for table `managment`
--
ALTER TABLE `managment`
ADD CONSTRAINT `managment_ibfk_1` FOREIGN KEY (`branchID`) REFERENCES `branch` (`branchID`);
--
-- Constraints for table `safebox`
--
ALTER TABLE `safebox`
ADD CONSTRAINT `safebox_ibfk_1` FOREIGN KEY (`branchID`) REFERENCES `branch` (`branchID`);
--
-- Constraints for table `servise`
--
ALTER TABLE `servise`
ADD CONSTRAINT `servise_ibfk_1` FOREIGN KEY (`branchID`) REFERENCES `branch` (`branchID`);
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 */;
First, you need to replace the backticks (`) with standard conforming double quotes ("). This will also prevent branchID from getting folded to lower case.
Second, instead of ALTER TABLE ... ADD KEY (colname) you have to use ALTER TABLE ... ADD UNIQUE (colname). You need a unique constraint as target of foreign key.
You can use this software for that purpose:
https://soft-builder.com/how-to-convert-mysql-database-to-postgresql/

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails Yii2

I knew there are a lot of questions the same, but I couldn't find useful information. The answers in other questions is saying that the value doesn't exist in another table, but I didn't understand it. In my case I guess all the values are correct:
This is my item database:
CREATE TABLE `item` (
`id` int(11) NOT NULL,
`category_id` int(11) NOT NULL,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`price` double NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `item`
--
ALTER TABLE `item`
ADD PRIMARY KEY (`id`),
ADD KEY `fk-sale-category_id` (`category_id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `item`
--
ALTER TABLE `item`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;
--
-- Apribojimai eksportuotom lentelėm
--
--
-- Apribojimai lentelei `item`
--
ALTER TABLE `item`
ADD CONSTRAINT `fk-sale-category_id` FOREIGN KEY (`category_id`) REFERENCES `category` (`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 */;
And this is my category database:
CREATE TABLE `category` (
`id` int(11) NOT NULL,
`name` varchar(80) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `category`
--
ALTER TABLE `category`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `category`
--
ALTER TABLE `category`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=15;
My Yii2 SiteController action:
public function actionCreate()
{
$category = new Category();
$category->scenario = Category::SCENARIO_CREATE;
$item = new Item();
if ($category->load(Yii::$app->request->post()) && $item->load(Yii::$app->request->post())) {
if ($category->validate(false) && $item->validate(false)) {
$item->category_id = $category->id;
return $this->redirect(['create']);
}
}
return $this->render('create', [
'category' => $category,
'item' => $item,
]);
}
}
In item database there is category_id, which has a foreign key, but that value is in category also, it's `id'. I hope you understand it, thanks for any help

MySQL error: Duplicate key on write or update

I have been using MySQL to design a database, when compiled into SQL code and executed it produces two errors. One that states Variable 'unique checks' can't be set to the value of 'NULL' and another that produces message "Duplicate key on write and update" I have a feeling that it is my foreign keys that are causing this issue but still cannot fix the problem. I have attached the query code below.
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';
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
DROP SCHEMA IF EXISTS `mydb` ;
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`User`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`User` ;
CREATE TABLE IF NOT EXISTS `mydb`.`User` (
`idUser` INT NOT NULL,
`first_name` VARCHAR(45) NULL,
`last_name` VARCHAR(45) NULL,
`username` VARCHAR(45) NULL,
`password` VARCHAR(45) NULL,
`company_pin` VARCHAR(45) NULL,
`student_pin` VARCHAR(45) NULL,
`isAdmin` TINYINT(1) NULL DEFAULT 0,
`isCandidate` TINYINT(1) NULL,
`isVoter` TINYINT(1) NULL,
`votes_left` INT NULL,
`votes_achieved` INT NULL,
PRIMARY KEY (`idUser`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Issue`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Issue` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Issue` (
`idIssue` INT NOT NULL,
`issueTitle` VARCHAR(45) NULL,
`issueDescription` VARCHAR(255) NULL,
`userID` INT NULL,
`isResolved` TINYINT(1) NULL DEFAULT 0,
`upVotes` INT NULL,
PRIMARY KEY (`idIssue`),
INDEX `FK_USERID_idx` (`userID` ASC),
CONSTRAINT `FK_USERID`
FOREIGN KEY (`userID`)
REFERENCES `mydb`.`User` (`idUser`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Forum`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Forum` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Forum` (
`idForum` INT NOT NULL,
`userID` INT NULL,
`postTitle` VARCHAR(45) NULL,
`postText` VARCHAR(255) NULL,
`upVotes` INT NULL,
PRIMARY KEY (`idForum`),
INDEX `FK_USERID_idx` (`userID` ASC),
CONSTRAINT `FK_USERID`
FOREIGN KEY (`userID`)
REFERENCES `mydb`.`User` (`idUser`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Vote`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Vote` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Vote` (
`idVote` INT NOT NULL,
`voteFor` ENUM('TOPIC', 'USER', 'ISSUE') NULL,
`topicID` INT NULL,
`userID` INT NULL,
`issueID` INT NULL,
PRIMARY KEY (`idVote`),
INDEX `FK_USER_VOTED_FOR_idx` (`userID` ASC),
INDEX `FK_ISSUE_VOTED_FOR_idx` (`issueID` ASC),
INDEX `FK_FORUM_VOTED_FOR_idx` (`topicID` ASC),
CONSTRAINT `FK_USER_VOTED_FOR`
FOREIGN KEY (`userID`)
REFERENCES `mydb`.`User` (`idUser`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `FK_ISSUE_VOTED_FOR`
FOREIGN KEY (`issueID`)
REFERENCES `mydb`.`Issue` (`idIssue`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `FK_FORUM_VOTED_FOR`
FOREIGN KEY (`topicID`)
REFERENCES `mydb`.`Forum` (`idForum`)
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;
You already have a Constraint with the name FK_USERID on table mydb.issue. Give the constraint on mydb.Forum another name. e.g.
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';
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
DROP SCHEMA IF EXISTS `mydb` ;
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`User`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`User` ;
CREATE TABLE IF NOT EXISTS `mydb`.`User` (
`idUser` INT NOT NULL,
`first_name` VARCHAR(45) NULL,
`last_name` VARCHAR(45) NULL,
`username` VARCHAR(45) NULL,
`password` VARCHAR(45) NULL,
`company_pin` VARCHAR(45) NULL,
`student_pin` VARCHAR(45) NULL,
`isAdmin` TINYINT(1) NULL DEFAULT 0,
`isCandidate` TINYINT(1) NULL,
`isVoter` TINYINT(1) NULL,
`votes_left` INT NULL,
`votes_achieved` INT NULL,
PRIMARY KEY (`idUser`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Issue`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Issue` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Issue` (
`idIssue` INT NOT NULL,
`issueTitle` VARCHAR(45) NULL,
`issueDescription` VARCHAR(255) NULL,
`userID` INT NULL,
`isResolved` TINYINT(1) NULL DEFAULT 0,
`upVotes` INT NULL,
PRIMARY KEY (`idIssue`),
INDEX `FK_USERID_idx` (`userID` ASC),
CONSTRAINT `FK_USERID`
FOREIGN KEY (`userID`)
REFERENCES `mydb`.`User` (`idUser`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Forum`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Forum` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Forum` (
`idForum` INT NOT NULL,
`userID` INT NULL,
`postTitle` VARCHAR(45) NULL,
`postText` VARCHAR(255) NULL,
`upVotes` INT NULL,
PRIMARY KEY (`idForum`),
INDEX `FK_USERID_idx` (`userID` ASC),
CONSTRAINT `FK_Forum_USERID`
FOREIGN KEY (`userID`)
REFERENCES `mydb`.`User` (`idUser`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Vote`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Vote` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Vote` (
`idVote` INT NOT NULL,
`voteFor` ENUM('TOPIC', 'USER', 'ISSUE') NULL,
`topicID` INT NULL,
`userID` INT NULL,
`issueID` INT NULL,
PRIMARY KEY (`idVote`),
INDEX `FK_USER_VOTED_FOR_idx` (`userID` ASC),
INDEX `FK_ISSUE_VOTED_FOR_idx` (`issueID` ASC),
INDEX `FK_FORUM_VOTED_FOR_idx` (`topicID` ASC),
CONSTRAINT `FK_USER_VOTED_FOR`
FOREIGN KEY (`userID`)
REFERENCES `mydb`.`User` (`idUser`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `FK_ISSUE_VOTED_FOR`
FOREIGN KEY (`issueID`)
REFERENCES `mydb`.`Issue` (`idIssue`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `FK_FORUM_VOTED_FOR`
FOREIGN KEY (`topicID`)
REFERENCES `mydb`.`Forum` (`idForum`)
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 got this error at a time of restoring of database & I found below details :
This issue comes into picture when your table/database names are not same like small/capital letters used in same table/database name on both side (needs to maintain same name on both side as it's case_sensitive).

MySQL #1100 - Table 'USER' was not locked with LOCK TABLES

I am having a very strange problem. I have developed a database and it works perfectly on my local server however then I tried to put it online on my server I keep getting MySQL error:
#1100 - Table 'USER' was not locked with LOCK TABLES
(This is then I try to import my .sql file via phpMyAdmin)
Once again this same script gives no errors on my local server (WAMP server)
I have no Idea why it does not work then I try to put it online. Also it creates the tables and structure the problem is only with the insert statements.
SQL script:
/*!40101 SET NAMES utf8 */;
/*!40101 SET SQL_MODE=''*/;
/*!40014 SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!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' */;
/*!40111 SET #OLD_SQL_NOTES=##SQL_NOTES, SQL_NOTES=0 */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/`a3823833_MiniPos` /*!40100 DEFAULT CHARACTER SET latin1 */;
USE `a3823833_MiniPos`;
/*Table structure for table `user` */
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`userid` int(4) NOT NULL auto_increment,
`username` varchar(20) NOT NULL,
`password` varchar(20) NOT NULL,
`email` varchar(50) NOT NULL,
PRIMARY KEY (`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*Table structure for table `profile` */
DROP TABLE IF EXISTS `profile`;
CREATE TABLE `profile` (
`userid` int(4) NOT NULL,
`lastName` varchar(50),
`firstName` varchar(50),
`dob` varchar(10),
PRIMARY KEY (`userid`),
CONSTRAINT `profile_fk` FOREIGN KEY (`userid`) REFERENCES `user` (`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*Table structure for table 'topic'*/
DROP TABLE IF EXISTS `topic`;
CREATE TABLE `topic` (
`topicid` int(4) NOT NULL auto_increment,
`userid` int(4) NOT NULL,
`text` varchar(2000),
`time` datetime,
PRIMARY KEY (`topicid`),
CONSTRAINT `userid_fk` FOREIGN KEY (`userid`) REFERENCES `user` (`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/* Table structure for table 'post'*/
DROP TABLE IF EXISTS `post`;
CREATE TABLE `post` (
`postid` int(4) NOT NULL auto_increment,
`topicid` int(4),
`userid` int(4) NOT NULL,
`text` varchar(2000),
`time` datetime,
PRIMARY KEY (`postid`),
CONSTRAINT `post_userid_fk` FOREIGN KEY (`userid`) REFERENCES `user` (`userid`),
CONSTRAINT `post_topicid_fk` FOREIGN KEY (`topicid`) REFERENCES `topic` (`topicid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
LOCK TABLES `user` WRITE;
INSERT INTO USER VALUES (1,'Martynas','MartynPass','Martyn#email.com'),(2,'Syed','SyedPass','Syed#email.com'),(3,'Stephanie','StephPass','Steph#email.com');
UNLOCK TABLES;
LOCK TABLES `topic` WRITE;
INSERT INTO topic VALUES (1, 1,'Topic Number 1','2015-06-11 20:15:00'),(2, 2,'Topic Number 2','2015-06-10 19:15:00'),(3, 3,'Topic Number 3','2015-06-09 18:15:00');
UNLOCK TABLES;
LOCK TABLES `post` WRITE;
INSERT INTO post VALUES (1, 1, 1, 'Very Interesting Topic Number 1','2015-06-11 20:15:00'),(2, 1, 2, 'Agree!','2015-06-11 21:15:00');
UNLOCK TABLES;