MySQL数据库的访问和基本操作命令以及SQL语句

一、MySQL数据库的访问

1.1、概述

MySQL数据库是一个典型的C/S架构的应用,要访问MySQL数据库需要使用专门的客户端软件。

1.2、Linux系统访问mysql

在Linux系统中用自带的mysql命令工具即可访问mysql数据库

[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系统图形化界面访问mysql

  • 设置mysql数据库权限,关闭防火墙
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 ##关闭增强型安全功能
  • 在W10系统中安装Navicat客户端软件,设置连接配置
    在这里插入图片描述
  • 连接完成后即可操作mysql数据库

在这里插入图片描述

二、MySQL数据库的常用命令

2.1、查看数据库列表

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

mysql> 

2.2、查看数据库中的数据表列表

  • 数据表存放在数据库中,先进入所要查看的数据表的数据库才能查看数据表信息。
  • USE 数据库名;
  • 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、显示数据表结构(字段)

  • DESCRIBE [数据库名.]表名;

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> 

三、SQL语句

3.1、SQL语句概述

  • 是结构化查询语言
  • 是关系型数据库标准语言
  • 用于维护管理数据库,如数据库查询、数据更新、访问控制、对象管理等功能

3.2、SQL语句分类

  • DDL:数据定义语言
  • DML:数据操控语言
  • DQL:数据查询语言
  • DCL:数据控制语言

四、SQL语句之DDL语句操作

4.1、DDL语句概述

DDL语句用于创建和删除数据库对象,如库、表、索引等

4.2、创建数据库

  • 格式: CREATE DATABASE 数据库名

例如:

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 TABLE 表名(字段定义…)

例如:

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、删除指定的数据表

  • 格式:DROP TABLE [数据库名.]表名

例如:

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、删除指定的数据库

  • 格式:DROP DATABASE 数据库名

例如:

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

五、SQL语句之DML语句操作

5.1、DML语句概述

  • DML语句用于对表中的数据进行管理,包括以下操作

5.2、插入新数据(INSERT)

  • 格式:INSERT INTO 表名(字段1,字段2,…)VALUES(字段1的值,字段2的值,…)

例如:

  • 创建一个abcd01的数据表,往其中插入相关数据
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)

  • 格式:UPDATE 表名 SET 字段名1=值1 WHERE条件表达式

例如:

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)

  • 格式:DELETE FROM 表名 WHERE 条件表达式

例如:

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)               ##变为空

六、SQL语句之DQL语句操作

6.1、概述

  • DQL是数据查询语句,只有一条SELECT命令
  • 用于从数据表中查找符合条件的数据记录
  • 查询时可不指定条件

6.2、数据表内容查询(SELECT)

  • 格式:selext 字段名1,字段名2… from 表名;

例如:上面查询abcd01数据表的信息("*"代表所有数据)
在这里插入图片描述

七、SQL语句之DCL语句操作

7.1、设置用户权限(GRANT)

  • 格式 :GRANT 权限列表 ON 数据库名.表名 TO 用户名@来源地址 [ IDENTIFIED BY ‘密码’ ]

例如:

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、查看用户的权限

  • SHOW GRANTS FOR 用户名@来源地址

7.3、撤销用户的权限(REVOKE)

  • REVOKE 权限列表 ON 数据库名.表名 FROM 用户名@来源地址
发布了43 篇原创文章 · 获赞 56 · 访问量 7896

猜你喜欢

转载自blog.csdn.net/weixin_42953006/article/details/103845931