Database---mysql(02)--database operation

MYSQL

1. Log in to mysql --- mysql command to log in to the database

Syntax : mysql -u username -p password -h the ip of the connected database server [-D] database name -P port

# 登录本地的mysql
[root@localhost ~]# mysql -u root -p
Enter password:
mysql> select version(); -- 获取当前版本信息
+-----------+
| version() |
+-----------+
| 8.0.33    |
+-----------+
1 row in set (0.00 sec)

mysql> select database(); -- 查询当前数据库名
+------------+
| database() |
+------------+
| mysql      |
+------------+
1 row in set (0.00 sec)

mysql> use sys; -- 使用sys数据库

Database changed
mysql> select user(); -- 返回当前用户名和主机名的组合
+-----------------+
| user()          |
+-----------------+
| root@localhost  |
+-----------------+
1 row in set (0.00 sec)

mysql> select current_user(); -- 获取当前用户身份
+-----------------------------------+
| current_user()                    |
+-----------------------------------+
| skip-grants user@skip-grants host |
+-----------------------------------+
1 row in set (0.00 sec)

2. Basics of sql statements

(1 Introduction:

SQL: Structured Query Language (Structured Query Language) , a standard language for performing data manipulation, data retrieval, and data maintenance on relational databases.

<1> Function

  • Change the structure of the database
  • Change system security settings
  • Increase user permissions on a database or table
  • Retrieve the required information in the database
  • Update the information in the database

<2> classification

SQL statements can be mainly divided into the following categories:

DDL (Data Definition Language): Data definition language, which defines operations on database objects (libraries, tables, columns, indexes). CREATE, DROP, ALTER, RENAME, TRUNCATE, etc.

DML (Data Manipulation Language): Data manipulation language, which defines the operation of database records. INSERT, DELETE, UPDATE, etc.

DQL (Data Query Language) data query language: SELECT statement.

DCL (Data Control Language): Data Control Language, which defines access rights and security levels for databases, tables, fields, and users. GRANT, REVOKE, etc.

TCL (Transaction Control): transaction control. COMMIT, ROLLBACK, SAVEPOINT, etc.

Note: You can use help to view the help information for these statements.

<3> Writing specifications

  • In the database system, SQL statements are not case-sensitive (uppercase is recommended).
  • But string constants are case sensitive.
  • SQL statements can be written in one or more lines, and end with ";".
  • Keywords cannot span multiple lines or be abbreviated.
  • Use spaces and indentation to improve statement readability.

<4> Comments

[1] Single-line comment : "-- "

mysql> -- select user,host from mysql.user;
mysql> select user,host from mysql.user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
+------------------+-----------+
4 rows in set (0.00 sec)

mysql> select user,host from mysql.user where user='root'; -- 获取user字段为root的列
+------+-----------+
| user | host      |
+------+-----------+
| root | localhost |
+------+-----------+
1 row in set (0.01 sec)

mysql> select user,host from mysql.user -- where user='root';
    -> ;
-- mysql.user后面的都被注释了,所以在下一行得添加;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
+------------------+-----------+
4 rows in set (0.00 sec)

[2] Multi-line comment : /* Multi-line comment content */

mysql> select user,host from mysql.user /*
   /*> 12345678u
   /*> sfgwrhrdhnt */
    -> ;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
+------------------+-----------+
4 rows in set (0.00 sec)

(2) Database operation

<1> view

Grammar :SHOW DATABASES [LIKE wild];

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> show databases like 'mysql';
+------------------+
| Database (mysql) |
+------------------+
| mysql            |
+------------------+
1 row in set (0.01 sec)

-- % : 任意0个或多个任意字符
-- _ : 任意单个字符
mysql> show databases like '__s%';
+-----------------+
| Database (__s%) |
+-----------------+
| mysql           |
| sys             |
+-----------------+
2 rows in set (0.00 sec)

<2> Create:

Grammar :CREATE DATABASE [IF NOT EXISTS] 数据库名;

mysql> create database mysql; -- mysql数据库存在,所以创建失败
ERROR 3552 (HY000): Access to system schema 'mysql' is rejected.
mysql> create database student;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| student            |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> show create database student; -- 查看创建数据库的流程
+----------+----------------------------------------------------------------------------------------------------+
| Database | Create Database                                                                                    |
+----------+----------------------------------------------------------------------------------------------------+
| student  | CREATE DATABASE `student` /*!40100 DEFAULT CHARACTER SET gbk */ /*!80016 DEFAULT ENCRYPTION='N' */ |
+----------+----------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

<3> Delete:

Grammar :DROP DATABASE [IF EXISTS]数据库名;

mysql> drop database student;
Query OK, 0 rows affected (0.01 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

<4> switch:

Grammar :USE 数据库名;

mysql> use mysql; 
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select database(); -- 查看当前使用的数据库
+------------+
| database() |
+------------+
| mysql      |
+------------+
1 row in set (0.00 sec)

-- 设置提示符显示当前使用的数据库名称
[root@localhost ~]# vim /etc/my.cnf
[mysql]
prompt=mysql8.0 [\\d]>
[root@localhost ~]# systemctl start mysqld
[root@localhost ~]# mysql -u root -p
Enter password:

mysql8.0 [(none)]>use mysql;
Database changed
mysql8.0 [mysql]>use sys; -- 已显示当前使用的数据库名
Database changed
mysql8.0 [sys]> -- 数据库切换,显示依旧切换

<5> Execute system commands:

Syntax : system command

mysql8.0 [sys]>system ls;
20230605         cron_task    Desktop    Downloads  initial-setup-ks.cfg  mysql80-community-release-el8-4.noarch.rpm  Public     Videos
anaconda-ks.cfg  dead.letter  Documents  echo       Music                 Pictures                                    Templates
mysql8.0 [sys]>system ls -l;
total 32
drwxr-xr-x. 2 root root    21 Jun  5 09:14 20230605
-rw-------. 1 root root  1488 Oct 21  2022 anaconda-ks.cfg
-rw-r--r--. 1 root root    12 Jun  5 10:34 cron_task
...

Guess you like

Origin blog.csdn.net/weixin_62443409/article/details/131679480