I have created a table in a MySQL database that stores a text field:
CREATE TABLE `Usuarios` (
`Id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`Nombre` varchar(50) DEFAULT NULL,
PRIMARY KEY (`Id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
I have entered 2 names: José and José
And from Java I do a search query that tells me that I have two equal records:
ResultSet resultado = consulta.executeQuery("SELECT COUNT(*) FROM `Usuarios` WHERE UPPER(`Nombre`) = ‘JOSÉ’";
resultado.first();
System.out.println("Usuarios llamados JOSÉ: " + resultado.getInt(1));
ANSWER: Users named JOSÉ: 2
How can I prevent it from treating stressed and unstressed vowels the same?
Do it with
utf8_bin
, for this the reserved word is usedCOLLATE
. With thischarset
we are indicating to MySQL that the comparison in the search be done at the binary level, with which now it will be able to distinguish between words with accents and without accents, uppercase and lowercase, it would be something like this:Obtained from here:
here
The problem was that the
COLLATION
of the table was set by default toutf8_general_ci
._ci
stands for case insensitive. Changingutf8_bin
it to fixes it. Information taken from the MySQL manualTo change only one table we have to do this:
And to change the entire database: