##00 Brief reviewI have written an article before about the case sensitivity of mysql tables. In fact, the content stored in fields in mysql is not case-sensitive. This article will briefly summarize it. Want to review: MySQL’s capitalization rules for database names, table names, column names, and aliases under Linux are as follows: 1. Database name and table name It is strictly case-sensitive; 2. Table aliases are strictly case-sensitive; 3. Column names and column aliases are case-insensitive in all cases. ;
4. Field content is case-insensitive by default.
01 An exampleSimple example:CREATE TABLE `tb_user` ( `id` BIGINT (20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '用户id', `username` VARCHAR (50) NOT NULL COMMENT '用户名', PRIMARY KEY (`id`) ) ENGINE = INNODB DEFAULT CHARSET = utf8 COMMENT = '用户表'; INSERT INTO `u2s`.`tb_user` (`id`, `username`) VALUES ('1', 'user'); INSERT INTO `u2s`.`tb_user` (`id`, `username`) VALUES ('2', 'User'); INSERT INTO `u2s`.`tb_user` (`id`, `username`) VALUES ('3', 'USER');
user, and the result is All three records have been queried.
mysql> SELECT username from tb_user where username = 'user'; +----------+ | username | +----------+ | user | | User | | USER | +----------+ 3 rows in set
BINARY keyword to make the search case-sensitive.
Add the BINARY keyword in the query sql
mysql> select * from tb_user where BINARY username ='user'; +----+----------+ | id | username | +----+----------+ | 1 | user | +----+----------+ 1 row in set
Restrict when creating the table
CREATE TABLE `tb_user1` ( `id` BIGINT (20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '用户id', `username` VARCHAR (50) BINARY NOT NULL COMMENT '用户名', PRIMARY KEY (`id`) ) ENGINE = INNODB DEFAULT CHARSET = utf8 COMMENT = '用户表'; mysql> show create table tb_user1; tb_user1 | CREATE TABLE `tb_user1` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '用户id', `username` varchar(50) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '用户名', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户表' 1 row in set
CREATE TABLE `tb_user2` ( `id` BIGINT (20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '用户id', `username` VARCHAR (50) NOT NULL COMMENT '用户名', `info` VARCHAR (100) NOT NULL COMMENT '详情描述', PRIMARY KEY (`id`) ) ENGINE = INNODB DEFAULT CHARSET = utf8 COLLATE=utf8_bin COMMENT = '用户表'; mysql> show create table tb_user2; tb_user2 | CREATE TABLE `tb_user2` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '用户id', `username` varchar(50) COLLATE utf8_bin NOT NULL COMMENT '用户名', `info` varchar(100) COLLATE utf8_bin NOT NULL COMMENT '详情描述', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='用户表'
NGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_binAll settings of varchar type in the field will be case-sensitive. The details of these two viewing tables are essentially adding
COLLATE utf8_bin to the fields.
For more MySQL related technical articles, please visit the MySQL Tutorial column to learn!
The above is the detailed content of Fields stored in MySQL are case-insensitive, did you know?. For more information, please follow other related articles on the PHP Chinese website!