#1071 - Specified key was too long MySQL - mysql

I'm really struggling with uploading a .sql file to phpmyadmin.
The error I get is this...
SQL query:
CREATE TABLE `coupons` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`code` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`description` varchar(300) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`percentage` decimal(5,2) NOT NULL DEFAULT '0.00',
`active_from` datetime DEFAULT '2018-03-23 12:15:55',
`active_to` datetime DEFAULT '2018-03-30 12:15:55',
`deleted_at` timestamp NULL DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `coupons_code_unique` (`code`)
) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
MySQL said:
#1071 - Specified key was too long; max key length is 767 bytes
Here is the original section in which I think it's referring to.
-- Table structure for table `coupons`
--
DROP TABLE IF EXISTS `coupons`;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `coupons` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`code` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`description` varchar(300) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`percentage` decimal(5,2) NOT NULL DEFAULT '0.00',
`active_from` datetime DEFAULT '2018-03-23 12:15:55',
`active_to` datetime DEFAULT '2018-03-30 12:15:55',
`deleted_at` timestamp NULL DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `coupons_code_unique` (`code`)
) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = #saved_cs_client */;
--
-- Dumping data for table `coupons`

Utfmb4 uses 4 byte per character. 4 × 255 is above max key length.
Change your code field from varchar (255) to a smaller length. Preferably use a realistic length and char data type.
Keep in mind. Longer keys require expensive computation. If in case you have larger text, create another field where you can save a computed hash of the larger string.

Related

MySQL - Copy row to another table with foreign key

i want to copy 1 row from this table:
tableold
-----------
oid (primary key, auto_incremment)
name
age
detail
to this table:
tablenew
------------
nid (primary key, auto_incremment)
fid (foreign key to another table)
name
age
detail
With this SQL-Command:
INSERT INTO tablenew (tablenew.name, tablenew.age, tablenew.detail)
SELECT tableold.name, tableold.age, tableold.detail
FROM tableold
WHERE tableold.oid = 123;
But i get the error:
ERROR 1364 (HY000): Field 'fid' doesn't have a default value
Is there any possibility to manually add the foreign key 'fid' to the SQL-Command?
*Edit, i have added the CREATE table statements:
CREATE TABLE `othertable` (
`fid` int NOT NULL AUTO_INCREMENT,
`value1` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`value2` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`value3` longtext CHARACTER SET utf8 COLLATE utf8_general_ci,
`value4` longtext CHARACTER SET utf8 COLLATE utf8_general_ci,
PRIMARY KEY (`fid`),
) ENGINE=InnoDB AUTO_INCREMENT=66 DEFAULT CHARSET=utf8;
CREATE TABLE `tablenew` (
`nid` int NOT NULL AUTO_INCREMENT,
`fid` int NOT NULL,
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`age` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`detail` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
PRIMARY KEY (`sid`),
KEY `fid_fkey` (`fid`),
CONSTRAINT `fid_fkey` FOREIGN KEY (`fid`) REFERENCES `othertable` (`fid`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
CREATE TABLE `tableold` (
`oid` int NOT NULL AUTO_INCREMENT,
`name` varchar(60) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`age` varchar(60) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`detail` varchar(60) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
PRIMARY KEY (`oid`)
) ENGINE=InnoDB AUTO_INCREMENT=157 DEFAULT CHARSET=utf8;
I have solved it:
INSERT INTO tablenew (tablenew.fid, tablenew.name, tablenew.age, tablenew.detail)
SELECT 'value', tableold.name, tableold.age, tableold.detail
FROM tableold
WHERE tableold.oid = value;

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?

MySQL updates column to always NULL

I have two tables which basically one is a master table and the other one is a table for developers. From time to time I would copy review the record from the developer table and move it to the master table:
--SHOW CREATE TABLE DOCUMENT --
CREATE TABLE `document` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`url` varchar(1000) CHARACTER SET utf8 NOT NULL,
`title` varchar(1000) CHARACTER SET utf8 NOT NULL,
`content` longtext CHARACTER SET utf8,
`source_type` varchar(100) CHARACTER SET utf8 DEFAULT NULL,
`date_crawled` datetime DEFAULT NULL,
`mime_type` varchar(100) CHARACTER SET utf8 DEFAULT NULL,
`type` varchar(100) CHARACTER SET utf8 DEFAULT NULL,
`date_posted` datetime DEFAULT NULL,
`tier` int(11) DEFAULT NULL,
`html_content` longtext CHARACTER SET utf8,
`dev` varchar(255) CHARACTER SET utf8 NOT NULL,
`dev_document_id` int(11) NOT NULL,
`promote` int(11) DEFAULT NULL,
`domain` varchar(128) CHARACTER SET utf8 DEFAULT NULL,
PRIMARY KEY (`id`,`url`(500),`dev`,`dev_document_id`),
UNIQUE KEY `url` (`url`)
) ENGINE=InnoDB AUTO_INCREMENT=628871 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ROW_FORMAT=COMPRESSED
--SHOW CREATE TABLE DOCUMENT_DEVELOPER--
CREATE TABLE `document_developer` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`url` varchar(1000) CHARACTER SET utf8 NOT NULL,
`title` varchar(1000) CHARACTER SET utf8 DEFAULT NULL,
`content` longtext CHARACTER SET utf8,
`jurisdiction` varchar(45) CHARACTER SET utf8 DEFAULT NULL,
`source_type` varchar(100) CHARACTER SET utf8 DEFAULT NULL,
`date_crawled` datetime DEFAULT NULL,
`mime_type` varchar(100) CHARACTER SET utf8 DEFAULT NULL,
`type` varchar(100) CHARACTER SET utf8 DEFAULT NULL,
`html_content` longtext CHARACTER SET utf8,
`date_posted` datetime DEFAULT NULL,
`tier` int(11) DEFAULT NULL,
`promote` int(11) NOT NULL,
`modified` tinyint(1) NOT NULL DEFAULT '0',
`date_modified` datetime DEFAULT NULL,
UNIQUE KEY `url_promote_UNIQUE` (`url`,`promote`),
KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=637067 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ROW_FORMAT=COMPRESSED
/*!50100 PARTITION BY LIST (promote)
(PARTITION part0 VALUES IN (-5) ENGINE = InnoDB,
PARTITION part1 VALUES IN (-4) ENGINE = InnoDB,
PARTITION part2 VALUES IN (-3) ENGINE = InnoDB,
PARTITION part3 VALUES IN (-2) ENGINE = InnoDB,
PARTITION part4 VALUES IN (-1) ENGINE = InnoDB,
PARTITION part5 VALUES IN (0) ENGINE = InnoDB,
PARTITION part6 VALUES IN (1) ENGINE = InnoDB,
PARTITION part7 VALUES IN (2) ENGINE = InnoDB) */
The column of dev_document_id will be the id of record in document_developer once the copy is done.
However, when i try to run update as below:
UPDATE document as d INNER JOIN document_developer AS dd ON d.dev_document_id = dd.id
SET
d.content=dd.content
WHERE dd.modified='1' AND dd.promote='2';
it will always give me erroneous content value such as NULL NULL NULL ... Any help will be greatly appreciated.
Your query should be
UPDATE document as d INNER JOIN document_developer AS da ON
d.dev_document_id = da.id
SET
d.content=da.content
WHERE da.modified='1' AND da.promote='2';

XAMPP's PHPMyAdmin Ignores AUTO_INCREMENT Attribute When Exporting the Table

The problem that I am going to tell is for all the tables in the DB, When I dump my DB it ignores AUTO_INCREMENT attribute. My actual table is as the following:
CREATE TABLE IF NOT EXISTS `departments` (
`departmentid` int(11) NOT NULL AUTO_INCREMENT,
`chairid` int(11) NOT NULL,
`department_name` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
`image` varchar(128) NOT NULL DEFAULT 'default_department.png',
`url` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
`active` tinyint(4) NOT NULL DEFAULT '1'
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;
But if I dump the table using PHPMyAdmin it does not add auto_increment which I mentioned before.
The output of the exported .sql file's content is here:
CREATE TABLE IF NOT EXISTS `departments` (
`departmentid` int(11) NOT NULL, -- AUTO_INCREMENT is missing
`chairid` int(11) NOT NULL,
`department_name` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
`image` varchar(128) NOT NULL DEFAULT 'default_department.png',
`url` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
`active` tinyint(4) NOT NULL DEFAULT '1'
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;
I especially checked that if auto_increment is disabled in CREATE TABLE options but no, it is not.
This was fixed in phpMyAdmin version 4.5.0.1 (Sep 2015):
https://github.com/phpmyadmin/phpmyadmin/issues/11492
I supposed it ignores auto increment but I explore file and at the bottom of sql I found that it altered to auto increment somewhere else

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;