MySQL sorting Ä and Ö with utf8_general_ci - mysql

Is there any way to sort characters Ä and Ö correctly without changing collation of connection? I'm using PHP and PDO.
If it's not sensible... I tried to convert every table to utf8_swedish_ci using
ALTER TABLE tablename COLLATE utf8_swedish_ci; but it seems not working. There are still Ä before A. What's wrong?
EDIT: I manually set every field to utf8_swedish_ci and now it's working, but is there any easier way to that?

Take this table as example:
CREATE TABLE `countries` (
`countryid` INT(4) UNSIGNED NOT NULL AUTO_INCREMENT,
`country` VARCHAR(44) NOT NULL,
PRIMARY KEY (`countryid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
For the country column to sort correctly use this query:
ALTER TABLE `countries`
CHANGE COLUMN `country` `country` VARCHAR(44) NOT NULL COLLATE 'utf8_swedish_ci' AFTER `countryid`;
As you mentioned you need the collate on the column you want to sort.

Related

mySQL query to postgreSQL

I am new to PostgreSQL. while following one article which is on MySQL I am facing one challenge that the mySQL query is throwing error in PostgreSQL. Can anyone tel me the solution for this.
MySQL query is
CREATE TABLE "sessions" (
"id" varchar(128)
CHARACTER SET utf8mb4
COLLATE utf8mb4_bin NOT NULL,
"expires" int(11) unsigned NOT NULL,
"data" text CHARACTER SET utf8mb4 COLLATE utf8mb4_bin,
PRIMARY KEY ("id")
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
What will be the similar query for PostgreSQL
I'm not sure how important the binary collations are for what you are doing. But this will probably work for you:
CREATE TABLE sessions (
id varchar(128) primary key,
expires int NOT NULL check(expires >= 0),
data text
) ;

arabic word changed to ???? after inserting into Mysql DB

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

View Japanese Characters in MySQL Workbench

I already tried This solution which says
ALTER TABLE title
CHARACTER SET utf8
COLLATE utf8_unicode_ci;
Ok here are some screen shots which might help you.
Update
here's what happens when i insert Japanese characters.
Update 2
Show create table gives this
CREATE TABLE `productInfo` (
`pID` int(11) NOT NULL AUTO_INCREMENT,
`pOperation` varchar(40) CHARACTER SET latin1 DEFAULT NULL,
`year` year(4) DEFAULT NULL,
`season` varchar(10) CHARACTER SET latin1 DEFAULT NULL,
`pName` varchar(40) CHARACTER SET latin1 DEFAULT NULL,
`category` varchar(40) CHARACTER SET latin1 DEFAULT NULL,
`margin1` text CHARACTER SET latin1,
`margin2` text CHARACTER SET latin1,
PRIMARY KEY (`pID`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
just see that
DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
But now see that the query
SELECT character_set_name, collation_name
FROM information_schema.columns
WHERE table_schema = 'trac_data'
AND table_name = 'productInfo'
AND column_name = 'pOperation';
gives
character_set_name collation_name
'latin1' 'latin1_swedish_ci'
Thats weird !
Update 3
SELECT hex(pOperation),pOperation FROM trac_data.productInfo;
gave 3F3F3F3F3F which is hex code for actual '?' and not any japanese character so that means no japanese characters are being stored
You have a mix of charsets in your table structure. The table itself uses utf8, but the column in question uses latin 1. You have it defined that way. As long as you have an own charset for your column you can change the table's or the schema's column a thousand times. It won't have any effect on your column. So, instead change the column's charset to either default (to use that of the table) or make it using utf8 explicitely.
When you alter the column's charset existing data will be converted (if possible). Your wrong input however stays wrong, so you have to fill the data again.
Ok i found the cause
CREATE TABLE `productInfo` (
`pID` int(11) NOT NULL AUTO_INCREMENT,
`pOperation` varchar(40) CHARACTER SET latin1 DEFAULT NULL,
`year` year(4) DEFAULT NULL,
`season` varchar(10) CHARACTER SET latin1 DEFAULT NULL,
`pName` varchar(40) CHARACTER SET latin1 DEFAULT NULL,
`category` varchar(40) CHARACTER SET latin1 DEFAULT NULL,
`margin1` text CHARACTER SET latin1,
`margin2` text CHARACTER SET latin1,
PRIMARY KEY (`pID`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
I noticed how in front of each column SET latin1 was present.
So I Just changed to sjis and problem solved.
You have to set the database collation to UTF-8, not only the table collation :
Here is the SQL script result :

Is there any point of using COLLATE per column when creating tables?

I just did an export of a MySQL database in order to duplicate it on another server. Looking at the sql script I see the following:
CREATE TABLE `X` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`email` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL
PRIMARY KEY (`id`),
KEY `name_index` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=8366
DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Do I need to use this per column when collate is set for the table?
I think the reason for why each column has this, was because I wanted to make sure I could store locale values such as æ, ø å.
Since your column-level collations are all the same, setting the collation at the table level will have the same effect.
Note that collation has no effect on what you can store; rather, it affects how the results are sorted. The only reason you would want a different collation per column would be if your table contains columns with data for different locales requiring a different sort order.

Is there any way to make a UNIQUE index case insensitive in Mysql 5.1.x ?

If so - What must change in this table ?
CREATE TABLE contestants
(
idContestants int(10) unsigned NOT NULL AUTO_INCREMENT,
idEvent int(10) unsigned NOT NULL,
ContestantName varchar(50) DEFAULT NULL,
PRIMARY KEY (idContestants),
UNIQUE KEY Index_UniqueName (idEvent,ContestantName),
)
ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
If you mean case sensitive then:
ALTER TABLE `contestants` CHANGE `ContestantName` `ContestantName` VARCHAR( 50 )
CHARACTER SET latin1 COLLATE latin1_bin NULL DEFAULT NULL
If you mean case insensitive then:
ALTER TABLE `contestants` CHANGE `ContestantName` `ContestantName` VARCHAR( 50 )
CHARACTER SET latin1 COLLATE latin1_general_ci NULL DEFAULT NULL
For table level do (for case insensitive):
ALTER TABLE `contestants` DEFAULT CHARACTER SET latin1 COLLATE latin1_general_ci
Note that table level only affects new columns.
For database level do (for case insensitive):
ALTER DATABASE `database_name` CHARACTER SET latin1 COLLATE latin1_general_ci
Note that database level only affect new tables.
Yes, use a case-insensitive collation on the columns involved.
MySQL Manual :: Column Character Set and Collation
This worked for me in Mysql 5.5
ALTER TABLE `contestants` MODIFY
`ContestantName` VARCHAR(50)
CHARACTER SET latin1
COLLATE latin1_bin;