MySQL Basics: Master these common commands to make your database operations more efficient!

Insert image description here

View msyql version

MySQL program options come in two general forms:

  1. Long options, consisting of two minus signs before the word
  2. Short option consisting of a single letter preceded by a minus sign
root@mysql-server:/home# mysql -V
mysql  Ver 14.14 Distrib 5.7.17, for Linux (x86_64) using  EditLine wrapper
root@mysql-server:/home# mysql --version
mysql  Ver 14.14 Distrib 5.7.17, for Linux (x86_64) using  EditLine wrapper
root@mysql-server:/home#

Create database

    CREATE DATABASE [IF NOT EXISTS] <数据库名>
    [[DEFAULT] CHARACTER SET <字符集名>] 
    [[DEFAULT] COLLATE <校对规则名>];

[]The content in is optional. The syntax is explained as follows:

  • <Database name>: The name of the created database. The data storage area of ​​MySQL will represent the MySQL database in a directory format, so the database name must comply with the folder naming rules of the operating system, cannot start with a number, and should be as meaningful as possible. Note that there is no case sensitivity in MySQL.
  • IF NOT EXISTS: Make a judgment before creating the database. The operation can only be performed if the database does not currently exist. This option can be used to avoid duplicate creation errors when the database already exists.
  • [DEFAULT] CHARACTER SET: Specifies the character set of the database. The purpose of specifying the character set is to avoid garbled data stored in the database. If you do not specify a character set when creating the database, the system's default character set is used.
  • [DEFAULT] COLLATE: Specifies the default collation rules for the character set.

Example 1:

    mysql> CREATE DATABASE IF NOT EXISTS test_db_char
        -> DEFAULT CHARACTER SET utf8
        -> DEFAULT COLLATE utf8_chinese_ci;
    Query OK, 1 row affected (0.03 sec)

Query the currently used database

mysql> select database();
+------------+
| database() |
+------------+
| test       |
+------------+
1 row in set (0.03 sec)

View database

In MySQL, you can use **SHOW DATABASES**the statement to view or display databases within the scope of the current user's permissions. The syntax format for viewing the database is:

   SHOW DATABASES [LIKE '数据库名'];

Example 2:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| bjpowernode        |
| eladmin            |
| mysql              |
| performance_schema |
| sqlalchemy         |
| sys                |
| test               |
+--------------------+
8 rows in set (0.08 sec)

Modify database

In MySQL, you can use it **ALTER DATABASE**to modify related parameters of a database that has been created or exists. The syntax format for modifying the database is:

    ALTER DATABASE [数据库名] { 
    [ DEFAULT ] CHARACTER SET <字符集名> |
    [ DEFAULT ] COLLATE <校对规则名>}

The syntax is explained as follows:

  • ALTER DATABASE is used to change the global attributes of the database.
  • Using ALTER DATABASE requires database ALTER permission.
  • The database name can be ignored, in which case the statement corresponds to the default database.
  • The CHARACTER SET clause is used to change the default database character set.

Example 3:

mysql> show create database sqlalchemy;
+------------+--------------------------------------------------------------------------------------+
| Database   | Create Database                                                                      |
+------------+--------------------------------------------------------------------------------------+
| sqlalchemy | CREATE DATABASE `sqlalchemy` /*!40100 DEFAULT CHARACTER SET utf8 COLLATE utf8_bin */ |
+------------+--------------------------------------------------------------------------------------+
1 row in set (0.13 sec)


mysql> alter database sqlalchemy
    -> default character set gb2312
    -> default collate gb2312_chinese_ci;
Query OK, 1 row affected (0.09 sec)

mysql> show create database sqlalchemy;
+------------+-----------------------------------------------------------------------+
| Database   | Create Database                                                       |
+------------+-----------------------------------------------------------------------+
| sqlalchemy | CREATE DATABASE `sqlalchemy` /*!40100 DEFAULT CHARACTER SET gb2312 */ |
+------------+-----------------------------------------------------------------------+
1 row in set (0.01 sec)

Delete database

In MySQL, when you need to delete a created database, you can use **DROP DATABASE**the statement. Its syntax format is:

 DROP DATABASE [ IF EXISTS ] <数据库名>

The syntax is explained as follows:

  • <Database name>: Specify the name of the database to be deleted.
  • IF EXISTS: Used to prevent errors from occurring when the database does not exist.
  • DROP DATABASE: Delete all tables in the database and delete the database at the same time. Be very careful when using this statement to avoid mistaken deletions. If you want to use DROP DATABASE, you need to obtain the database DROP permission.

Exit mysql

Can use \q, QUIT or EXIT

mysql> \q
Bye
root@mysql-server:/#

Guess you like

Origin blog.csdn.net/sinat_28521487/article/details/132684466