[MySQL Getting Started Guide] Database Basic DDL Operations

MySQL library operation

1. SQL statement

  • DDL (data definition language): Data definition language, used to maintain the attribute structure of data tables and databases. The representative command hascreate/drop/alter
  • DML (data manipulation language): data manipulation language, used to modify file content
  • DCL (Data Control Language): Data Control Language, mainly responsible for database security and authority management

In this article, we will focus on adding, deleting, checking, and modifying operations on the database. It should be noted that we only modify the attribute structure of the database, but not the content of the database, so the SQL statements introduced next are all DDL statements.

​ Different versions of MySQL may actually be different. The version of MySQL used in this tutorial is 5.7

2. Create a database

1. Grammar

CREATE DATABASE [IF NOT EXISTS] db_name [CHARACTER SET   charset_name] [COLLATE collation_name]

illustrate:

  • [] indicates that it can be added or not
  • CHARACTER SET: Specifies the character set used by the database. If not, use the default selected in the configuration file
  • COLLATE: Specifies the validation rules for the character set of the database. If not, use the default selected in the configuration file
  • create, database are keywords in mysql. mysql pairkeywordsThe case is insensitive (note that it is only insensitive to keywords, but it is still sensitive to database names and data table names, for example), for the sake of readability, it will be presented in lowercase afterwards

(Character sets and checksums will be introduced later)

2. Case

  • Create a database called db1

    create database db1;
    
  • Plus if not exists. Only created if the target database does not exist

    image-20230410162253905

  • Create a database using the utf-8 character set

    create database db2 charset=utf8;  -- 写法1
    create database db2 character set utf8;  -- 写法2
    
  • Create a database that uses the utf-8 character set and the checksum is utf8_general_ci

    create database db3 charset=utf8 collate utf8_general_ci;
    create database db3 charset=utf8 collate=utf8_general_ci;
    
  • Create a database named create. Therefore, create is a keyword in mysql, if we need to use it as the database name, we need to add ``.

    create database `create`
    

​ If the character set and verification set are not set, the default settings will be used; otherwise, according to the principle of proximity, the user-specified ones will be used. In general, bothNot recommendedThe user sets the character set and verification set by himself

3. The method is extremely not recommended

​ When we create a database, on a physical level, the essence is to create a corresponding folder for us under var/lib/mysqlthe path :

image-20230410163949437

​ If we manually create a directory under this path, it will be recognized as a database by mysql, but this approach is not recommended. It's like your leader's leader directly sends you a task, which is obviously messed up.

image-20230410164141431

image-20230410164214654

3. View the database

1. Grammar

show databases;   -- 显示所有的数据库
show create database db_nams  -- 显示创建一个数据库的指令

image-20230410164522960

image-20230410164354443

Fourth, modify the database

Basic syntax:

ALTER DATABASE db_name [alter_spacification……]

Common modifications:

  • Modify character set and checksum

    alter database db1 charset=gbk;
    

    image-20230411193031532

    ​ Comparing before and after modification, we found that although we only modified the character set, the encoding set will be automatically adjusted to the corresponding one. And the database creation statement will change

    image-20230411193225025

  • Note that modifying the database name is not supported under mysql 5.7. The behavior of modifying the database name itself is extremely not recommended, and it should be clear at the beginning, otherwise it will easily affect the upper-level business.

5. Delete the database

drop database [if exists] db_ name;

When you delete a database, everything in the database is deleted. Be careful!

6. Character set and verification rules

1. what is

  • A character set refers to the encoding rules used to represent characters. In MySQL, the character set determineshow to storeCharacter data, and how to pass character data to the client. MySQL supports a variety of character sets, and the selection of these character sets will affect the reliability of data storage, the correctness of data transmission, and the results of operations such as text comparison and sorting.
  • Validation rules are used to specify character datacomparison method. In MySQL, validation rules determine how to compare strings, including how to distinguish case, how to sort different characters, and so on. MySQL supports a variety of validation rules, which may produce different comparison results on different occasions. Correct selection of validation rules can ensure the correctness of operations such as data comparison and sorting

image-20230411085554502

The dp.opt file in the database folder saves the character set and checksum used by the database

2. Related instructions

  • View the character set and validation rules used by the database by default

    show variables like 'character_set_database';
    show variables like 'collation_database';
    

    image-20230411084407360

  • See more with fuzzy queries

    show variables like 'character%';
    show variables like 'collation%';
    

    image-20230411084550805

  • View all character sets supported by the database. You can see that each character set matches a checksum by default

    show charset;
    

    image-20230411084742476

    (part)

  • View all checksums supported by the database. A character set can actually correspond to multiple checksums

    show collation;
    

    image-20230411084936410

3. The impact of verification rules

​ We mentioned earlier that check sets are used for comparison between character data. Different checksums will also affect case sensitivity and sorting.

  • Create two databases with different character sets
create database db2 collate utf8_general_ci; -- 不区分大小写	
create database test2 collate utf8_bin; -- 区分大小写	
  • Insert the following data respectively
insert into person values('a');
insert into person values('A');
insert into person values('b');
insert into person values('B');
  • search result:
select * from person where name='a';

image-20230411091902442

  • Sort results
select* from person order by name

image-20230411092130302

Seven, backup database

1. Basic grammar

​ We mentioned earlier that at the physical level, the database is essentially a folder under var/lib/mysqlthe path , and the data table is a file in the folder. If we want to copy a database, although we can also use cpthe command copy folder to copy the database, but the same behavior is extremely not recommended.

​ MySQL provides a command-line tool mysqldumpspecifically for backing up and restoring the mysql database. Its usage is as follows:

mysqldump -P 3306 -uroot -p password -B database > backup.sql

illustrate:

  • -P indicates the port number of the mysql server
  • -uroot is equivalent to -u root, both can be written
  • -p indicates the password of the database
  • -B indicates the name of the database to be backed up
  • > backup.sql Indicates that the data result will be redirected to the backup.sql file, otherwise it will be output to the screen

​ We can easily find from the following saved results that the database backup is not to back up specific data for us in essence, but to save the historical SQL statements that created the database for us (MySQL will optimize it for us). In this way, in addition to backing up the data itself, it can also include the complete structure, constraints, triggers, stored procedures and other information of the database, so that the consistency and reliability of the data can be better guaranteed during backup and restoration .

image-20230411195515903

The recovery method is as follows (command under mysql):

source xxx  -- 复原xx路径下的文件

2. Precautions

  • How to backup only one or more tables in the database?

     mysqldump -u root -p 数据库名 表名1 表名2 > /mytest.sql
    
  • How to back up multiple databases at the same time

    mysqldump -u root -p -B 数据库名1 数据库名2 ... > /mytest.sql
    
  • If there is no -Bparameter when backing up a database, when restoring the database, you need to create an empty database (create), then use the database (use), and then use the source to restore

Guess you like

Origin blog.csdn.net/whc18858/article/details/130122137