MySql - doesn't support BLOB/TEXT columns - mysql

create temporary table if not exists tmp engine=memory
SELECT id, CONCAT(TRIM(lastName),TRIM(firstName),TRIM(zip)) AS identify
FROM customers
GROUP BY identify;
While running the procedure I get the following error message:
The used table type doesn't support BLOB/TEXT columns
I already saw this thread but it didn't help me.
The types on the columns are the following:
lastName -> VARCHAR(255)
firstName -> VARCHAR(255)
zip -> VARCHAR(10)
when I exclude the zip from the procedure it works as It should so I guess there is a problem with the length of the varchar?
Does anyone knew a solution without changing the varchar length of zip from 10 to 255?

Incidence is presented by the value of the constant CONVERT_IF_BIGGER_TO_BLOB:
/**
CHAR and VARCHAR fields longer than this number of characters are converted
to BLOB.
Non-character fields longer than this number of bytes are converted to BLOB.
Comparisons should be '>' or '<='.
*/
#define CONVERT_IF_BIGGER_TO_BLOB 512 /* Used for CREATE ... SELECT */
mysql-server/sql/sql_const.h::52
and
16.3 The MEMORY Storage Engine
...
Support for variable-length data types (including BLOB and TEXT) not supported by MEMORY.
Example:
mysql> DROP TEMPORARY TABLE IF EXISTS `tmp`;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TEMPORARY TABLE IF NOT EXISTS `tmp` ENGINE=MEMORY
-> SELECT SPACE(512) `tmp_col`;
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> DROP TEMPORARY TABLE IF EXISTS `tmp`;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TEMPORARY TABLE IF NOT EXISTS `tmp` ENGINE=MEMORY
-> SELECT SPACE(513) `tmp_col`;
ERROR 1163 (42000): The used table type doesn't support BLOB/TEXT columns
Try:
mysql> DROP TABLE IF EXISTS `tmp`, `customers`;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE IF NOT EXISTS `customers` (
-> `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> `lastName` VARCHAR(255) NOT NULL,
-> `firstName` VARCHAR(255) NOT NULL,
-> `zip` VARCHAR(10) NOT NULL
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TEMPORARY TABLE IF NOT EXISTS `tmp` (
-> `id` BIGINT UNSIGNED NOT NULL PRIMARY KEY,
-> `identify` VARCHAR(520) NOT NULL
-> ) ENGINE=MEMORY;
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO `tmp`
-> SELECT `id`, CONCAT(TRIM(`lastName`),
-> TRIM(`firstName`),
-> TRIM(`zip`)) `identify`
-> FROM `customers`
-> GROUP BY `id`, `identify`;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0

My experience with ENGINE=MEMORY was that the charset is also relevant.
varchar(65000) with charset latin1 worked; charset UTF8 did not
ENGINE=MEMORY DEFAULT CHARSET=latin1

Related

mysql "create table if not exists", determine if table created

Is it possible to determine if a table was created or already existed when using create table if not exists...?
There seem to be two variables returned:
(query ok, affected_rows)
The query always returns (1, 0) regardless of if the table was created or already existed.
But it seems a create schema if not exists does return affected_rows=1.
Thanks.
I get 1 warning if the table exists (mysql 8.0.25):
mysql> CREATE TABLE IF NOT EXISTS `test` (`id` INT NOT NULL, PRIMARY KEY (`id`));
Query OK, 0 rows affected (0.01 sec)
mysql> CREATE TABLE IF NOT EXISTS `test` (`id` INT NOT NULL, PRIMARY KEY (`id`));
Query OK, 0 rows affected, 1 warning (0.00 sec)
The warning is 1050 Table 'test' already exists
But you can just check if the table exists before creating it

MySQL 'NOT NULL' Error

I have question about 'NOT NULL' error. I need to make a table with variable type: 'SERIAL' and length of 7. Here is an error that SQL sends to me:
SQL query:
CREATE TABLE `table42`.`CheckOuts ` (
`CheckOutID` SERIAL( 7 ) NOT NULL
) ENGINE = MYISAM ;
The error that is returned:
#1064 - You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '(7) NOT NULL) ENGINE = MyISAM' at line 1
I think your problem is that SERIAL isn't supported in that method for MySQL. Docs suggest you should be using AUTO_INCREMENT instead:
see: http://www.sqlines.com/mysql/auto_increment
also: What is the difference between SERIAL and AUTO_INCREMENT in mysql
These suggest you should be creating your table with the following example syntax:
create table myfriends (
id int primary key auto_increment,
frnd_name varchar(50) not null
);
The SERIAL type already has NOT NULL as part of its definition, so your NOT NULL declaration is redundant
Error is not due to NOT NULL, although SERIAL already includes NOT NULL, an error does not occur when duplicating it. The cause of the error is for the length that you try to assign (7):
mysql> DROP TABLE IF EXISTS `CheckOuts`;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE IF NOT EXISTS `CheckOuts` (
-> -- `CheckOutID` SERIAL(7) NOT NULL
-> `CheckOutID` SERIAL NOT NULL
-> ) ENGINE=MYISAM;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW CREATE TABLE `CheckOuts`\G
*************************** 1. row ***************************
Table: CheckOuts
Create Table: CREATE TABLE `CheckOuts` (
`CheckOutID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
UNIQUE KEY `CheckOutID` (`CheckOutID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
I don't understand what you're trying to do, but if you need to assign a length, you'll need something like:
mysql> DROP TABLE IF EXISTS `CheckOuts`;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> CREATE TABLE IF NOT EXISTS `CheckOuts` (
-> `CheckOutID` INT(7) UNSIGNED SERIAL DEFAULT VALUE
-> ) ENGINE=MYISAM;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW CREATE TABLE `CheckOuts`\G
*************************** 1. row ***************************
Table: CheckOuts
Create Table: CREATE TABLE `CheckOuts` (
`CheckOutID` int(7) unsigned NOT NULL AUTO_INCREMENT,
UNIQUE KEY `CheckOutID` (`CheckOutID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
See documentation:
11.1.1 Numeric Type Overview
...
SERIAL is an alias for BIGINT UNSIGNED NOT NULL AUTO_INCREMENT
UNIQUE.
SERIAL DEFAULT VALUE in the definition of an integer column is an
alias for NOT NULL AUTO_INCREMENT UNIQUE.
...

Set AUTO_INCREMENT on a column with missing values

I have a table called users in my database. It has a field id of type int. As of now this is manually incremented for every user that registers and some intermediate values are missing because of deleted user accounts. I cannot change the user id of other registered users . I tried to change this column to AUTO_INCREMENT using this statement
ALTER TABLE `userinfo` CHANGE `id` `id` BIGINT(20) NOT NULL AUTO_INCREMENT;
But I got the following error
1062 - ALTER TABLE causes auto_increment resequencing, resulting in
duplicate entry '1' for key 'PRIMARY'
I only have around 200 users in my table. So I wanted to start AUTO_INCREMENT from 201. I executed the following statements
ALTER TABLE `userinfo` AUTO_INCREMENT=201;
ALTER TABLE `userinfo` CHANGE `id` `id` INT(10) NOT NULL AUTO_INCREMENT;
But still I encounter the same issue.
If you don't have duplicates (id), you should not have problems:
mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.7.17 |
+-----------+
1 row in set (0.00 sec)
mysql> DROP TABLE IF EXISTS `userinfo`;
Query OK, 0 rows affected (0.01 sec)
mysql> CREATE TABLE IF NOT EXISTS `userinfo` (
-> `id` INT(11) NOT NULL
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO `userinfo`
-> (`id`)
-> VALUES
-> (1),
-> (2),
-> (3),
-> -- (1),
-> (10),
-> (11),
-> (15),
-> (20),
-> (182),
-> (191);
Query OK, 9 rows affected (0.00 sec)
Records: 9 Duplicates: 0 Warnings: 0
mysql> ALTER TABLE `userinfo`
-> CHANGE `id` `id` BIGINT(20) NOT NULL
-> PRIMARY KEY AUTO_INCREMENT;
Query OK, 9 rows affected (0.00 sec)
Records: 9 Duplicates: 0 Warnings: 0
mysql> ALTER TABLE `userinfo` AUTO_INCREMENT = 201;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> SHOW CREATE TABLE `userinfo`\G
*************************** 1. row ***************************
Table: userinfo
Create Table: CREATE TABLE `userinfo` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=201 DEFAULT CHARSET=utf8mb4
1 row in set (0.00 sec)
In another case:
mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.7.17 |
+-----------+
1 row in set (0.00 sec)
mysql> DROP TABLE IF EXISTS `userinfo`;
Query OK, 0 rows affected (0.01 sec)
mysql> CREATE TABLE IF NOT EXISTS `userinfo` (
-> `id` INT(11) NOT NULL
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO `userinfo`
-> (`id`)
-> VALUES
-> (1),
-> (2),
-> (3),
-> (1), -- Duplicate
-> (10),
-> (11),
-> (15),
-> (20),
-> (182),
-> (191);
Query OK, 10 rows affected (0.00 sec)
Records: 10 Duplicates: 0 Warnings: 0
mysql> ALTER TABLE `userinfo`
-> CHANGE `id` `id` BIGINT(20) NOT NULL
-> PRIMARY KEY AUTO_INCREMENT;
ERROR 1062 (23000): ALTER TABLE causes auto_increment resequencing, resulting in duplicate entry '1' for key 'PRIMARY'
Try resetting the column to primary key, like this:
ALTER TABLE `userinfo` CHANGE `id` `id` INT(10) PRIMARY KEY AUTO_INCREMENT;
You can just drop auto_increment then set auto_increment, do not need recreate primary key
ALTER TABLE `userinfo` CHANGE `id` `id` int NOT NULL;
ALTER TABLE `userinfo` AUTO_INCREMENT=9;
ALTER TABLE `userinfo` CHANGE `id` `id` int NOT NULL AUTO_INCREMENT;
I was having this same problem/error (MySql 5.7.28) with bulk inserts of data that was missing primary key/auto_increment values - even though there were no duplicates. I wanted to preserve the primary key and needed to preserve the original field values also as resequencing would have caused data-integrity problems in the system.
My solution was to remove the auto_increment and then primary key as suggested above:
ALTER TABLE `userinfo` CHANGE `id` `id` int NOT NULL;
ALTER TABLE `userinfo` DROP PRIMARY KEY;
Then run my bulk insert statement without resequencing problems.
Then restore the primary key and original values using the following steps:
renaming the original PK/auto-increment field which now has the desired values in it
creating a new PK/auto-increment field with the original field name
setting the auto-increment value to one more than the max value of the renamed field
copying the original values into the new PK/auto-increment field
dropping the renamed field (now you have the original schema with PK/auto-increment field with original gaps in the sequence as desired.
Here is the sql for the first two steps:
ALTER TABLE `userinfo` CHANGE `id` `orig_id` INT;
ALTER TAble `userinfo` add id int auto_increment not null primary key;
Then use the following query result:
select max(orig_id) + 1 from `userinfo`;
to set the new auto-increment, before updating the new PK/auto-increment values:
ALTER TABLE `userinfo` AUTO_INCREMENT=201;
UPDATE `userinfo` set id = orig_id;
ALTER TABLE `userinfo` drop column orig_id;
and now I've got the original table values copied exactly as wanted. I hope this helps others save time.

change character encoding of mysql table

When I change the character set of a table from utf8 to latin1, the columns still keep the old setting.
For e.g. in the example below, name column is still utf8 even if the table is converted to latin1
mysql> create table mytest (id int, name varchar(255)) engine=myisam default charset=utf8;
Query OK, 0 rows affected (0.02 sec)
mysql> alter table mytest charset=latin1;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table mytest\G
*************************** 1. row ***************************
Table: mytest
Create Table: CREATE TABLE `mytest` (
`id` int(11) DEFAULT NULL,
`name` varchar(255) CHARACTER SET utf8 DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
This must be to preseve the data integrity. But is there any way to change everything to latin1. I am not much worried about data. I am ok if there are a few junk characters due to conversion.

How to copy MySQL table structure to table in memory?

How to create a table in memory which is identical to "regular" table? I want a copy without indexes and constraints. Pure SQL (no external tools).
This works (however indexes are created):
create table t_copy (like t_original)
This doesn't:
create table t_copy (like t_original) engine=memory
CREATE TABLE t_copy ENGINE=MEMORY SELECT * FROM t_original;
I actually tried it, it works !!!
mysql> show create table queue\G
*************************** 1. row ***************************
Table: queue
Create Table: CREATE TABLE `queue` (
`ndx` int(11) NOT NULL AUTO_INCREMENT,
`folderid` int(11) NOT NULL,
PRIMARY KEY (`ndx`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1
1 row in set (0.02 sec)
mysql> create table queue_memory engine=MEMORY as select * from queue;
Query OK, 0 rows affected (0.05 sec)<BR>
Records: 0 Duplicates: 0 Warnings: 0<BR>
lwdba#localhost (DB test) :: show create table queue_memory\G
*************************** 1. row ***************************
Table: queue_memory
Create Table: CREATE TABLE `queue_memory` (
`ndx` int(11) NOT NULL DEFAULT '0',
`folderid` int(11) NOT NULL
) ENGINE=MEMORY DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
Give it a try !!!
This should get you an empty table without any indexes.
create table t_copy
select *
from t_original
where 0
To create an in-memory table with the same structure AND indexes as the first table, try:
create table t_copy like t_original;
alter table t_copy engine=mem;