MySQL database access and basic operation commands and SQL statements

First, access the MySQL database

1.1 Overview

MySQL database is a typical application of C / S architecture, to access the MySQL database requires the use of specialized client software.

1.2, Linux system access mysql

In the Linux system comes with tools to access the mysql command mysql database

[root@localhost ~]#  mysql -u root -p
Enter password:   ##输入密码
mysql>                   ##登陆成功
mysql> show databases; ##查看数据库列表
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.09 sec)
mysql>

1.3, win10 system graphical interface to access mysql

  • Set mysql database permissions, turn off the firewall
mysql> grant all privileges on *.* to 'root'@'%' identified by 'abc123' with grant option;
Query OK, 0 rows affected, 1 warning (0.11 sec) 
 ##允许数据库中的所有表,所有权限给root用户
[root@localhost ~]# systemctl stop firewalld.service  ##关闭防火墙
[root@localhost ~]# setenforce 0 ##关闭增强型安全功能
  • Navicat client software installed W10 systems, connection configuration settings
    Here Insert Picture Description
  • To complete the connection operation mysql database

Here Insert Picture Description

Two, MySQL database of commonly used commands

2.1, see the list of databases

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

mysql> 

2.2, view the data in a database table list

  • Data is stored in a database table, first enter the data you want to view the database table to view the data table.
  • USE database name;
  • SHOW TABLES;
mysql> use sys;        ##进入sys库
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> show tables;     ##查看sys库的数据表列表
+-----------------------------------------------+
| Tables_in_sys                                 |
+-----------------------------------------------+
| host_summary                                  |
| host_summary_by_file_io                       |
| host_summary_by_file_io_type                  |
//更多信息.....

2.3, the display data configuration table (field)

  • DESCRIBE [database name.] Table;

mysql> describe host_summary;   ###显示host_summary数据表的结构
+------------------------+---------------+------+-----+---------+-------+
| Field                  | Type          | Null | Key | Default | Extra |
+------------------------+---------------+------+-----+---------+-------+
| host                   | varchar(60)   | YES  |     | NULL    |       |
| statements             | decimal(64,0) | YES  |     | NULL    |       |
| statement_latency      | text          | YES  |     | NULL    |       |
| statement_avg_latency  | text          | YES  |     | NULL    |       |
| table_scans            | decimal(65,0) | YES  |     | NULL    |       |
| file_ios               | decimal(64,0) | YES  |     | NULL    |       |
| file_io_latency        | text          | YES  |     | NULL    |       |
| current_connections    | decimal(41,0) | YES  |     | NULL    |       |
| total_connections      | decimal(41,0) | YES  |     | NULL    |       |
| unique_users           | bigint(21)    | NO   |     | 0       |       |
| current_memory         | text          | YES  |     | NULL    |       |
| total_memory_allocated | text          | YES  |     | NULL    |       |
+------------------------+---------------+------+-----+---------+-------+
12 rows in set (0.00 sec)

mysql> 

Three, SQL statements,

3.1, SQL Statement Summary

  • Structured Query Language
  • It is the standard language for relational databases
  • For the maintenance and management of databases, such as database queries, data updates, access control, object management and other functions

3.2, SQL statement classification

  • DDL: Data Definition Language
  • DML: Data manipulation language
  • DQL: Data Query Language
  • DCL: Data Control Language

Four, DDL statements SQL statements of operations

4.1, DDL statements Overview

DDL statements for creating and deleting database objects such as databases, tables, indexes, etc.

4.2, create a database

  • Format: CREATE DATABASE database name

E.g:

mysql> create database abc;   ##创建abc库
Query OK, 1 row affected (0.01 sec)

mysql> show databases;  ##查看库列表
+--------------------+
| Database           |
+--------------------+
| information_schema |
| abc                |      ##abc库已创建
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)
mysql> 

4.3, create a data table

  • Format: CREATE TABLE table name (field definitions ...)

E.g:

mysql> use abc;   ##进入abc库
Database changed
mysql> create table abc01(        ##创建abc01表                                                                          
     -> id int not null,              ##定义表中字段
    -> name char(10) not null,     
    -> address varchar(50) default 'BJ',
    -> primary key (id));
Query OK, 0 rows affected (0.43 sec)
mysql>  describe abc01;     ###查看数据表结构
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id      | int(11)     | NO   | PRI | NULL    |       |
| name    | char(10)    | NO   |     | NULL    |       |
| address | varchar(50) | YES  |     | BJ      |       |
+---------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
mysql> 

4.4, delete the specified data table

  • Format: DROP TABLE [database name.] Table

E.g:

mysql> drop table abc.abc01;   ##删除abc01数据表
Query OK, 0 rows affected (0.04 sec)
mysql> use abc;    ##进入abc库
Database changed
mysql> show tables; ##查看数据表列表
Empty set (0.00 sec)
                             ####没有任何数据表,abc01数据表已被删除
mysql> 

4.5, delete the specified database

  • Format: DROP DATABASE database name

E.g:

mysql> drop database abc;   ##删除abc数据库
Query OK, 0 rows affected (0.01 sec)     ##ok,删除成功

Five, DML statements SQL statements of operations

5.1, DML statements Overview

  • DML statement for managing data in the table, including the operations of

5.2, insert new data (INSERT)

  • Format: INSERT INTO table name (field 1, field 2, ...) (the value of the field 1, field 2, ...) the VALUES

E.g:

  • Abcd01 create a data table, to insert the relevant data
mysql> create database abcd;     ##创建abcd库
Query OK, 1 row affected (0.00 sec)
mysql> use abcd;                 ##进入abcd库
Database changed
mysql> create table abcd01(           ##创建abcd01表
    -> id int(4) not null,
    -> name char(10) not null,    
    -> address varchar(50) default 'NJ',
    -> primary key (id));
Query OK, 0 rows affected (0.04 sec)
mysql> desc abcd01;         ##显示abcd01表的结构
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id      | int(4)      | NO   | PRI | NULL    |       |
| name    | char(10)    | NO   |     | NULL    |       |
| address | varchar(50) | YES  |     | NJ      |       |
+---------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
mysql> insert into abcd01(id,name)values(1,'zhangsan');  
                  ##插入数据id=1,姓名zhangsan
mysql> select * from abcd01;  ##查看表中数据内容
+----+----------+---------+
| id | name     | address |            ##id已经为1,姓名为zhangsan
+----+----------+---------+
|  1 | zhangsan | NJ      |
+----+----------+---------+
1 row in set (0.00 sec)
mysql> 

5.3, update existing data (UPDATE)

  • Format: UPDATE table SET field name 1 = value of the conditional expression 1 the WHERE

E.g:

mysql> update abcd01 set name='wangwu' where id =1;  
              ##修改id为1的字段姓名为wangwu
mysql> select * from abcd01;  ##显示表中数据内容
+----+--------+---------+
| id | name   | address |
+----+--------+---------+             ##姓名已变为wangwu
|  1 | wangwu | NJ      |
+----+--------+---------+
1 row in set (0.00 sec)

5.4, ​​delete unnecessary data (DELETE)

  • Format: DELETE FROM table WHERE conditional expression

E.g:

mysql> delete from  abcd01  where id =1;  ##删除id为1的字段的数据
Query OK, 1 row affected (0.01 sec)

mysql> select * from abcd01;     ##显示表中数据内容
Empty set (0.00 sec)               ##变为空

Six, DQL statements SQL statements of operations

6.1 Overview

  • DQL is a data query, only one SELECT command
  • To find qualified data from the data table records
  • Queries may specify conditions

6.2, query data table of contents (SELECT)

  • Format: selext field names 1, 2 ... from the field name table;

For example: the above query information abcd01 data table ( "*" represents all of the data)
Here Insert Picture Description

Seven, DCL statements SQL statements of operations

7.1, set user permissions (GRANT)

  • Format:. GRANT privileges ON database name list table name TO username @ Source Address [IDENTIFIED BY 'password']

E.g:

mysql> grant all privileges on *.* to 'root'@'%' identified by 'abc123' with grant option;
Query OK, 0 rows affected, 1 warning (0.11 sec) 
 ##允许数据库中的所有表,所有权限给root用户

7.2, see the user's permissions

  • SHOW GRANTS FOR username @ Source Address

7.3, revoke a user's privilege (REVOKE)

  • REVOKE privileges ON database name list. Table name FROM user name @ Source Address
Published 43 original articles · won praise 56 · views 7896

Guess you like

Origin blog.csdn.net/weixin_42953006/article/details/103845931