mysql string query case sensitive

I have encountered a problem a few days ago.

When mysql queries based on strings:

select * from poc_sku where barcode = 'ZR01961050100'

 

The detected barcode value is zr01961050100.

That is, mysql defaults to the strings 'zr' and 'ZR' being the same. But in java, if it is case sensitive, obviously this is not right.

If you want to query string case, the solution is very simple. When querying, use the keyword binary  to specify that the corresponding column is converted into binary. For example, the above sql becomes:

select * from poc_sku where binary barcode = 'ZR01961050100'。

 

 

The above is only simple processing when querying, but it seems unreasonable to do binary conversion every time. The best way is to specify the relevant columns are case sensitive when creating the table. for example:

Originally built table sql: (case insensitive at this time)

CREATE TABLE `poc_sku` (
  `poc_sku_id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '商品ID',
  `barcode` VARCHAR(128) NOT NULL DEFAULT '' COMMENT 'Commodity barcode',
  `poc_vendor_code` VARCHAR(50) NOT NULL DEFAULT '' COMMENT 'Vendor code',
  PRIMARY KEY (`poc_sku_id`)
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='Basic product information'
 

 

After modification, specifying that the barcode column is highly case-sensitive :

CREATE TABLE `poc_sku` (
  `poc_sku_id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '商品ID',
  `barcode` VARCHAR(128) BINARY NOT NULL DEFAULT '' COMMENT 'Product barcode',
  `poc_vendor_code` VARCHAR(50) NOT NULL DEFAULT '' COMMENT 'Vendor code',
  PRIMARY KEY (`poc_sku_id`)
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='Basic product information'
 

 

If the table has been built previously, and there is already a lot of data online, it is not convenient to rebuild the drop table, you can also use the ddl to modify the table structure to make changes, such as:

ALTER TABLE poc_sku MODIFY `barcode` VARCHAR(128) BINARY NOT NULL DEFAULT '' COMMENT 'Product barcode';

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326485458&siteId=291194637