How to migrate MYSQL database with having different column name? - mysql

I have two database old.sql & new.sql.
I have user table named user in new.sql. and users in old.sql.
table structure for user from new.sql
--
-- Table structure for table `user`
--
DROP TABLE IF EXISTS `user`;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `user` (
`id` int NOT NULL AUTO_INCREMENT,
`email` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`password` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`firstName` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`lastName` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`phoneNumber` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`role` enum('USER','ADMIN') COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'USER',
`createdOn` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
PRIMARY KEY (`id`),
UNIQUE KEY `User_email_key` (`email`),
UNIQUE KEY `User_phoneNumber_key` (`phoneNumber`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = #saved_cs_client */;
and users from old.sql
DROP TABLE IF EXISTS `users`;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `users` (
`id` int NOT NULL AUTO_INCREMENT,
`roleId` int NOT NULL,
`firstName` varchar(255) DEFAULT NULL,
`lastName` varchar(255) DEFAULT NULL,
`email` varchar(255) NOT NULL,
`password` varchar(255) DEFAULT NULL,
`status` tinyint(1) DEFAULT '1',
`resetPasswordExpires` datetime DEFAULT NULL,
`resetPasswordToken` varchar(255) DEFAULT NULL,
`createdAt` datetime NOT NULL,
`updatedAt` datetime NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`),
KEY `roleId` (`roleId`),
CONSTRAINT `Users_ibfk_1` FOREIGN KEY (`roleId`) REFERENCES `roles` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=884 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
/*!40101 SET character_set_client = #saved_cs_client */;
now I want to migrate user from old database to new database.
this is only one table, we have many tables to migrate data.
how can I achieve that ?
also we have done normalization in new database like
old.sql one table separated in 2 or 3 new tables in new database.
These DBs are on the same MySQL instance.

You can use a simple script like:
INSERT INTO
user (id, email, password, firstName, lastName, phoneNumber, role, createdOn)
SELECT
id, email, password, firstName, lastName, null, IF(roleId = 1, 'ADMIN', 'USER'), created
FROM users;
Just set correct roleId for condition and remove id if you need a new one.

Tiny example of the data transferring realization with some remarks: DEMO fiddle

Related

mysql foreign key constraint fails when disabled

I'm having a problem restoring data into a mysql DB from a dump. I'm running the same version of mysql, I have disabled foreign key checks, and data types are the same, but I can't understand why the check fails
Error in foreign key constraint of table staging/bookmarks:
FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `bookmarks_video_id_foreign` FOREIGN KEY (`video_id`) REFERENCES `videos` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci:
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
table schema
-- Server version 5.6.34-log
/*!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 */;
/*!40103 SET #OLD_TIME_ZONE=##TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!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 */;
...
--
-- Table structure for table `bookmarks`
--
DROP TABLE IF EXISTS `bookmarks`;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `bookmarks` (
`id` varchar(36) COLLATE utf8_unicode_ci NOT NULL,
`user_id` varchar(36) COLLATE utf8_unicode_ci NOT NULL,
`video_id` varchar(36) COLLATE utf8_unicode_ci NOT NULL,
`course_id` varchar(36) COLLATE utf8_unicode_ci NOT NULL,
`time` int(11) NOT NULL,
`title` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`deleted_at` timestamp NULL DEFAULT NULL,
`notes` text COLLATE utf8_unicode_ci,
PRIMARY KEY (`id`),
UNIQUE KEY `bookmarks_course_id_user_id_video_id_time_unique` (`course_id`,`user_id`,`video_id`,`time`),
KEY `bookmarks_user_id_foreign` (`user_id`),
KEY `bookmarks_video_id_foreign` (`video_id`),
CONSTRAINT `bookmarks_course_id_foreign` FOREIGN KEY (`course_id`) REFERENCES `courses` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `bookmarks_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `bookmarks_video_id_foreign` FOREIGN KEY (`video_id`) REFERENCES `videos` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
/*!40101 SET character_set_client = #saved_cs_client */;
...
--
-- 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` varchar(36) COLLATE utf8_unicode_ci NOT NULL,
`username` varchar(63) COLLATE utf8_unicode_ci DEFAULT NULL,
`password` varchar(60) COLLATE utf8_unicode_ci DEFAULT NULL,
`email` varchar(63) COLLATE utf8_unicode_ci NOT NULL,
`role_id` varchar(36) COLLATE utf8_unicode_ci DEFAULT NULL,
`last_login` datetime DEFAULT NULL,
`customer_id` int(11) DEFAULT NULL,
`hours_watched` int(11) NOT NULL DEFAULT '0',
`points` int(11) NOT NULL DEFAULT '0',
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NULL DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
`remember_token` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
/*!40101 SET character_set_client = #saved_cs_client */;
....
--
-- Table structure for table `videos`
--
DROP TABLE IF EXISTS `videos`;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `videos` (
`id` varchar(36) COLLATE utf8_unicode_ci NOT NULL,
`title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`is_public` smallint(6) NOT NULL DEFAULT '0',
`is_visible` smallint(6) NOT NULL DEFAULT '0',
`points` int(11) NOT NULL DEFAULT '0',
`duration` varchar(10) COLLATE utf8_unicode_ci NOT NULL,
`provider_id` varchar(36) COLLATE utf8_unicode_ci NOT NULL,
`presenter_id` varchar(36) COLLATE utf8_unicode_ci NOT NULL,
`level_id` varchar(36) COLLATE utf8_unicode_ci NOT NULL,
`hd_url` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`high_url` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`mobile_url` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`transcript_file` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`deleted_at` timestamp NULL DEFAULT NULL,
`order` int(11) NOT NULL DEFAULT '0',
`prefix_title` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
`jwplayer_mediaid` varchar(16) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `videos_level_id_foreign` (`level_id`),
KEY `videos_provider_id_foreign` (`provider_id`),
KEY `videos_presenter_id_foreign` (`presenter_id`),
CONSTRAINT `videos_level_id_foreign` FOREIGN KEY (`level_id`) REFERENCES `levels` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `videos_presenter_id_foreign` FOREIGN KEY (`presenter_id`) REFERENCES `presenters` (`id`) ON UPDATE CASCADE,
CONSTRAINT `videos_provider_id_foreign` FOREIGN KEY (`provider_id`) REFERENCES `providers` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
/*!40101 SET character_set_client = #saved_cs_client */;
...
/*!40014 SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS */;
You must define the table you want to reference (users) before you can declare a foreign key to it.
In your case, you are creating the table bookmarks before the other table users exists.
The error you got has nothing to do with the foreign_key_checks being disabled vs. enabled.
Actually, I have to correct my answer. This dump file should work fine, and does work fine when I test it in my local MySQL 5.6 instance.
The way it gets around the foreign key ordering problem is this:
SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
This disables enforcement of the foreign key rules until the enforcement is restored at the end:
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
All I can suggest is that something in the spaces where you have ... is restoring foreign key enforcement too soon. You need to examine your complete file and figure that out.

I am missing a closing bracket was expected. (near ")" at position 136)

I received this error while uploading SQL to server phpMyAdmin. I did look at the answers from similar posts, but I don't think it's the same thing. It should be after -- Table structure for table wzflh_admintools_log. These are lines 127 through 146:
CREATE TABLE `wzflh_admintools_ipblock` (
`id` bigint(20) UNSIGNED NOT NULL,
`ip` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`description` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- --------------------------------------------------------
--
-- Table structure for table `wzflh_admintools_log`
--
CREATE TABLE `wzflh_admintools_log` (
`id` bigint(20) UNSIGNED NOT NULL,
`logdate` datetime NOT NULL,
`ip` varchar(40) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`url` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`reason` enum('other','adminpw','ipwl','ipbl','sqlishield','antispam','wafblacklist','tpone','tmpl','template','muashield','csrfshield','badbehaviour','geoblocking','rfishield','dfishield','uploadshield','xssshield','httpbl','loginfailure','securitycode','external','awayschedule','admindir','sessionshield','nonewadmins','nonewfrontendadmins','configmonitor','phpshield') COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`extradata` longtext COLLATE utf8mb4_unicode_ci
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
I have created a database named mydb and imported your tables via the console. This is what I have:
-- phpMyAdmin SQL Dump
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
CREATE TABLE `wzflh_admintools_ipblock` (
`id` bigint(20) UNSIGNED NOT NULL,
`ip` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`description` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `wzflh_admintools_log` (
`id` bigint(20) UNSIGNED NOT NULL,
`logdate` datetime NOT NULL,
`ip` varchar(40) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`url` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`reason` enum('other', 'adminpw', 'ipwl', 'ipbl', 'sqlishield',
'antispam', 'wafblacklist', 'tpone', 'tmpl',
'template', 'muashield', 'csrfshield',
'badbehaviour', 'geoblocking', 'rfishield',
'dfishield', 'uploadshield', 'xssshield', 'httpbl',
'loginfailure', 'securitycode', 'external',
'awayschedule', 'admindir', 'sessionshield',
'nonewadmins', 'nonewfrontendadmins', 'configmonitor',
'phpshield') COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`extradata` longtext COLLATE utf8mb4_unicode_ci
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
Copy and paste this to your SQL file:
-- phpMyAdmin SQL Dump
-- version 4.5.1
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Oct 19, 2017 at 07:46 PM
-- Server version: 10.1.16-MariaDB
-- PHP Version: 5.6.24
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: `centeroakland`
--
-- --------------------------------------------------------
--
-- Table structure for table `wzflh_admintools_ipblock`
--
CREATE TABLE `wzflh_admintools_ipblock` (
`id` bigint(20) UNSIGNED NOT NULL,
`ip` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`description` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- --------------------------------------------------------
--
-- Table structure for table `wzflh_admintools_log`
--
CREATE TABLE `wzflh_admintools_log` (
`id` bigint(20) UNSIGNED NOT NULL,
`logdate` datetime NOT NULL,
`ip` varchar(40) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`url` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`reason` enum('other','adminpw','ipwl','ipbl','sqlishield','antispam','wafblacklist','tpone','tmpl','template','muashield','csrfshield','badbehaviour','geoblocking','rfishield','dfishield','uploadshield','xssshield','httpbl','loginfailure','securitycode','external','awayschedule','admindir','sessionshield','nonewadmins','nonewfrontendadmins','configmonitor','phpshield') COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`extradata` longtext COLLATE utf8mb4_unicode_ci
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!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 */;
Save it then import it.

Getting a Cannot add Foreign Key Constraint 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.

Cannot create a table in mysql error(150)

I want to create 3 tables called curenttasks, originaltasks and previoustasks. I make one table and copied to other two. But one table cannot create while other 2 are created successfully.
The three table are:
DROP TABLE IF EXISTS `CurrentTasks`;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `CurrentTasks` (
`taskID` mediumint(9) NOT NULL AUTO_INCREMENT,
`taskName` varchar(255) NOT NULL,
`isActive` boolean DEFAULT TRUE,
`startDate` datetime NOT NULL,
`endDate` datetime NOT NULL,
`completeDate` datetime DEFAULT NULL,
`complexityID` smallint(6) NOT NULL,
`managerID` mediumint(9) NOT NULL,
`projectID` mediumint(9) NOT NULL,
`requirementName` varchar(255) NOT NULL,
`xPos` smallint(6) DEFAULT NULL,
`yPos` smallint(6) DEFAULT NULL,
`description` text NOT NULL,
`stageName` enum('Definition','Design','Development','Testing','Evaluation') NOT NULL DEFAULT 'Definition',
PRIMARY KEY (`taskID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = #saved_cs_client */;
--
-- Dumping data for table `CurrentTasks`
--
LOCK TABLES `PreviousTasks` WRITE;
/*!40000 ALTER TABLE `PreviousTasks` DISABLE KEYS */;
/*!40000 ALTER TABLE `PreviousTasks` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `PreviousTasks`
--
DROP TABLE IF EXISTS `PreviousTasks`;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `PreviousTasks` (
`taskID` mediumint(9) NOT NULL AUTO_INCREMENT,
`taskName` varchar(255) NOT NULL,
`isActive` boolean DEFAULT TRUE,
`startDate` datetime NOT NULL,
`endDate` datetime NOT NULL,
`completeDate` datetime DEFAULT NULL,
`complexityID` smallint(6) NOT NULL,
`managerID` mediumint(9) NOT NULL,
`projectID` mediumint(9) NOT NULL,
`requirementName` varchar(255) NOT NULL,
`xPos` smallint(6) DEFAULT NULL,
`yPos` smallint(6) DEFAULT NULL,
`description` text NOT NULL,
`stageName` enum('Definition','Design','Development','Testing','Evaluation') NOT NULL DEFAULT 'Definition',
PRIMARY KEY (`taskID`),
UNIQUE KEY `uniquePreviousTasks` (`requirementName`,`projectID`,`taskName`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = #saved_cs_client */;
--
-- Dumping data for table `PreviousTasks`
--
LOCK TABLES `PreviousTasks` WRITE;
/*!40000 ALTER TABLE `PreviousTasks` DISABLE KEYS */;
/*!40000 ALTER TABLE `PreviousTasks` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `OriginalTasks`
--
DROP TABLE IF EXISTS `OriginalTasks`;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `OriginalTasks` (
`taskID` mediumint(9) NOT NULL AUTO_INCREMENT,
`taskName` varchar(255) NOT NULL,
`isActive` boolean DEFAULT TRUE,
`startDate` datetime NOT NULL,
`endDate` datetime NOT NULL,
`completeDate` datetime DEFAULT NULL,
`complexityID` smallint(6) NOT NULL,
`managerID` mediumint(9) NOT NULL,
`projectID` mediumint(9) NOT NULL,
`requirementName` varchar(255) NOT NULL,
`xPos` smallint(6) DEFAULT NULL,
`yPos` smallint(6) DEFAULT NULL,
`description` text NOT NULL,
`stageName` enum('Definition','Design','Development','Testing','Evaluation') NOT NULL DEFAULT 'Definition',
PRIMARY KEY (`taskID`),
UNIQUE KEY `uniqueOriginalTasks` (`requirementName`,`projectID`,`taskName`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = #saved_cs_client */;
--
-- Dumping data for table `OriginalTasks`
--
LOCK TABLES `OriginalTasks` WRITE;
/*!40000 ALTER TABLE `OriginalTasks` DISABLE KEYS */;
/*!40000 ALTER TABLE `OriginalTasks` ENABLE KEYS */;
UNLOCK TABLES;
The currenttasks cannot cteate, the error is:
ERROR 1005 (HY000): Can't create table 'rem.currenttasks' (errno: 150)
I just very confused because I delete currenttasks and copied previoustasks to it, replace every previous with current, but it still cannot work.
You forgot to delete lines between 28-31. PreviousTasks table is not created yet, but those lines tries to alter table. Also same thing exist at lines 64-67.

How do I speed up this 1.7 Second Mysql Query?

I'm writing a webapp in PHP where a list of users can be voted on. The query I'm using to pull a user from the table is quite slow. I suspect there is a much more efficient way to check if the target user has already been voted on by the active user.
SELECT *
FROM users
WHERE id NOT IN (
SELECT ratedid
FROM votes
WHERE who LIKE 12707264
)
AND picture1 NOT LIKE ''
AND cp1 < '10'
AND gender NOT LIKE 'male'
ORDER BY RAND( )
LIMIT 1
Table data as follows:
>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: `notchus_userdb`
--
-- --------------------------------------------------------
--
-- Table structure for table `users`
--
CREATE TABLE IF NOT EXISTS `users` (
`id` bigint(20) NOT NULL,
`username` varchar(30) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`first_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`last_name` varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`bio` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`picture1` varchar(200) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`cp1` int(100) DEFAULT NULL,
`picture2` varchar(200) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`picture3` varchar(200) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`friends` blob,
`relationship_status` varchar(20) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`relationship_interest` varchar(10) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`verified` int(1) NOT NULL,
`gender` varchar(10) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`birthday` varchar(10) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`hometown` varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`citylocation` varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`oauth_provider` varchar(10) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`oauth_uid` int(11) NOT NULL,
`ratchet` int(1) DEFAULT NULL,
`boss` int(1) DEFAULT NULL,
`isadmin` int(1) DEFAULT NULL,
`views` int(10) NOT NULL,
`reviews` int(10) NOT NULL,
`email` varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`isuser` int(10) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `username` (`username`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Server version: 5.1.70-cll
-- PHP Version: 5.3.17
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: `notchus_userdb`
--
-- --------------------------------------------------------
--
-- Table structure for table `votes`
--
CREATE TABLE IF NOT EXISTS `votes` (
`uqid` int(11) NOT NULL AUTO_INCREMENT,
`value` tinyint(4) NOT NULL,
`picture` int(11) DEFAULT NULL,
`ratedid` bigint(20) DEFAULT NULL,
`comment` int(11) DEFAULT NULL,
`quote` int(11) DEFAULT NULL,
`who` bigint(20) NOT NULL,
`votedate` int(11) NOT NULL,
`control` varchar(100) NOT NULL,
PRIMARY KEY (`uqid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1084 ;
First of all, LIKE is only for doing wildcard matching. I'm surprised that you're not getting an error doing a LIKE on an integer. Then again, nothing MySQL does surprises me.
SELECT *
FROM users
WHERE id NOT IN (
SELECT ratedid
FROM votes
WHERE who = 12707264
)
AND picture1 <> ''
AND cp1 < '10'
AND gender <> 'male'
ORDER BY RAND( )
LIMIT 1
Also note that if it's OK for picture1 to be an empty string, then you shouldn't have a NOT NULL on the column, and you should store a NULL in the column where is no picture.
Your like statements don't appear to be using wildcards, I see no reason to use them here. Try changing them to equal signs. Indexing your tables could help you as well.
You don't appear to have indices on the foreign key relationships.
try:
create index who on votes (who);
create index q1 on users (picture1, cp1, gender);
Also, you appear to be confused about "like" and "=" - when comparing an integer column (e.g. CP1), use cp1 < 1 or who = 12707264.