How to operate MySql database with gorm

How to operate MySql database with gorm

1. Setting case sensitivity of fields in the table

When using gorm query, the account names A and a may be the same, because MySQL is case-insensitive by default.

1. Causes of the problem

MySQL is not case sensitive under Windows .
The case rules for MySQL database names, table names, column names, and aliases under Linux are as follows:

1. Database names and table names are strictly case-sensitive;
2. The table alias is strictly case-sensitive;
3. Column names and column aliases are case-insensitive in all cases;
4. Variable names are also strictly case-sensitive;

MySQL is case-insensitive when querying strings. When compiling MySQL, the ISO-8859 character set is generally used as the default character set. This character set is case-insensitive. Therefore, the case conversion of Chinese encoded characters during the comparison process causes this phenomenon.

2. Sorting rules in MySQL

utf8_bin stores each character in a string as binary data, distinguishing between uppercase and lowercase letters. utf8_genera_ci is case-insensitive. ci is the abbreviation of case insensitive, which means it is case-insensitive. utf8_general_cs is case-sensitive. cs is the abbreviation of case sensitive. ( Note: In MySQL 5.6.10, utf8_genral_cs is not supported!!!

3. Solution

1. Binary keyword

Simply modify the SQL query statement and add the binary keyword before the field to be queried. (Not recommended)

1. Add the binary keyword before each condition

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

2. Surround the parameters with binary('')

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

2. Modify Collation properties

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

1. Modify the table structure

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

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

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

This is the end of this article about how to use gorm to operate MySql database. For more information about how to use gorm to operate MySql database, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • golang gorm basic usage of mysql and gorm
  • Implementation of gORM operation on MySQL

<<:  Difference between HTML ReadOnly and Enabled

>>:  CSS and JS to achieve romantic meteor shower animation

Blog    

Recommend

How to quickly modify the host attribute of a MySQL user

When you log in to MySQL remotely, the account yo...

Vue imports Echarts to realize line scatter chart

This article shares the specific code of Vue impo...

Solution for FileZilla 425 Unable to connect to FTP (Alibaba Cloud Server)

Alibaba Cloud Server cannot connect to FTP FileZi...

Sharing tips on using scroll bars in HTML

Today, when we were learning about the Niu Nan new...

Improvements to the web server to improve website performance

<br />In the first section of this series, w...

Detailed explanation of important cascading concepts in CSS

Recently, I encountered a problem in the process ...

Examples of correct use of maps in WeChat mini programs

Table of contents Preface 1. Preparation 2. Actua...

Summary of basic usage of CSS3 @media

//grammar: @media mediatype and | not | only (med...

Installation, activation and configuration of ModSecurity under Apache

ModSecurity is a powerful packet filtering tool t...

Correct use of MySQL partition tables

Overview of MySQL Partitioned Tables We often enc...

In-depth explanation of MySQL isolation level and locking mechanism

Table of contents Brief description: 1. Four char...

Detailed explanation of execution context and call stack in JavaScript

Table of contents 1. What is the execution contex...

Summary of relevant knowledge points of ajax in jQuery

Preface Students who learn JavaScript know that A...

MySQL binlog opening steps

Binlog is a binary log file that is used to recor...

How to implement a password strength detector in react

Table of contents Preface use Component Writing D...