Having this table using mysql 5.7:
CREATE TABLE `emails` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`subject` varchar(191) COLLATE utf8mb4_bin DEFAULT NULL,
PRIMARY KEY (`id`),
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
When I try to insert some emojis:
INSERT INTO `emails` (`from_address`, `subject`) VALUES (1, 'A😀B C👨🏽🎨D')
I receive:
Incorrect string value: '\xF0\x9F\x98\x80B ...' for column 'subject' at row 1
Why? if i'm using utfmb?
Is your connection also utf8mb4? Detailed explanation of this can be found at: https://mathiasbynens.be/notes/mysql-utf8mb4
Related
I have a MySQL table looking as follows:
CREATE TABLE `User` (
`id` char(25) CHARACTER SET utf8 NOT NULL,
`role` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'CUSTOMER',
PRIMARY KEY (`id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
In all rows of the table, role either has the value 'CUSTOMER' or 'ADMIN'. I now want to change the type of that column to an enum and keep the same data in all rows.
What's the SQL statement to achieve that?
Just found the answer myself:
ALTER TABLE User MODIFY COLUMN role ENUM('ADMIN', 'CUSTOMER') DEFAULT 'CUSTOMER';
We have a simple query that checks if a record exists in one table that has the id of another. If so it returns the id. This works for small data sets such as the working example below in Aurora.
However, if we have a lot of ids (like 1000) the first query returns 0 rows and the second succeeds in Aurora but works on MySQL.
We have been unable to see any optimizations or other gaps in Aurora MySQL that would explain this behavior. Any advice or guidance is appreciated.
Working example:
CREATE TABLE `test_table_1` (
`id` char(36) COLLATE utf8mb4_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `test_table_2` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`test_table_1_id` char(36) COLLATE utf8mb4_unicode_ci NOT NULL,
`another_id` char(36) COLLATE utf8mb4_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
KEY `test_table_1_id_foreign` (`test_table_1_id`),
KEY `another_id_index` (`another_id`),
CONSTRAINT `test_table_1_id_foreign` FOREIGN KEY (`test_table_1_id`) REFERENCES `test_table_1` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
INSERT INTO `test_table_1` (`id`)
VALUES
('e54b3097-21ff-4d7d-8bee-5aa62e66d9b6'),
('4fada427-ef5a-455d-ad55-831de6192dc1');
INSERT INTO `test_table_2` (`test_table_1_id`, `another_id`)
VALUES
('e54b3097-21ff-4d7d-8bee-5aa62e66d9b6', 'aeef668e-1365-41e8-a4a7-026d9a021935'),
('4fada427-ef5a-455d-ad55-831de6192dc1', '61e7b307-e356-4537-bcc5-927c3c217992');
SELECT * FROM `test_table_1`
WHERE `id` = 'e54b3097-21ff-4d7d-8bee-5aa62e66d9b6'
AND EXISTS (
SELECT * FROM `test_table_2`
WHERE `test_table_2`.`another_id` = 'aeef668e-1365-41e8-a4a7-026d9a021935'
AND `test_table_2`.`test_table_1_id` = `test_table_1`.`id`);
SELECT * FROM `test_table_1`
WHERE `id` = '4fada427-ef5a-455d-ad55-831de6192dc1'
AND EXISTS (
SELECT * FROM `test_table_2`
WHERE `test_table_2`.`another_id` = '61e7b307-e356-4537-bcc5-927c3c217992'
AND `test_table_2`.`test_table_1_id` = `test_table_1`.`id`);
when I insert data in this table, my query is
INSERT INTO urdu_word (word) VALUES ('Abdelali Abou Dher (عبد العالي ابو ذر)') ON DUPLICATE KEY UPDATE word='Abdelali Abou Dher (عبد العالي ابو ذر)' word value replace to like ???
My table structure is:
CREATE TABLE `urdu_word` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`word` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `word` (`word`),
KEY `idx_aml_word_status` (`word`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
I also tried to chage table structure to utf8_unicode_ci but same problem facing
CREATE TABLE `aml_word` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`word` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `word` (`word`),
KEY `idx_aml_word_status` (`word`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
mysql version 5.6
when insert query run in mysql command line then it inserted well in urdu but when I inserted through code using mybatis ORM then create problem.
The default character set for MySQL at (mt) Media Temple is latin1,
with a default collation of latin1_swedish_
So you need to change your callation
Try with this query
ALTER DATABASE dbname CHARACTER SET utf8 COLLATE utf8_general_ci;
Followed by this tutorial
https://mediatemple.net/community/products/dv/204403914/default-mysql-character-set-and-collation#gs
I want to import an SQL file to my phpMyAdmin database, the file contains tables about a restaurant application and a table about the admin login, but I have 2 unexpected errors, I really don't see what is exactly wrong with that?. here is the error message:
2 errors were found during analysis.
An opening bracket followed by a set of values was expected. (near "CREATE" at position 109)
Unexpected token. (near "CREATE" at position 109)
SQL query:
INSERT INTO `adminlogin` (`id`, `Username`, `Password`, `right`) VALUES (1, 'admin#gmail.com', '123', 1), CREATE TABLE IF NOT EXISTS `tbl_food` ( `id` int(11) NOT NULL AUTO_INCREMENT, `food_type` varchar(50) NOT NULL, `food_id` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1
MySQL said: Documentation
#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 'CREATE TABLE IF NOT EXISTS `tbl_food` (
`id` int(11) NOT NULL AUTO_INCREMENT,
' at line 6
and here is the two tables that show errors. Any insight on that? thank you
CREATE TABLE IF NOT EXISTS `adminlogin` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`Username` varchar(100) NOT NULL,
`Password` varchar(50) NOT NULL,
`right` tinyint(1) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
--
-- Dumping data for table `adminlogin`
--
INSERT INTO `adminlogin` (`id`, `Username`, `Password`, `right`) VALUES
(1, 'admin#gmail.com', '123', 1),
-- --------------------------------------------------------
--
-- Table structure for table `tbl_food`
--
CREATE TABLE IF NOT EXISTS `tbl_food` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`food_type` varchar(50) NOT NULL,
`food_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
You have a comma at the end of INSERT statement, instead of semicolon.
Assume we have the following 2 databases:
DROP DATABASE IF EXISTS `adb`;
CREATE DATABASE `adb` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;
USE `adb`;
CREATE TABLE IF NOT EXISTS `Login` (
`ID` bigint(20) NOT NULL,
`Login` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `Login` (`ID`, `Login`) VALUES
(1, '2012-11-09 11:18:29'),
(2, '2012-12-22 21:48:48'),
(3, '2013-01-01 12:39:22');
DROP DATABASE IF EXISTS `bdat`;
CREATE DATABASE `bdat` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;
USE `bdat`;
CREATE TABLE IF NOT EXISTS `Login` (
`ID` bigint(20) NOT NULL,
`Login` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `Login` (`ID`, `Login`) VALUES
(1, '2011-11-09 15:15:15'),
(2, '2012-12-22 13:08:18'),
(3, '2010-02-11 17:00:02');
We also have 2 queries.
Query1 is:
SELECT table_schema AS "Database", round(sum(data_length+index_length)/1024/1024,4) AS "Size (MB)" FROM information_schema.tables GROUP BY table_schema;
Query2 is:
SELECT Max(Login) AS "Last Login" FROM Login
How to combine the two queries together to get the following result ?
Database Size (MB) Last Login
adb 0.0020 2012-12-22 13:08:18
bdat 0.0020 2013-01-01 12:39:22
information_schema 0.0078 NULL
mysql 0.6133 NULL
If you use a Mysql version greater than 5.0 can use FEDERATED TABLES.
For example, in BDAT create a FEDERATED TABLE to ADB, with this code:
CREATE TABLE federated_Login (
`ID` bigint(20) NOT NULL,
`Login` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`ID`)
)
ENGINE=FEDERATED
DEFAULT CHARSET=latin1
CONNECTION='mysql://root#remote_host:9306/federated/Login';
And then you can use federated_Login as a local table for BDAT, for more information:
http://dev.mysql.com/doc/refman/5.0/es/federated-use.html