Gorm method of operating MySql database

This article mainly introduces the method for gorm to operate MySql database. This article introduces you in very detailed, which has a certain reference value for everyone's study or work, and friends who need it can refer to it.

When using gorm query, the account name A and a are the same, because mysql is case-insensitive by default.

1. The cause of the problem

MySQL is not case sensitive under Windows.

The case rules of MySQL database names, table names, column names, and aliases under Linux are as follows:

1. The database name and table name are strictly case-sensitive;

2. The alias of the table is strictly case-sensitive;

3. Column names and column aliases are case-ignoring in all cases;

4. Variable names are also strictly case-sensitive;

MySQL is case-insensitive when querying strings. The ISO-8859 character set is generally used as the default character set when compiling MySQL. This character set is case-insensitive, so the Chinese coded character case during the comparison process Conversion caused this phenomenon.

2. Sorting rules in mysql

utf8_bin stores each character in the string as binary data, which is case sensitive. utf8_genera_ci is not case sensitive, and ci is the abbreviation of case insensitive, that is, case insensitive. utf8_general_cs is case sensitive, cs is the abbreviation of case sensitive, that is, case sensitive. (Note: In Mysql5.6.10 version, utf8_genral_cs is not supported!!!

3. Solution

1. Binary keyword

Modify the sql query statement directly, and add the binary keyword in front of the field to be queried. (Not recommended)

1. Add the binary keyword before each condition

1 select * from user where binary username = 'admin' and binary password = 'admin';

2. Surround the parameters with binary ('')

1 select * from user where username like binary('admin') and password like binary('admin');

2. Modify the Collation property

When creating a table, directly set the collate attribute of the table to utf8_general_cs or utf8_bin; if the table has been created, directly modify the collation attribute of the field to utf8_general_cs or utf8_bin.

1. Modify the table structure

1 ALTER TABLE TABLENAME MODIFY COLUMN COLUMNNAME VARCHAR(50) BINARY CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL;

2. Modify the field (use gorm to set the field in the table to be case sensitive)

1 `gorm:"unique" sql:"type:VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_bin"`

Guess you like

Origin blog.csdn.net/hbznd/article/details/114529160