MySQL Procedure with parameter syntax - mysql

I have a schema with lists of characters from star wars, the movies they appear in, the planets they visit, etc. Here is the schema:
CREATE DATABASE IF NOT EXISTS `starwarsFINAL` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `starwarsFINAL`;
DROP TABLE IF EXISTS `characters`;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `characters` (
`character_name` varchar(45) NOT NULL,
`race` varchar(45) DEFAULT NULL,
`homeworld` varchar(45) DEFAULT 'Unknown',
`affiliation` varchar(45) DEFAULT NULL,
PRIMARY KEY (`character_name`),
KEY `planet_fk` (`homeworld`),
CONSTRAINT `planet_fk` FOREIGN KEY (`homeworld`) REFERENCES `planets` (`planet_name`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = #saved_cs_client */;
DROP TABLE IF EXISTS `movies`;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `movies` (
`movie_id` int(11) NOT NULL,
`title` varchar(128) DEFAULT NULL,
`scenes_in_db` int(11) DEFAULT NULL,
`scenes_in_movies` int(11) DEFAULT NULL,
PRIMARY KEY (`movie_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = #saved_cs_client */;
DROP TABLE IF EXISTS `planets`;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `planets` (
`planet_name` varchar(45) NOT NULL,
`planet_type` varchar(30) DEFAULT NULL,
`affiliation` varchar(30) DEFAULT NULL,
PRIMARY KEY (`planet_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = #saved_cs_client */;
DROP TABLE IF EXISTS `timetable`;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `timetable` (
`character_name` varchar(45) DEFAULT NULL,
`planet_name` varchar(45) DEFAULT 'Unknown',
`movie_id` int(11) DEFAULT NULL,
`arrival` int(11) DEFAULT NULL,
`departure` int(11) DEFAULT NULL,
`time_id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`time_id`),
UNIQUE KEY `timetable_un` (`character_name`,`planet_name`,`movie_id`,`arrival`),
KEY `timetable_fkplanet` (`planet_name`),
KEY `timetable_fkmovie` (`movie_id`),
CONSTRAINT `timetable_fkcharacter` FOREIGN KEY (`character_name`) REFERENCES `characters` (`character_name`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `timetable_fkmovie` FOREIGN KEY (`movie_id`) REFERENCES `movies` (`movie_id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `timetable_fkplanet` FOREIGN KEY (`planet_name`) REFERENCES `planets` (`planet_name`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=57 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = #saved_cs_client */;
Here's my problem:
Write a procedure track_planet(planet) that accepts a planet name and returns
a result set that contain the planet name, the movie name, and the number of
characters that appear on that planet during that movie.
Here's what I have so far, but I'm lost/stuck on how to make it work. (I'm new to MySQL)
DROP PROCEDURE IF EXISTS track_planet;
DELIMITER $$
CREATE PROCEDURE track_planet(IN planet VARCHAR(45))
BEGIN
SELECT planet_name FROM planets, title FROM movies,
COUNT(DISTINCT character_name) FROM characters WHERE planet_name = planet;
END
DELIMITER;

Hello_ friend.
First thanks for posting the schema of your database it really helps. From it I was able to made this picture:
And I've changed your code to this:
DELIMITER $$
DROP PROCEDURE IF EXISTS track_planet $$
CREATE PROCEDURE track_planet(IN planet VARCHAR(45))
BEGIN
SELECT
tt.planet_name as planetName, m.title as movieName, COUNT(tt.character_name) as characters
FROM timetable tt
LEFT JOIN movies m ON tt.movie_id = m.movie_id
WHERE tt.planet_name = planet
GROUP BY planetName, movieName;
END $$
DELIMITER ;
The code for the function declaration is pretty much the same except few modifications.
The major change is the SELECT statement:
selecting the necessary columns;
seems that you have most of the information inside timetable so only join movies to get movie name;
use GROUP BY first on planet name and then on movie name so it will count characters on a specific planet by specific movie
and that's it ...
Hope this was helpful. Let me know if it works for you.
Cheers!

Related

MySQL dump file has some issue with foreign key constraint. referenced column '**' in foreign key constraint '**' are incompatible

I would like to import SQL dump file, some how it's not working.
I got this error when I try to import data.
Referencing column 'originator_id' and referenced column 'name' in foreign key constraint 'fk_rails_79d8c4ccda' are incompatible.
DROP TABLE IF EXISTS `loan_fee_shares`;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `loan_fee_shares` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`originator_id` varchar(255) DEFAULT NULL,
`percent` decimal(6,2) DEFAULT NULL COMMENT 'belongs to [0,1] set',
`active` tinyint(1) DEFAULT '0',
`deactivated_at` timestamp NULL DEFAULT NULL,
`created_at` datetime NOT NULL,
`updated_at` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_rails_79d8c4ccda` (`originator_id`),
CONSTRAINT `fk_rails_79d8c4ccda` FOREIGN KEY (`originator_id`) REFERENCES `originators` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = #saved_cs_client */;
DROP TABLE IF EXISTS `originators`;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `originators` (
`name` varchar(255) DEFAULT NULL,
`description` varchar(100) DEFAULT NULL,
`created_at` datetime NOT NULL,
`updated_at` datetime NOT NULL,
PRIMARY KEY (`name`),
UNIQUE KEY `index_originators_on_name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = #saved_cs_client */;
I am posting this for someone who might have the same issue in the future.
Barmar has a good point. and I moved originators table to before loan_fee_shares. But I still had an issue.
and It had an issue with DEFAULT CHARSET=latin1, I had to change it to DEFAULT CHARSET=utf8. and It works.
I suggest that change all your tables to the same character set.
How to change all the tables in my database to UTF8 character set?

Problem with importing mysql workbench data

I have a table and data. When importing, automatically importing data into the user table throws a foreign key error. How do I load data into a table?
I understand that the problem is in the foreign key and the difference in id and name, but how to import the data?
need to write some kind of request?
This is the csv of the loaded data
Administrator,j.doe#amonic.com,123,John,Doe,Abu dhabi,1/13/1983,1
User,k.omar#amonic.com,4258,Karim,Omar,Abu dhabi,3/19/1980,1
User,h.saeed#amonic.com,2020,Hannan,Saeed,Cairo,12/20/1989,1
User,a.hobart#amonic.com,6996,Andrew,Hobart,Riyadh,1/30/1990,1
User,k.anderson#amonic.com,4570,Katrin,Anderson,Doha,11/10/1992,1
User,h.wyrick#amonic.com,1199,Hava,Wyrick,Abu dhabi,8/8/1988,1
User,marie.horn#amonic.com,55555,Marie,Horn,Bahrain,4/6/1981,1
User,m.osteen#amonic.com,9800,Milagros,Osteen,Abu dhabi,2/3/1991,0
and this is the mysql code
-- Dumping data for table `countries`
--
LOCK TABLES `countries` WRITE;
/*!40000 ALTER TABLE `countries` DISABLE KEYS */;
INSERT INTO `countries` VALUES (1,'Afghanistan'),(2,'Albania'),(3,'Algeria'),(4,'Andorra'),(5,'Angola'),(6,'Antigua & Deps'),(7,'Argentina'),(8,'Armenia'),(9,'Australia'),(10,'Austria'),(11,'Azerbaijan'),(12,'Bahamas'),(13,'Bahrain'),(14,'Bangladesh'),(15,'Barbados'),(16,'Belarus'),(17,'Belgium'),(18,'Belize'),(19,'Benin'),(20,'Bhutan'),(21,'Bolivia'),(22,'Bosnia Herzegovina'),(23,'Botswana'),(24,'Brazil'),(25,'Brunei'),(26,'Bulgaria'),(27,'Burkina'),(28,'Burundi'),(29,'Cambodia'),(30,'Cameroon'),(31,'Canada'),(32,'Cape Verde'),(33,'Central African Rep'),(34,'Chad'),(35,'Chile'),(36,'China'),(37,'Colombia'),(38,'Comoros'),(39,'Congo'),(40,'Congo {Democratic Rep}'),(41,'Costa Rica'),(42,'Croatia'),(43,'Cuba'),(44,'Cyprus'),(45,'Czech Republic'),(46,'Denmark'),(47,'Djibouti'),(48,'Dominica'),(49,'Dominican Republic'),(50,'East Timor'),(51,'Ecuador'),(52,'Egypt'),(53,'El Salvador'),(54,'Equatorial Guinea'),(55,'Eritrea'),(56,'Estonia'),(57,'Ethiopia'),(58,'Fiji'),(59,'Finland'),(60,'France'),(61,'Gabon'),(62,'Gambia'),(63,'Georgia'),(64,'Germany'),(65,'Ghana'),(66,'Greece'),(67,'Grenada'),(68,'Guatemala'),(69,'Guinea'),(70,'Guinea-Bissau'),(71,'Guyana'),(72,'Haiti'),(73,'Honduras'),(74,'Hungary'),(75,'Iceland'),(76,'India'),(77,'Indonesia'),(78,'Iran'),(79,'Iraq'),(80,'Ireland {Republic}'),(81,'Israel'),(82,'Italy'),(83,'Ivory Coast'),(84,'Jamaica'),(85,'Japan'),(86,'Jordan'),(87,'Kazakhstan'),(88,'Kenya'),(89,'Kiribati'),(90,'Korea North'),(91,'Korea South'),(92,'Kosovo'),(93,'Kuwait'),(94,'Kyrgyzstan'),(95,'Laos'),(96,'Latvia'),(97,'Lebanon'),(98,'Lesotho'),(99,'Liberia'),(100,'Libya'),(101,'Liechtenstein'),(102,'Lithuania'),(103,'Luxembourg'),(104,'Macedonia'),(105,'Madagascar'),(106,'Malawi'),(107,'Malaysia'),(108,'Maldives'),(109,'Mali'),(110,'Malta'),(111,'Marshall Islands'),(112,'Mauritania'),(113,'Mauritius'),(114,'Mexico'),(115,'Micronesia'),(116,'Moldova'),(117,'Monaco'),(118,'Mongolia'),(119,'Montenegro'),(120,'Morocco'),(121,'Mozambique'),(122,'Myanmar, {Burma}'),(123,'Namibia'),(124,'Nauru'),(125,'Nepal'),(126,'Netherlands'),(127,'New Zealand'),(128,'Nicaragua'),(129,'Niger'),(130,'Nigeria'),(131,'Norway'),(132,'Oman'),(133,'Pakistan'),(134,'Palau'),(135,'Panama'),(136,'Papua New Guinea'),(137,'Paraguay'),(138,'Peru'),(139,'Philippines'),(140,'Poland'),(141,'Portugal'),(142,'Qatar'),(143,'Romania'),(144,'Russian Federation'),(145,'Rwanda'),(146,'St Kitts & Nevis'),(147,'St Lucia'),(148,'Saint Vincent & the Grenadines'),(149,'Samoa'),(150,'San Marino'),(151,'Sao Tome & Principe'),(152,'Saudi Arabia'),(153,'Senegal'),(154,'Serbia'),(155,'Seychelles'),(156,'Sierra Leone'),(157,'Singapore'),(158,'Slovakia'),(159,'Slovenia'),(160,'Solomon Islands'),(161,'Somalia'),(162,'South Africa'),(163,'South Sudan'),(164,'Spain'),(165,'Sri Lanka'),(166,'Sudan'),(167,'Suriname'),(168,'Swaziland'),(169,'Sweden'),(170,'Switzerland'),(171,'Syria'),(172,'Taiwan'),(173,'Tajikistan'),(174,'Tanzania'),(175,'Thailand'),(176,'Togo'),(177,'Tonga'),(178,'Trinidad & Tobago'),(179,'Tunisia'),(180,'Turkey'),(181,'Turkmenistan'),(182,'Tuvalu'),(183,'Uganda'),(184,'Ukraine'),(185,'United Arab Emirates'),(186,'United Kingdom'),(187,'United States'),(188,'Uruguay'),(189,'Uzbekistan'),(190,'Vanuatu'),(191,'Vatican City'),(192,'Venezuela'),(193,'Vietnam'),(194,'Yemen'),(195,'Zambia'),(196,'Zimbabwe');
/*!40000 ALTER TABLE `countries` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `offices`
--
DROP TABLE IF EXISTS `offices`;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `offices` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`CountryID` int(11) NOT NULL,
`Title` varchar(50) COLLATE utf8_bin NOT NULL,
`Phone` varchar(50) COLLATE utf8_bin NOT NULL,
`Contact` varchar(250) COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`ID`),
KEY `FK_Office_Country` (`CountryID`),
CONSTRAINT `FK_Office_Country` FOREIGN KEY (`CountryID`) REFERENCES `countries` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
/*!40101 SET character_set_client = #saved_cs_client */;
--
-- Dumping data for table `offices`
--
LOCK TABLES `offices` WRITE;
/*!40000 ALTER TABLE `offices` DISABLE KEYS */;
INSERT INTO `offices` VALUES (1,185,'Abu dhabi','638-757-8582\r\n','MIchael Malki'),(3,52,'Cairo','252-224-8525','David Johns'),(4,13,'Bahrain','542-227-5825','Katie Ballmer'),(5,142,'Doha','758-278-9597','Ariel Levy'),(6,152,'Riyadh','285-285-1474','Andrew Hobart');
/*!40000 ALTER TABLE `offices` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `roles`
--
DROP TABLE IF EXISTS `roles`;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `roles` (
`ID` int(11) NOT NULL,
`Title` varchar(50) COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
/*!40101 SET character_set_client = #saved_cs_client */;
--
-- Dumping data for table `roles`
--
LOCK TABLES `roles` WRITE;
/*!40000 ALTER TABLE `roles` DISABLE KEYS */;
INSERT INTO `roles` VALUES (1,'Administrator'),(2,'User');
/*!40000 ALTER TABLE `roles` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `users`
--
DROP TABLE IF EXISTS `users`;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `users` (
`ID` int(11) NOT NULL,
`RoleID` int(11) NOT NULL,
`Email` varchar(150) COLLATE utf8_bin NOT NULL,
`Password` varchar(50) COLLATE utf8_bin NOT NULL,
`FirstName` varchar(50) COLLATE utf8_bin DEFAULT NULL,
`LastName` varchar(50) COLLATE utf8_bin NOT NULL,
`OfficeID` int(11) DEFAULT NULL,
`Birthdate` date DEFAULT NULL,
`Active` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`ID`),
KEY `FK_Users_Offices` (`OfficeID`),
KEY `FK_Users_Roles` (`RoleID`),
CONSTRAINT `FK_Users_Offices` FOREIGN KEY (`OfficeID`) REFERENCES `offices` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `FK_Users_Roles` FOREIGN KEY (`RoleID`) REFERENCES `roles` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
/*!40101 SET character_set_client = #saved_cs_client */;
--
-- Dumping data for table `users`
--

MySQL Cannot add foreign key constraint Error Code: 1215

I am making a simple MySQL database but i keep getting an error:Cannot add foreign key constraint any help appreciated!
Also a Side question is there any difference between using symbol ` and ' while creating your database?
/*!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_MiniPost` /*!40100 DEFAULT CHARACTER SET latin1 */;
USE `a3823833_MiniPost`;
/*Table structure for table `user` */
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`userid` bigint(10) NOT NULL,
`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(11),
`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;
the bigint(10) and int(11) are not the same. make em same, and pk on parent table (as u are)

Mysql Database not creating table [messy code]

I tried to add this:
-- phpMyAdmin SQL Dump
-- version 3.4.5
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Nov 09, 2013 at 04:43 PM
-- Server version: 5.5.16
-- PHP Version: 5.3.8
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 */;
--
-- Database: `rezzion`
--
-- --------------------------------------------------------
--
-- Table structure for table `punishments`
--
CREATE TABLE IF NOT EXISTS `punishments` (
`username` varchar(20) NOT NULL AUTO_INCREMENT,
`punisher` varchar(20) NOT NULL,
`server` varchar(15) NOT NULL,
`type` int(255) NOT NULL,
`date` varchar(1024) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
/*!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 */;
to my database "rezzion" & for some reason i'm getting this error:
Anyone know why it's doing this? I have my database set to utf8-bin & it's name is rezzion... I'm confused why the code above isn't working?
Your error starts, at least, here:
`username` varchar(20) NOT NULL AUTO_INCREMENT,
This is not allowed in MySQL. And, besides, you're declaring primary key with another column:
PRIMARY KEY (`id`)
Auto Increment can only be used on a INT change it to a integer and your problem is solved
AUTO_INCREMENT my only be used with number types. You are applying it to a VARCHAR column. Also, you specify an "id" column as your primary key which does not exist.
You probably want it to look like this:
CREATE TABLE IF NOT EXISTS `punishments` (
`id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`username` varchar(20) NOT NULL,
`punisher` varchar(20) NOT NULL,
`server` varchar(15) NOT NULL,
`type` int(255) NOT NULL,
`date` varchar(1024) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

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.