Max values of column - mysql

With this script I get the max value of that column.
SELECT p.ploegnaam, MAX(k.punten) AS punten
FROM ploeg p,
klassement k
WHERE p.id = k.ploeg
But when I have 2 values with the same value that are the max value. I only
get the first one.
When the max value of that column is 20 and I have 3 values with 20 in that column, how can I get all of them and not only the first one of that column with that value of 20?
db script
/*Table structure for table `klassement` */
DROP TABLE IF EXISTS `klassement`;
CREATE TABLE `klassement` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`punten` int(2) NOT NULL,
`ploeg` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `ploeg` (`ploeg`),
CONSTRAINT `klassement_ibfk_1` FOREIGN KEY (`ploeg`) REFERENCES `ploeg` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=latin1;
/*Data for the table `klassement` */
insert into `klassement`(`id`,`punten`,`ploeg`) values
(14,7,29),
(15,7,30),
(16,1,31),
(17,0,32),
(18,0,34),
(19,1,36),
(20,0,40),
(21,0,41);
/*Table structure for table `ploeg` */
DROP TABLE IF EXISTS `ploeg`;
CREATE TABLE `ploeg` (
`id` int(3) NOT NULL AUTO_INCREMENT,
`Ploegnaam` varchar(50) CHARACTER SET latin1 NOT NULL,
`Stadium` int(3) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `Stadium` (`Stadium`),
CONSTRAINT `ploeg_ibfk_1` FOREIGN KEY (`Stadium`) REFERENCES `stadium` (`id`),
CONSTRAINT `ploeg_ibfk_2` FOREIGN KEY (`Stadium`) REFERENCES `stadium` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=42 DEFAULT CHARSET=utf8;
/*Data for the table `ploeg` */
insert into `ploeg`(`id`,`Ploegnaam`,`Stadium`) values
(29,'Club Brugge',41),
(30,'Cercle Brugge',41),
(31,'België',41),
(32,'belgiê',41),
(33,'belgië',41),
(34,'belgie',41),
(35,'belgiê',41),
(36,'Mùnchen',41),
(37,'mùnchen',41),
(38,'mùnchen',41),
(39,'mùnchen',41),
(40,'mùchuo',41),
(41,'ùùùù',41);
/*Table structure for table `stadium` */
DROP TABLE IF EXISTS `stadium`;
CREATE TABLE `stadium` (
`id` int(3) NOT NULL AUTO_INCREMENT,
`Naam` varchar(50) CHARACTER SET latin1 NOT NULL,
`info` varchar(240) CHARACTER SET latin1 DEFAULT NULL,
`Adres` varchar(2048) CHARACTER SET latin1 NOT NULL,
`afbeelding` varchar(240) CHARACTER SET latin1 NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=52 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
/*Data for the table `stadium` */
insert into `stadium`(`id`,`Naam`,`info`,`Adres`,`afbeelding`) values
(41,'Jan breydel','jan breydel in brugge','Jan Breydelstadion, Koning Leopold III-laan, Brugge, Belgi&euml','../uploads/volkswagen.jpg'),
(42,'jan','f','Kortrijk, België','../uploads/12042828_428221037364899_823789106058625890_n.jpg'),
(43,'jab','bebe','Leuven, België','../uploads/12417623_10206864642527072_5223843936076001877_n.jpg'),
(44,'jab','bebe','Leuven, België','../uploads/12417623_10206864642527072_5223843936076001877_n.jpg'),
(45,'jab','bebe','Leuven, België','../uploads/12417623_10206864642527072_5223843936076001877_n.jpg'),
(46,'jab','bebe','Leuven, België','../uploads/12417623_10206864642527072_5223843936076001877_n.jpg'),
(47,'jab','bebe','Leuven, België','../uploads/12417623_10206864642527072_5223843936076001877_n.jpg'),
(48,'jab','bebe','Leuven, België','../uploads/12417623_10206864642527072_5223843936076001877_n.jpg'),
(49,'jab','bebe','Leuven, België','../uploads/12417623_10206864642527072_5223843936076001877_n.jpg'),
(50,'jab','bebe','Leuven, België','../uploads/12417623_10206864642527072_5223843936076001877_n.jpg'),
(51,'jab','bebe','Leuven, België','../uploads/12417623_10206864642527072_5223843936076001877_n.jpg');
/*!40101 SET SQL_MODE=#OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS */;
/*!40111 SET SQL_NOTES=#OLD_SQL_NOTES */;
Thanks

I think you may use nested query:
SELECT p.ploegnaam, k.punten AS punten
FROM ploeg p inner join klassement k on p.id = k.ploeg
WHERE k.punten = (select max(punten) from klassement )

You could use this
select p.ploegnaam, k.punten
from ploeg p
inner join klassement k
on p.id = k.ploeg
inner join (SELECT p.ploegnaam, MAX(k.punten) AS max_punten
FROM ploeg p1
inner join klassement k1
on p1.id = k1.ploeg
) m
on p.id = m.ploegnaam and k.punten = m.max_punten;
And try not using old-style-join from now on.

Related

Does anyone know how to fix Error 1103 in MySQL?

I am quite new to MySQL, I downloaded the file and then opened it in SQL I ran it, but it didn't work and stated the following
Error Code: 1103. Incorrect table name 'campaign';/!40101 SET #saved_cs_client = ##character_set_client /;/!40101 SET character_set'
This is where I believe the problem is
DROP TABLE IF EXISTS `campaign';
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `campaign` (
`id` bigint(20) NOT NULL,
`name` text,
`sub_category_id` bigint(20) DEFAULT NULL,
`country_id` bigint(20) DEFAULT NULL,
`currency_id` bigint(20) DEFAULT NULL,
`launched` datetime DEFAULT NULL,
`deadline` datetime DEFAULT NULL,
`goal` double DEFAULT NULL,
`pledged` double DEFAULT NULL,
`backers` bigint(20) DEFAULT NULL,
`outcome` text,
PRIMARY KEY (`id`),
KEY `country_id` (`country_id`),
KEY `sub_category_id` (`sub_category_id`),
KEY `currency_id` (`currency_id`),
CONSTRAINT `campaign_ibfk_1` FOREIGN KEY (`country_id`) REFERENCES `country` (`id`),
CONSTRAINT `campaign_ibfk_2` FOREIGN KEY (`sub_category_id`) REFERENCES `sub_category` (`id`),
CONSTRAINT `campaign_ibfk_3` FOREIGN KEY (`currency_id`) REFERENCES `currency` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = #saved_cs_client */;
Dumping data for table `campaign`
--
LOCK TABLES `campaign` WRITE;
/*!40000 ALTER TABLE `campaign` DISABLE KEYS */;
INSERT INTO `campaign` VALUES (1,'Ragdolls',23,2,2)
INSERT INTO `campaign` VALUES (8667,'Blank Screen Films Summer Project 2013')
/*!40000 ALTER TABLE `campaign` ENABLE KEYS */;
UNLOCK TABLES;
Feel free to ask any question to attain more insight.
Where you have:
DROP TABLE IF EXISTS `campaign';
this is supposed to be:
DROP TABLE IF EXISTS `campaign`;
Either you accidentally changed the backtick to a single quote, or something in your "downloaded the file and then opened it in SQL" changed it on you.

MariaDB/PostreSQL - Query optimization

I need to produce some numbers, I have designed a query to get the result desired by my "customers".
This query is based on a table that contains one million records.
I usually use MariaDB for that, and I get a result in ~ 7s.
This execution time is quite suitable but I am looking to optimize again to improve my skills.
After some research, I came across a few posts saying "MySQL is fine, but not on tables> 1M of records, you have to switch on something else" PostgreSQL has been quoted several times.
So I installed PostgreSQL, and copied my tables, indexes and data.
I executed the same query, and I had a result in ~ 12s
I know less PostgreSQL, I think I did not use the specificities inherent to the language.
So for now I stay on MariaDB. Do you have an idea to improve the execution time?
Here my query :
select categorie.cat
,dhu_type.type
,COUNT(DISTINCT(
CASE WHEN dhu.date between '2013-01-01' and '2013-12-31'
THEN dhu.id
END )
) AS "2013"
,COUNT(DISTINCT(
CASE WHEN dhu.date between '2014-01-01' and '2014-12-31'
THEN dhu.id
END )
) AS "2014"
,COUNT(DISTINCT(
CASE WHEN dhu.date between '2015-01-01' and '2015-12-31'
THEN dhu.id
END )
) AS "2015"
,COUNT(DISTINCT(
CASE WHEN dhu.date between '2016-01-01' and '2016-12-31'
THEN dhu.id
END )
) AS "2016"
from dhu
inner join dhu_type on dhu.type_id = dhu_type.id
inner join patient on dhu.patient_id=patient.id
inner join fa on patient.id = fa.patient_id
inner join categorie on categorie.id = fa.cat_id
group by cat,dhu_type.type
I complete my question with a diagram
Here the CREATE TABLE :
/*!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' */;
CREATE TABLE IF NOT EXISTS `categorie` (
`id` tinyint(3) unsigned NOT NULL AUTO_INCREMENT,
`cat` varchar(50) NOT NULL DEFAULT 'neonat',
PRIMARY KEY (`id`,`cat`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `cp` (
`id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`cp` varchar(5) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `cp` (`cp`)
) ENGINE=InnoDB AUTO_INCREMENT=4096 DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `dhu` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`patient_id` int(10) unsigned NOT NULL,
`date` date NOT NULL,
`type_id` tinyint(3) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `FK_dhu_patient` (`patient_id`),
KEY `FK_dhu_dhu_type` (`type_id`),
CONSTRAINT `FK_dhu_dhu_type` FOREIGN KEY (`type_id`) REFERENCES `dhu_type` (`id`),
CONSTRAINT `FK_dhu_patient` FOREIGN KEY (`patient_id`) REFERENCES `patient` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=953590 DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `dhu_import` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`noip` bigint(10) unsigned zerofill NOT NULL,
`date` date NOT NULL,
`cp` varchar(5) NOT NULL,
`type` varchar(4) NOT NULL,
PRIMARY KEY (`id`),
KEY `noip` (`noip`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `dhu_type` (
`id` tinyint(3) unsigned NOT NULL AUTO_INCREMENT,
`type` varchar(4) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `type` (`type`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `dpt` (
`dpt` tinyint(3) unsigned DEFAULT NULL,
`abrev` char(3) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `fa` (
`patient_id` int(10) unsigned NOT NULL,
`cat_id` tinyint(3) unsigned NOT NULL,
PRIMARY KEY (`patient_id`,`cat_id`),
KEY `idx_cat_id_pat_id` (`cat_id`,`patient_id`),
CONSTRAINT `FK_fa_patient` FOREIGN KEY (`patient_id`) REFERENCES `patient` (`id`),
CONSTRAINT `FK_fa_categorie` FOREIGN KEY (`cat_id`) REFERENCES `categorie` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `fa_import` (
`noip` bigint(10) unsigned zerofill NOT NULL,
`cat` varchar(50) NOT NULL,
PRIMARY KEY (`noip`,`cat`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;
CREATE TABLE IF NOT EXISTS `patient` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`noip` bigint(10) unsigned zerofill NOT NULL,
`cp_id` smallint(5) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `FK_patient_cp` (`cp_id`),
CONSTRAINT `FK_patient_cp` FOREIGN KEY (`cp_id`) REFERENCES `cp` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=262141 DEFAULT CHARSET=utf8;
/*!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 */;
Here the explain query :
Here a modification improving performance (select categorie.id instead of categorie.cat):
Here the best best query I found thanks #RickJames & #BillKarwin
select categorie.cat
,dhu_type.`type`
,t.`2013`
,t.`2014`
,t.`2015`
,t.`2016`
from ( select fa.cat_id as catid
,dhu.type_id typid
,COUNT(DISTINCT(
CASE WHEN dhu.date between '2013-01-01' and '2013-12-31'
THEN dhu.id
END )
) AS "2013"
,COUNT(DISTINCT(
CASE WHEN dhu.date between '2014-01-01' and '2014-12-31'
THEN dhu.id
END )
) AS "2014"
,COUNT(DISTINCT(
CASE WHEN dhu.date between '2015-01-01' and '2015-12-31'
THEN dhu.id
END )
) AS "2015"
,COUNT(DISTINCT(
CASE WHEN dhu.date between '2016-01-01' and '2016-12-31'
THEN dhu.id
END )
) AS "2016"
from dhu
inner join patient on dhu.patient_id=patient.id
inner join fa on patient.id = fa.patient_id
group by fa.cat_id, dhu.type_id ) t
inner join categorie on t.catid = categorie.id
inner join dhu_type on t.typid = dhu_type.id
order by categorie.cat,dhu_type.`type`
MySQL does just fine with billion-row tables.
Any database engine is at the mercy of the speed of the disk and how much (or little) RAM you have for caching.
The textbooks say to normalize everything, but I suggest that a 4-char type is not worth normalizing. Ditto for the 5-char cp.
Unless you really want output rows with all zeros, add this WHERE dhu.date between '2016-01-01' and '2016-12-31' before the GROUP BY.
Follow my advice here on many:many schema design (fa). This may speed up the query for MySQL. (I don't know if the same principles apply to Postgres.)

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

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;

Use of Min() and Max() with MySQL Stored Procedure

I have a simple table which stores results of subject, so one user may have done few subject and they may have different marks. I want to create a stored procedure to retrieve min and max subject details for given student:
DELIMITER $$
CREATE DEFINER=`root`#`localhost`
PROCEDURE `Search_Min_Marks_For_Student`(IN Student_code SMALLINT)
BEGIN
SELECT Subject_Subject_code AS `Minimum Scored Subject`,
Subject_title AS `Subject Title`,
Min(Total_mk) AS Marks
FROM result,subject
WHERE result.Student_Student_code = Student_code AND
Subject_Subject_code=Subject_code;
END
I wrote one for minimum score and it works fine, but is there a way I can add max results to the same query?
-- Table structure for table `result`
--
DROP TABLE IF EXISTS `result`;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `result` (
`Result_code` smallint(6) NOT NULL AUTO_INCREMENT,
`Student_Student_code` smallint(6) NOT NULL,
`Subject_Subject_code` smallint(6) NOT NULL,
`Practical_mk` smallint(6) NOT NULL,
`Assignment_mk` smallint(6) NOT NULL,
`Exam_mk` smallint(6) NOT NULL,
`Total_mk` smallint(6) NOT NULL,
`Grade` varchar(20) NOT NULL,
PRIMARY KEY (`Result_code`),
KEY `fk_Result_Subject1_idx` (`Subject_Subject_code`),
KEY `fk_Result_Student1_idx` (`Student_Student_code`),
CONSTRAINT `fk_Result_Student1` FOREIGN KEY (`Student_Student_code`) REFERENCES `student` (`Student_code`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_Result_Subject1` FOREIGN KEY (`Subject_Subject_code`) REFERENCES `subject` (`Subject_code`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = #saved_cs_client */;
--
-- Table structure for table `subject`
--
DROP TABLE IF EXISTS `subject`;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `subject` (
`Subject_code` smallint(6) NOT NULL AUTO_INCREMENT,
`Subject_title` varchar(50) NOT NULL,
`Num_of_credits` smallint(6) NOT NULL,
`Description` varchar(100) DEFAULT NULL,
`Course_Course_code` smallint(6) NOT NULL,
`Department_Dep_code` smallint(6) NOT NULL,
PRIMARY KEY (`Subject_code`),
KEY `fk_Subject_Course1_idx` (`Course_Course_code`),
KEY `fk_Subject_Department1_idx` (`Department_Dep_code`),
CONSTRAINT `fk_Subject_Course1` FOREIGN KEY (`Course_Course_code`) REFERENCES `course` (`Course_code`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_Subject_Department1` FOREIGN KEY (`Department_Dep_code`) REFERENCES `department` (`Dep_code`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=8004 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = #saved_cs_client */;
You can get both using a (somewhat verbose) join, with their corresponding code and title. This query returns the best and worst subject as two separate rows;
SELECT DISTINCT s.Subject_Subject_code, s.Subject_title, r1.Total_mk
FROM result r1
LEFT JOIN result r2
ON r1.Total_mk > r2.Total_mk
AND r1.Student_Student_code = r2.Student_Student_code
LEFT JOIN result r3
ON r1.Total_mk < r3.Total_mk
AND r1.Student_Student_code = r3.Student_Student_code
JOIN Subject s
ON r1.Subject_code=s.Subject_Subject_code
WHERE (r2.Total_mk IS NULL OR r3.Total_mk IS NULL)
AND r1.Student_Student_code = 1;
What it does is basically select the rows where there exists no lower score (the r2 join) or there exists no better score (the r3 join) for the student, and join with subject to get the data to display.
An SQLfiddle to test with.