[MySql] Addition, deletion, modification and query of database

The main purpose of this article: How to add, delete, query and modify the database

Create database create

The main details are in the option question, the encoding option

CREATE DATABASE [IF NOT EXISTS] db_name [create_specification [,
create_specification] ...]
create_specification:
[DEFAULT] CHARACTER SET charset_name
[DEFAULT] COLLATE collation_name

illustrate:

Uppercase indicates that the keyword
[] is optional
CHARACTER SET: specifies the character set used by the database
COLLATE: specifies the validation rules for the database character set

  • View the list of current user databases show databases;

image-20230606163930320

  • Create database create database db_name;

image-20230606164020469

When we create data without specifying character set and verification rules, the system uses the default character set: utf8, verification rules: utf_general_ci;

Simply verify: create a database create database d1, and then go to /var/lib/mysql/d1/db.opt to view:,

image-20230606170835046

  • Delete database drop database db_name;

image-20230606164118583

Create a database: create database db_name (essentially, Linux creates a directory in /var/lib/mysql), delete the database: drop database db_name; (delete the directory)

For example, we create a directory youcanseeme under /var/lib/mysql, and use the mysql command show databases; it can also be seen naturally. (But it is very unreasonable to manually create a directory with mkdir in /var/lib/mysql)

  • Create a database that does not exist (if not exists) create database if not exists database1;

image-20230606164534363

  • database encoding problem

创建数据库的时候,有两个编码集:1.数据库编码集 2.数据库校验集

Database encoding set - the database will store data in the future

Database check set ——supports the encoding used by the database for field comparison, and is essentially an encoding format used to read data in the database

No matter what operation the database performs on the data, it must ensure that the operation and encoding must be consistent! **Prevent garbled characters

  • View the system default character set and verification rules

Encoding set: show variables like 'character_set_database';

image-20230606165232619

Validation set: show variables like 'collation_database';

image-20230606165436555

In addition to the database, what about others: show variables like 'collation_%';

image-20230606165535828

If you want to view the character set supported by the database: show charset;

If you want to view the character set verification rules supported by the database: show collation;

  • Create a database with the specified encoding set

If not specified, the configuration file is the default

There are two ways to create a table whose character set is utf8:

create database d2 charset=utf8;
create database d3 character set utf8;

image-20230606171035300

image-20230606171058791

Set the character set and checksum at the same time:

image-20230606171552333

image-20230606171633156

image-20230606171914824

  • The impact of validation rules on the database

To illustrate this situation, we now create two databases:

The test1 database verification set is set to utf8_general_ci; the default character set is utf8; the verification rule uses utf8_ general_ ci [case-insensitive]

The test2 database verification set is set to utf8_bin; the character set defaults to utf8; the verification rule uses utf8_ bin [case-sensitive]

image-20230606174058107

image-20230606174309208

  • test1 database

Now look at the database test1 first, insert data into the database test1, first use the database use test1; create a table person

 create table if not exists person (name varchar(20));

Insert data:

mysql> insert into person (name) values ('a');
Query OK, 1 row affected (0.01 sec)

mysql> insert into person (name) values ('b');
Query OK, 1 row affected (0.00 sec)

mysql> insert into person (name) values ('A');
Query OK, 1 row affected (0.01 sec)

mysql> insert into person (name) values ('B');
Query OK, 1 row affected (0.00 sec)

mysql> insert into person (name) values ('c');
Query OK, 1 row affected (0.01 sec)

mysql> insert into person (name) values ('D');
Query OK, 1 row affected (0.00 sec)

At this time, the table person, query table person:

image-20230606184457001

Query a in the person table: the result is that both uppercase and lowercase can be found, but utf8_general_ci is not case sensitive:

image-20230606184745103

Take a look at the sorted results:

image-20230606185739270

The verification rule in test1 is that when utf8_general_ci is compared, case is not distinguished when performing verification, and the verification set will affect the result. Generally, we follow the default

  • test2 database

Now look at the database test2, use the database use test2;, create a table person:

create table if not exists person (name varchar(20));

Insert data, view table person:

image-20230606185157251

Query a in the person table: the result is only lowercase: this is utf8_bin case-sensitive:

image-20230606185257603

For sorting, the person in the database test2 is in ascending order by default, according to the ascii value from small to large:

image-20230606185545237

view databaseshow

show databases;

image-20230606190034481

  • use database use db_name;

After checking, you can’t use the database directly. If you want to use the database, use use+database name; such as using the database helloworld;

use helloworld;
  • Confirm the currently used database select database();
select database();

Currently using the test1 database

image-20230606190917925

delete database drop

DROP DATABASE [IF EXISTS] db_ name;

The result after performing the delete:

The database folder corresponding to the corresponding database cannot be seen inside the database, and
the corresponding database folder is deleted, cascading deletion, and all the data tables in it are deleted

Note: Do not delete the database at will

//删除数据d5\d4\d3;
mysql> drop database d5;
Query OK, 0 rows affected (0.00 sec)

mysql> drop database d4;
Query OK, 0 rows affected (0.00 sec)

mysql> drop database d3;
Query OK, 0 rows affected (0.00 sec)

Modify the database alter

ALTER DATABASE db_name
[alter_spacification [,alter_spacification]...]
alter_spacification:
[DEFAULT] CHARACTER SET charset_name
[DEFAULT] COLLATE collation_name

Use alter to modify the database

The modification of the database mainly refers to modifying the character set of the database, and the verification rules

alter database test2 charset=gbk collate gbk_chinese_ci;

After modification, view the information of database test2:

image-20230606192003148

/*!40100 DEFAULT CHARACTER SET gbk */: This is not a comment, indicating that the current mysql version is greater than version 4.01, just execute this sentence

Guess you like

Origin blog.csdn.net/weixin_60478154/article/details/131074488