Skip to content

Commit

Permalink
adding MySql doc on how to generate case insensitive columns
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvinrfr committed Jan 25, 2022
1 parent 2fa788f commit 4b646f8
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Repo to hold important notes, links and guides to help throughout the carrer
1. [How to run performance test with locust](/locust/how_to_run_performance_test_with_locust.md)

## MySQL
1. [How to create table with case insensitive columns](/mysql/how_to_create_table_with_case_insensitive_columns.md)
1. [How to insert blob](/mysql/how_to_insert_blob.md)

## Powershell
Expand Down
57 changes: 57 additions & 0 deletions mysql/how_to_create_table_with_case_insensitive_columns.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# How to create table with case insensitive columns

MySQL includes character set support that enables you to store data using a variety of character sets and perform comparisons according to a variety of collations. The default MySQL server character set and collation are utf8mb4 and utf8mb4_0900_ai_ci, but you can specify character sets at the server, database, table, column, and string literal levels.

- [Chapter 10 Character Sets, Collations, Unicode](https://dev.mysql.com/doc/refman/8.0/en/charset.html)
- [10.3 Specifying Character Sets and Collations](https://dev.mysql.com/doc/refman/8.0/en/charset-syntax.html)
- [10.3.1 Collation Naming Conventions](https://dev.mysql.com/doc/refman/8.0/en/charset-collation-names.html)

Every “character” column (that is, a column of type CHAR, VARCHAR, a TEXT type, or any synonym) has a column character set and a column collation. Column definition syntax for CREATE TABLE and ALTER TABLE has optional clauses for specifying the column character set and collation:

```sql
col_name {CHAR | VARCHAR | TEXT} (col_length)
[CHARACTER SET charset_name]
[COLLATE collation_name]
```
These clauses can also be used for ENUM and SET columns:

```sql
col_name {ENUM | SET} (val_list)
[CHARACTER SET charset_name]
[COLLATE collation_name]
```


Examples
```sql
CREATE TABLE t1
(
col1 VARCHAR(5)
CHARACTER SET latin1
COLLATE latin1_german1_ci
);


ALTER TABLE t1 MODIFY
col1 VARCHAR(5)
CHARACTER SET latin1
COLLATE latin1_swedish_ci;


CREATE TABLE t2
(
col1 CHAR(10) CHARACTER SET utf8 COLLATE utf8_unicode_ci
) CHARACTER SET latin1 COLLATE latin1_bin;

CREATE TABLE t3
(
col1 CHAR(10) CHARACTER SET utf8
) CHARACTER SET latin1 COLLATE latin1_bin;
```

[10.3.5 Column Character Set and Collation](https://dev.mysql.com/doc/refman/8.0/en/charset-column.html)


In MySQL(5.7) we can create a table with case insensitive columns by using "COLLATE utf8mb4_unicode_ci" in the column definition.

In MySQL(8.0) and up we can create a table with Case insensitive and Accent sensitive columns by declaring "COLLATE utf8mb4_0900_as_ci" in the column definition.

0 comments on commit 4b646f8

Please sign in to comment.