mysql 大小写踩坑

mysql创建表时,对于varchar等类型字段如果不指明binary的话查询是不区分大小写的,
例:

#建表语句:
  CREATE TABLE `users` (
  `username` varchar(50) NOT NULL,
  `password` varchar(500) NOT NULL,
  `enabled` tinyint(1) NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
#查询语句的返回结果相同
select * from `users` where username = ‘A‘; 
select * from `users` where username = ‘a‘; 

踩坑场景:

  INSERT INTO users(username,`password`,enabled) VALUES(‘A‘,‘A‘,1);
  INSERT INTO users(username,`password`,enabled) VALUES(‘a‘,‘a‘,1);
  UPDATE users SET `password` = "b" WHERE username="a";
  #A a都被更新

解决办法:

#1 字段声明binary
  CREATE TABLE `users` (
  `username` varchar(50) binary NOT NULL,
  `password` varchar(500) NOT NULL,
  `enabled` tinyint(1) NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
#2 查询声明binary
select * from `users` where binary username = ‘a‘;

mysql 大小写踩坑

上一篇:linuxIO刷新机制fsync和fdatasync详解


下一篇:分享一款生成数据库表结构文档的工具