Getting a Cannot add Foreign Key Constraint MySQL - mysql

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

Columns referenced in a foreign key have to be indexed. You don't have an index on Customer_Number in the customer table. Either change the order of the columns in the composite primary key to(Customer_Number, Scout_Number)or add an additional key just on theCustomer_Number` column.
Note, however, that having a foreign key pointing to a non-unique column is a MySQL extension to SQL, and likely to be a bad idea. See Can a foreign key reference a non-unique index?. I wonder why the primary key of the customer table isn't just Customer_Number.

Related

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

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

mysql update parent updates all column values in child

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

Wordpress thousand of categories crashing site

I have a big website, at the moment i have about 10k unique visitors daily, I have 2000 categories, 12k Tags, 36k custom taxonomies.
It's not a design flaw, as its a website to watch online movies, I used, categories for Seasons and stuff like that, the custom taxonomies are Actors, regizors etc.
The problem is that my hosting keeps crashing with 100% processor, the resources cannot be an issue, as i have my own server with 4xXeon, 64 GB ram.
I have isolated the problem, and it's due to very slow queries for example, what are my options? :
# Time: 160830 17:01:20
# User#Host: razvypp[razvypp] # localhost []
# Query_time: 9.159090 Lock_time: 0.001680 Rows_sent: 30 Rows_examined: 420117
SET timestamp=1472565680;
SELECT wphn_posts.ID
FROM wphn_posts
INNER JOIN wphn_term_relationships
ON (wphn_posts.ID = wphn_term_relationships.object_id)
WHERE 1=1
AND ( wphn_term_relationships.term_taxonomy_id IN (3630,
4955,4956,4957,4958,4959,4960,4961,4962,4963,4964,4965,
4966,4967,4968,4969,4970,4971,4972,4973,4974,4975,4976,
4977,4978,4979,4980,4981,4982,4983,4984,4985,4986,4987,
4988,4989,4990,4991,4992,4993,4994,4995,4996,4997,4998,
4999,5000,5001,5002,5003,5004,5005,5006,5007,5008,5009,
5010,5011,5012, ... (hundreds of numbers) ...,49740,49779)
Here is a dump of the tables
-- phpMyAdmin SQL Dump
-- version 4.4.15
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: 30 Aug 2016 la 15:17
-- Versiune server: 5.5.51-log
-- PHP Version: 5.5.37
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 utf8mb4 */;
--
-- Database: `trolio`
--
-- --------------------------------------------------------
--
-- Structura de tabel pentru tabelul `wphn_posts`
--
CREATE TABLE IF NOT EXISTS `wphn_posts` (
`ID` bigint(20) unsigned NOT NULL,
`movie_type` int(2) NOT NULL,
`post_author` bigint(20) unsigned NOT NULL DEFAULT '0',
`post_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`post_date_gmt` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`post_content` longtext NOT NULL,
`post_title` text NOT NULL,
`post_excerpt` text NOT NULL,
`post_status` varchar(20) NOT NULL DEFAULT 'publish',
`comment_status` varchar(20) NOT NULL DEFAULT 'open',
`ping_status` varchar(20) NOT NULL DEFAULT 'open',
`post_password` varchar(20) NOT NULL DEFAULT '',
`post_name` varchar(200) NOT NULL DEFAULT '',
`to_ping` text NOT NULL,
`pinged` text NOT NULL,
`post_modified` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`post_modified_gmt` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`post_content_filtered` longtext NOT NULL,
`post_parent` int(20) unsigned NOT NULL DEFAULT '0',
`guid` varchar(255) NOT NULL DEFAULT '',
`menu_order` int(11) NOT NULL DEFAULT '0',
`post_type` varchar(20) NOT NULL DEFAULT 'post',
`post_mime_type` varchar(100) NOT NULL DEFAULT '',
`comment_count` bigint(20) NOT NULL DEFAULT '0',
`views` int(99) NOT NULL,
`views_this_week` int(5) NOT NULL,
`imdb` double NOT NULL,
`imdb_votes` float NOT NULL,
`imdb_id` varchar(20) NOT NULL,
`fb_shares_root` int(5) NOT NULL,
`movie_name` varchar(140) NOT NULL,
`season` int(2) NOT NULL,
`episode` int(5) NOT NULL,
`serial_id` int(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Structura de tabel pentru tabelul `wphn_term_relationships`
--
CREATE TABLE IF NOT EXISTS `wphn_term_relationships` (
`object_id` bigint(20) unsigned NOT NULL DEFAULT '0',
`term_taxonomy_id` bigint(20) unsigned NOT NULL DEFAULT '0',
`term_order` int(11) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Structura de tabel pentru tabelul `wphn_term_taxonomy`
--
CREATE TABLE IF NOT EXISTS `wphn_term_taxonomy` (
`term_taxonomy_id` bigint(20) unsigned NOT NULL,
`term_id` bigint(20) unsigned NOT NULL DEFAULT '0',
`taxonomy` varchar(32) NOT NULL DEFAULT '',
`description` longtext NOT NULL,
`parent` bigint(20) unsigned NOT NULL DEFAULT '0',
`count` bigint(20) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `wphn_posts`
--
ALTER TABLE `wphn_posts`
ADD PRIMARY KEY (`ID`),
ADD KEY `post_name` (`post_name`(191)),
ADD KEY `type_status_date` (`post_type`,`post_status`,`post_date`,`ID`),
ADD KEY `post_parent` (`post_parent`),
ADD KEY `post_author` (`post_author`),
ADD KEY `serial_id` (`serial_id`),
ADD KEY `episode` (`episode`),
ADD KEY `season` (`season`),
ADD KEY `movie_name` (`movie_name`);
--
-- Indexes for table `wphn_term_relationships`
--
ALTER TABLE `wphn_term_relationships`
ADD PRIMARY KEY (`object_id`,`term_taxonomy_id`),
ADD KEY `term_taxonomy_id` (`term_taxonomy_id`),
ADD KEY `term_order` (`term_order`);
--
-- Indexes for table `wphn_term_taxonomy`
--
ALTER TABLE `wphn_term_taxonomy`
ADD PRIMARY KEY (`term_taxonomy_id`),
ADD UNIQUE KEY `term_id_taxonomy` (`term_id`,`taxonomy`),
ADD KEY `taxonomy` (`taxonomy`),
ADD KEY `parent` (`parent`),
ADD KEY `count` (`count`),
ADD KEY `term_id` (`term_id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `wphn_posts`
--
ALTER TABLE `wphn_posts`
MODIFY `ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table `wphn_term_taxonomy`
--
ALTER TABLE `wphn_term_taxonomy`
MODIFY `term_taxonomy_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT;
/*!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 */;
"Rows examined" = 420K -- That smacks of a missing index.
Probably missing:
INDEX(term_taxonomy_id, object_id)
Please provide SHOW CREATE TABLE for the two tables.
What is the value of innodb_buffer_pool_size?

#1064 SQL Syntax error

I understand this is a common and annoying problem so before I ask for an explanation I just want to say sorry.
As the title says I've been getting a 1064 syntax error from my MySQL db I imported in to php myadmin running wamp and building through netbeans.
I've checked out how other posts on the matter and have done reading about proper syntax but cannot see what my problem is.
An explanation would be greatly appreciated as I really want to learn this and improve my skills.
(if you have any questions about my code please ask)
Here's my full code:
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: `mydb`
--
CREATE DATABASE IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
USE `mydb`;
-- --------------------------------------------------------
--
-- Table structure for table `follow`
--
DROP TABLE IF EXISTS `follow`;
CREATE TABLE IF NOT EXISTS `follow` (
`follow_id` int(11) NOT NULL,
`follow_current_user` varchar(255) NOT NULL,
`follow_profile_user` varchar(255) NOT NULL,
`follow_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=17 ;
--
-- Dumping data for table `follow`
--
INSERT INTO `follow` (`follow_id`, `follow_current_user`, `follow_profile_user`, `follow_timestamp`) VALUES
(13, 'dude', 'the', '2015-10-10 03:09:58'),
-- --------------------------------------------------------
--
-- Table structure for table `user`
--
DROP TABLE IF EXISTS `user`;
CREATE TABLE IF NOT EXISTS `user` (
`user_id` int(11) NOT NULL,
`user_email` varchar(255) NOT NULL,
`user_password` varchar(255) NOT NULL,
`user_firstname` text NOT NULL,
`user_lastname` text NOT NULL,
`user_avatar` varchar(255) NOT NULL,
`user_shortbio` text,
`user_username` varchar(255) NOT NULL,
`user_longbio` text,
`user_facebook` varchar(255) DEFAULT NULL,
`user_twitter` varchar(255) DEFAULT NULL,
`user_linkedin` varchar(255) DEFAULT NULL,
`user_stumbleupon` varchar(255) DEFAULT NULL,
`user_pinterest` varchar(255) DEFAULT NULL,
`user_googleplus` varchar(255) DEFAULT NULL,
`user_website` varchar(255) DEFAULT NULL,
`user_dob` date DEFAULT NULL,
`user_profession` text,
`user_gender` varchar(255) DEFAULT NULL,
`user_maritialstatus` varchar(255) DEFAULT NULL,
`user_address` text,
`user_backgroundpicture` varchar(255) NOT NULL,
`user_joindate` date NOT NULL,
`user_country` varchar(255) DEFAULT NULL,
`user_skype` varchar(255) DEFAULT NULL,
`user_github` varchar(255) DEFAULT NULL,
`user_youtube` varchar(255) DEFAULT NULL,
`user_vimeo` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=18 ;
--
-- Dumping data for table `user`
--
INSERT INTO `user` (`user_id`, `user_email`, `user_password`, `user_firstname`, `user_lastname`, `user_avatar`, `user_shortbio`, `user_username`, `user_longbio`, `user_facebook`, `user_twitter`, `user_linkedin`, `user_stumbleupon`, `user_pinterest`, `user_googleplus`, `user_website`, `user_dob`, `user_profession`, `user_gender`, `user_maritialstatus`, `user_address`, `user_backgroundpicture`, `user_joindate`, `user_country`, `user_skype`, `user_github`, `user_youtube`, `user_vimeo`) VALUES
(1, 'me#gmail.com', '123password123', 'Forname', 'Surname', '1470024_601216136604826_1065650331_n-884862517.jpg', 'Lorem ipsum.', 'username', 'Lorem ipsum', NULL, NULL, NULL, NULL, NULL, NULL, 'mysite.com', '1987-11-11', 'Student', 'Male', NULL, 'kablah', 'untitled-90969185.jpg', '2015- 11-11', 'Country', NULL, NULL, NULL, NULL),
--
-- --------------------------------------------------------
--
-- Table structure for table `view`
--
DROP TABLE IF EXISTS `view`;
CREATE TABLE IF NOT EXISTS `view` (
`view_id` int(11) NOT NULL,
`view_profile_user` varchar(255) NOT NULL,
`view_current_user` varchar(255) NOT NULL,
`view_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=123 ;
--
-- Dumping data for table `view`
--
INSERT INTO `view` (`view_id`, `view_profile_user`, `view_current_user`, `view_timestamp`) VALUES
(65, 'blub', 'rugsworth', '2015-10-18 19:30:56'),
--
-- Indexes for dumped tables
--
--
-- Indexes for table `follow`
--
ALTER TABLE `follow`
ADD PRIMARY KEY (`follow_id`);
--
-- Indexes for table `user`
--
ALTER TABLE `user`
ADD PRIMARY KEY (`user_id`), ADD UNIQUE KEY `user_email` (`user_email`), ADD UNIQUE KEY `user_username` (`user_username`);
--
-- Indexes for table `view`
--
ALTER TABLE `view`
ADD PRIMARY KEY (`view_id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `follow`
--
ALTER TABLE `follow`
MODIFY `follow_id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=17;
--
-- AUTO_INCREMENT for table `user`
--
ALTER TABLE `user`
MODIFY `user_id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=18;
--
-- AUTO_INCREMENT for table `view`
--
ALTER TABLE `view`
MODIFY `view_id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=123;
/*!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 just the part of my code I think is causing the problem:
-- Dumping data for table `user`
--
INSERT INTO `user` (`user_id`, `user_email`, `user_password`, `user_firstname`, `user_lastname`, `user_avatar`, `user_shortbio`, `user_username`, `user_longbio`, `user_facebook`, `user_twitter`, `user_linkedin`, `user_stumbleupon`, `user_pinterest`, `user_googleplus`, `user_website`, `user_dob`, `user_profession`, `user_gender`, `user_maritialstatus`, `user_address`, `user_backgroundpicture`, `user_joindate`, `user_country`, `user_skype`, `user_github`, `user_youtube`, `user_vimeo`) VALUES
(1, 'me#gmail.com', '123password123', 'Forname', 'Surname', '1470024_601216136604826_1065650331_n-884862517.jpg', 'Lorem ipsum.', 'username', 'Lorem ipsum', NULL, NULL, NULL, NULL, NULL, NULL, 'mysite.com', '1987-11-11', 'Student', 'Male', NULL, 'kablah', 'untitled-90969185.jpg', '2015- 11-11', 'Country', NULL, NULL, NULL, NULL),
--
-- --------------------------------------------------------
--
-- Table structure for table `view`
--
DROP TABLE IF EXISTS `view`;
CREATE TABLE IF NOT EXISTS `view` (
`view_id` int(11) NOT NULL,
`view_profile_user` varchar(255) NOT NULL,
`view_current_user` varchar(255) NOT NULL,
`view_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=123 ;
--
-- Dumping data for table `view`
--
INSERT INTO `view` (`view_id`, `view_profile_user`, `view_current_user`, `view_timestamp`) VALUES
(65, 'blub', 'rugsworth', '2015-10-18 19:30:56'),
You have a comma at the end of your INSERT INTO 'view' statement instead of a semicolon.
Should be:
INSERT INTO `view` (`view_id`, `view_profile_user`, `view_current_user`, `view_timestamp`)
VALUES (65, 'blub', 'rugsworth', '2015-10-18 19:30:56');
Does that fix it up?

Issue converting table from InnoDB to MyISAM (there can be only one auto column and it must be defined as a key)

I'm having issues trying to convert a table from MyISAM to InnoDB in MySQL 5.6.
The following is the table dump:
--
-- Table structure for table `companies`
--
DROP TABLE IF EXISTS `companies`;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `companies` (
`uid` int(20) NOT NULL,
`cid` int(20) NOT NULL AUTO_INCREMENT,
`cname` varchar(500) NOT NULL,
`rfc` varchar(20) NOT NULL,
`address` varchar(1000) NOT NULL,
`dbUseExternal` tinyint(1) NOT NULL DEFAULT '0',
`dbHost` varchar(50) NOT NULL,
`dbPort` varchar(50) NOT NULL,
`dbUser` varchar(50) NOT NULL,
`dbPass` varchar(50) NOT NULL,
`dbSSL` varchar(50) NOT NULL,
`dbDriver` varchar(50) NOT NULL,
`dbName` varchar(50) NOT NULL,
`status` int(10) NOT NULL,
PRIMARY KEY (`uid`,`cid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = #saved_cs_client */;
It works as MyISAM. But, if I try to convert it to InnoDB (or if I try editing this dump to insert it on the command line as a SQL file), I get the following error:
Incorrect table definition; there can be only one auto column and it must be defined as a key
I understand that error - or at least I thought I did. I'm not really using more than one AUTO_INCREMENT column and it is defined as primary key.
Also, the information I've found regarding the error is always because of an obvious missing key or a duplicate AUTO_INCREMENT definition. One more thing I see generally commented is that the same is true for MyISAM and InnoDB.
So, why does it work for MyISAM and not for InnoDB?
in InnoDB, auto_increment keys have to have to: either have their own index, or at least be the primary sub-index of a compound index. (a in KEY(a,b)).
Fix this by adding a KEY(cid) after your PRIMARY KEY, like so:
CREATE TABLE `companies2` (
`uid` int(20) NOT NULL,
`cid` int(20) NOT NULL AUTO_INCREMENT,
`cname` varchar(500) NOT NULL,
`rfc` varchar(20) NOT NULL,
`address` varchar(1000) NOT NULL,
`dbUseExternal` tinyint(1) NOT NULL DEFAULT '0',
`dbHost` varchar(50) NOT NULL,
`dbPort` varchar(50) NOT NULL,
`dbUser` varchar(50) NOT NULL,
`dbPass` varchar(50) NOT NULL,
`dbSSL` varchar(50) NOT NULL,
`dbDriver` varchar(50) NOT NULL,
`dbName` varchar(50) NOT NULL,
`status` int(10) NOT NULL,
PRIMARY KEY (`uid`,`cid`),
KEY(`cid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;