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

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.

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.

Creating MYSQL table with foreign key ERROR

CREATE TABLE `u914452720_yzawa`.`aucs_manufacturer_files`
( `id` INT NOT NULL AUTO_INCREMENT , `manufacturer_id` INT NOT NULL,
FOREIGN KEY ('manufacturer_id') REFERENCES aucs_manufacturer(manufacturer_id) ON DELETE CASCADE)
ENGINE=INNODB;
got error:
SQL query:
CREATE TABLE `u914452720_yzawa`.`aucs_manufacturer_files`
( `id` INT NOT NULL AUTO_INCREMENT , `manufacturer_id` INT NOT NULL,
FOREIGN KEY ('manufacturer_id') REFERENCES aucs_manufacturer(manufacturer_id) ON DELETE CASCADE)
ENGINE=INNODB
MySQL atsakymas: Dokumentacija
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''manufacturer_id') REFERENCES aucs_manufacturer(manufacturer_id) ON DELETE CASCA' at line 3
EXPORTED TABLE FROM WHICH I WANT TO GET MANUFACTURER_ID:
-- phpMyAdmin SQL Dump
-- version 4.9.2
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1:3306
-- Generation Time: 2019 m. Grd 16 d. 11:37
-- Server version: 10.2.27-MariaDB
-- PHP Version: 7.2.23
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: `u914452720_yzawa`
--
-- --------------------------------------------------------
--
-- Sukurta duomenų struktūra lentelei `aucs_manufacturer`
--
CREATE TABLE `aucs_manufacturer` (
`manufacturer_id` int(11) NOT NULL,
`name` varchar(64) NOT NULL,
`image` varchar(255) DEFAULT NULL,
`instruction` varchar(255) DEFAULT NULL,
`catalog` varchar(255) DEFAULT NULL,
`sketch` varchar(255) DEFAULT NULL,
`sort_order` int(3) NOT NULL,
`id` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `aucs_manufacturer`
--
ALTER TABLE `aucs_manufacturer`
ADD PRIMARY KEY (`manufacturer_id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `aucs_manufacturer`
--
ALTER TABLE `aucs_manufacturer`
MODIFY `manufacturer_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=31;
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 */;
Probably the single quotes might not be necessary and both the referenced table and the new table that is being created have to be on the same Engine (MyISAM in this case).
CREATE TABLE `aucs_manufacturer_files`
( `id` INT PRIMARY KEY AUTO_INCREMENT , `manufacturer_id` INT NOT NULL,
FOREIGN KEY (manufacturer_id) REFERENCES aucs_manufacturer(manufacturer_id) ON DELETE CASCADE)
ENGINE=MyISAM DEFAULT CHARSET=utf8;
Without knowing a lot about the other table that you are trying to reference, it's hard for us to guess certain information. I put together this SQLFiddle to demonstrate a working solution.
create table am (
id int unique not null auto_increment
)
ENGINE=INNODB;
CREATE TABLE amf
( `id` INT unique NOT NULL AUTO_INCREMENT , `manufacturer_id` INT NOT NULL,
FOREIGN KEY (manufacturer_id) REFERENCES am(id) ON DELETE CASCADE)
ENGINE=INNODB;
insert into am(id) values (null),(null),(null),(null),(null),(null),(null);
insert into amf(id, manufacturer_id) VALUES (null, 1),(null, 2),(null, 3),(null, 2),(null, 1),(null, 4),(null, 6);
delete from am where id = 6;
If you run the SELECT * from amf you will see that there are no values with the manufacturer_id as 6, meaning that the cascade worked correctly.
try this, hope this would work:
CREATE TABLE `u914452720_yzawa`.`aucs_manufacturer_files`
( `id` INT NOT NULL AUTO_INCREMENT , `manufacturer_id` INT,
FOREIGN KEY (manufacturer_id) REFERENCES aucs_manufacturer(manufacturer_id) ON DELETE CASCADE)
ENGINE=INNODB
After you fixed the single quotes that should have been backticks or removed altogether, you have:
CREATE TABLE `u914452720_yzawa`.`aucs_manufacturer_files` (
`id` INT NOT NULL AUTO_INCREMENT ,
`manufacturer_id` INT NOT NULL,
FOREIGN KEY ('manufacturer_id') REFERENCES aucs_manufacturer(manufacturer_id) ON DELETE CASCADE
) ENGINE=INNODB;
And you are getting this error:
#1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key
This has nothing to do with the foreign key; it results from specifying auto increment for a column that is not a key. Presumably you meant to say:
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
(If you end up with yet another error after fixing this, please make sure to edit your question with the new create statement, error message, and output from show create table aucs_manufacturer.)

MySQL Procedure with parameter syntax

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!

Max values of column

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.

Cannot Create Table after Lowercased all text in MySQL Dump file

I'm having an error restoring tables which have Foreign Keys after lowercasing the contents of a mysql dump file.
My goal is to lowercase all structure in a mysql database.
I used vim to lowercase all contents of a mysqldump file when trying to restore the database, this is the error I get.
$ mysql -u [user] -p[password] dme < mysqldump.sql
ERROR 1005 (HY000) at line 399: Can't create table 'dme.contacts' (errno: 121)
The restore is error'ing out on any table which has Foreign Key contstraints. If I remove lines after the Primary Key for each table, the entire restore works fine.
My question is, what is causing this and how can I keep my Foreign Keys in the process?
Lowercased.sql example which results in the error (fyi prior to dme.contacts there are multiple tables which are created just fine. Only tables w/ Foreign Keys and Contstraints have issues.)
drop table if exists `contacts`;
/*!40101 set #saved_cs_client = ##character_set_client */;
/*!40101 set character_set_client = utf8 */;
create table `contacts` (
`contactid` int(11) not null auto_increment,
`firstname` varchar(100) default null,
`lastname` varchar(100) not null,
`fullname` varchar(200) not null,
`dob` datetime default null,
`sex` char(1) default null,
`voided` datetime default null,
`vendorid` int(11) not null,
`contacttypeid` int(11) default null,
`comments` varchar(1500) default null,
primary key (`contactid`),
key `fk_contacts_vendors` (`vendorid`),
key `fk_contacts_contacttype` (`contacttypeid`),
constraint `fk_contacts_contacttype` foreign key (`contacttypeid`) references `contacttype` (`contacttypeid`) on delete no action on update no action,
constraint `fk_contacts_vendors` foreign key (`vendorid`) references `vendors` (`vendorid`) on delete no action on update no action
) engine=innodb default charset=utf8;
/*!40101 set character_set_client = #saved_cs_client */;
--
-- dumping data for table `contacts`
--
lock tables `contacts` write;
/*!40000 alter table `contacts` disable keys */;
/*!40000 alter table `contacts` enable keys */;
unlock tables;
Original from the mysqldump.sql (No Error)
--
-- Table structure for table `Contacts`
--
DROP TABLE IF EXISTS `Contacts`;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `Contacts` (
`ContactId` int(11) NOT NULL AUTO_INCREMENT,
`FirstName` varchar(100) DEFAULT NULL,
`LastName` varchar(100) NOT NULL,
`FullName` varchar(200) NOT NULL,
`DOB` datetime DEFAULT NULL,
`Sex` char(1) DEFAULT NULL,
`Voided` datetime DEFAULT NULL,
`VendorID` int(11) NOT NULL,
`ContactTypeID` int(11) DEFAULT NULL,
`Comments` varchar(1500) DEFAULT NULL,
PRIMARY KEY (`ContactId`),
KEY `FK_Contacts_Vendors` (`VendorID`),
KEY `FK_Contacts_ContactType` (`ContactTypeID`),
CONSTRAINT `FK_Contacts_ContactType` FOREIGN KEY (`ContactTypeID`) REFERENCES `ContactType` (`ContactTYpeID`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `FK_Contacts_Vendors` FOREIGN KEY (`VendorID`) REFERENCES `Vendors` (`VendorId`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = #saved_cs_client */;
--
-- Dumping data for table `Contacts`
--
LOCK TABLES `Contacts` WRITE;
/*!40000 ALTER TABLE `Contacts` DISABLE KEYS */;
/*!40000 ALTER TABLE `Contacts` ENABLE KEYS */;
UNLOCK TABLES;
Try removing the constrain name. Sample:
constraint `fk_contacts_contacttype` foreign key (`contacttypeid`) ....
To
constraint foreign key (`contacttypeid`) ...