MySQL 8 教程

MySQL 主页 MySQL 简介 MySQL 功能 MySQL 版本 MySQL 变量 MySQL 下载安装和环境配置 MySQL 管理 MySQL PHP 语法 MySQL Node.js 语法 MySQL Java 语法 MySQL Python 语法 MySQL 连接 MySQL Workbench

MySQL 8 数据库

MySQL 创建数据库 MySQL 删除数据库 MySQL 选择数据库 MySQL 显示数据库 MySQL 复制数据库 MySQL 数据库导出 MySQL 数据库导入 MySQL 数据库信息

MySQL 8 用户

MySQL 创建用户 MySQL 删除用户 MySQL 显示用户 MySQL 更改密码 MySQL 授予权限 MySQL 显示权限 MySQL 撤销权限 MySQL 锁定用户账户 MySQL 解锁用户账户

MySQL 8 表

MySQL 创建表 MySQL 显示表 MySQL 修改表 MySQL 重命名表 MySQL 克隆表 MySQL 截断表 MySQL 临时表 MySQL 修复表 MySQL 描述表 MySQL 添加/删除列 MySQL 显示列 MySQL 重命名列 MySQL 表锁定 MySQL 删除表 MySQL 派生表

MySQL 8 查询

MySQL 查询 MySQL 约束 MySQL INSERT 插入查询 MySQL SELECT 查询 MySQL UPDATE 更新查询 MySQL DELETE删除查询 MySQL REPLACE 替换查询 MySQL 忽略插入 MySQL 重复键更新时插入 MySQL 插入到另一个表语句

MySQL 8 视图

MySQL 创建视图 MySQL 更新视图 MySQL 删除视图 MySQL 重命名视图

MySQL 8 索引

MySQL 索引 MySQL 创建索引 MySQL 删除索引 MySQL 显示索引 MySQL 唯一索引 MySQL 聚集索引 MySQL 非聚集索引

MySQL 运算符和子句

MySQL Where 子句 MySQL Limit 子句 MySQL Distinct 子句 MySQL Order By 子句 MySQL Group By 子句 MySQL Having 子句 MySQL AND 运算符 MySQL OR 或运算符 MySQL LIKE 运算符 MySQL IN 运算符 MySQL ANY 运算符 MySQL Exists 运算符 MySQL NOT 运算符 MySQL NOT EQUAL 运算符 MySQL IS NULL 运算符 MySQL IS NOT NULL 运算符 MySQL Between 运算符 MySQL UNION 运算符 MySQL UNION 与 UNION ALL MySQL MINUS 运算符 MySQL INTERSECT 运算符 MySQL INTERVAL 运算符

MySQL 连接

MySQL 使用连接 MySQL Inner Join 内连接 MySQL LEFT JOIN 左连接 MySQL RIGHT JOIN 右连接 MySQL CROSS JOIN 交叉连接 MySQL 全连接 MySQL 自连接 MySQL Delete Join 删除连接 MySQL UPDATE JOIN 更新连接 MySQL 联合 vs 连接

MySQL 键

MySQL UNIQUE 唯一键 MySQL PRIMARY KEY 主键 MySQL FOREIGN KEY 外键 MySQL 复合键 MySQL 备用键

MySQL 触发器

MySQL 触发器 MySQL 创建触发器 MySQL 显示触发器 MySQL 删除触发器 MySQL 插入前触发器 MySQL 插入后触发器 MySQL 更新前触发器 MySQL 更新后触发器 MySQL 删除前触发器 MySQL 删除后触发器

MySQL 8 数据类型

MySQL 数据类型 MySQL VARCHAR MySQL BOOLEAN MySQL ENUM 枚举 MySQL DECIMAL 十进制 MySQL INT 整数 MySQL FLOAT 浮点数 MySQL BIT 位 MySQL TINYINT 微小整数 MySQL BLOB 二进制大对象 MySQL SET 集合

MySQL 正则表达式

MySQL 正则表达式 MySQL RLIKE 运算符 MySQL NOT LIKE 运算符 MySQL NOT REGEXP 运算符 MySQL regexp_instr() 函数 MySQL regexp_like() 函数 MySQL regexp_replace() 函数 MySQL regexp_substr() 函数

MySQL 全文搜索

MySQL 全文搜索 MySQL 自然语言全文搜索 MySQL 布尔全文搜索 MySQL 查询扩展全文搜索 MySQL ngram 全文解析器

MySQL8 函数和运算符

MySQL 日期和时间函数 MySQL 算术运算符 MySQL 数字函数 MySQL 字符串函数 MySQL 聚合函数

MySQL 8 其他概念

MySQL NULL 值 MySQL 事务 MySQL 序列 MySQL 处理重复项 MySQL SQL 注入 MySQL 子查询 MySQL 注释 MySQL 检查约束 MySQL 存储引擎 MySQL 将表导出为 CSV 文件 MySQL 将 CSV 文件导入数据库 MySQL UUID MySQL 通用表表达式 MySQL 级联删除 MySQL Upsert 操作 MySQL 水平分区 MySQL 垂直分区 MySQL 游标 MySQL 存储函数 MySQL SIGNAL 异常处理 MySQL RESIGNAL 异常处理 MySQL 字符集 MySQL 排序规则 MySQL 通配符 MySQL 别名 MySQL ROLLUP 超级聚合 MySQL 当前日期 MySQL 字面量 MySQL 存储过程 MySQL EXPLAIN 语句 MySQL JSON MySQL 标准差 MySQL 查找重复记录 MySQL 删除重复记录 MySQL 选择随机记录 MySQL 显示进程列表 MySQL 更改列类型 MySQL 重置自动增量 MySQL Coalesce() 函数

MySQL 8 实用资源

MySQL 实用函数 MySQL 语句参考 MySQL 快速指南 MySQL 实用资源 MySQL 讨论


MySQL - SHOW TABLES 显示表

MySQL Show Tables 显示表语句

在 MySQL 中,我们使用 SHOW TABLES 命令来检索特定数据库中存在的表的名称。此命令在各种情况下都很有用,例如:

  • 当我们想要查看数据库中现有表的名称以验证特定表是否存在时。

  • 当我们想要显示数据库中每个表的附加信息时,我们使用带有 MySQL FULL 修饰符的 SHOW TABLES 命令。

  • 此外,我们可以使用带有 WILDCARDS 的 SHOW TABLES 命令来过滤并仅显示与特定模式匹配的表。

语法

以下是 MySQL SHOW TABLES 命令的语法:

SHOW TABLES;

在继续示例之前,假设以下表存在于两个数据库中:testdb1 和 testdb2:

Databases testdb1 testdb2
Tables employee_remarks employee_age
employee_salary students_marks
students_attendance
students_fees
students_remarks

示例

首先,我们将数据库更改为 testdb1,并对其执行 SHOW TABLES 操作。

mysql> USE testdb1;
Database changed

现在,执行以下查询以列出 testdb1 数据库中的所有表。

SHOW TABLES;

输出

以下是 testdb1 数据库中存在的表 -

Tables_in_testdb1
employee_remarks
employee_salary
students_attendance
students_fees
students_remarks

使用 FULL 修饰符显示表

在 MySQL 中,我们使用可选的 FULL 修饰符以及 SHOW TABLES 命令来显示第二个输出列,该列包含有关数据库中现有表的附加信息,例如它们的类型:BASE TABLE 表示表,VIEW 表示视图,或 SYSTEM VIEW 表示 INFORMATION_SCHEMA 表。

示例

在下面的查询中,我们使用 FULL 修饰符以及 SHOW TABLES 命令来列出 testdb1 数据库中的表及其类型。

SHOW FULL TABLES;

输出

以下是上述查询的输出 -

Tables_in_testdb1 Table_type
employee_remarks BASE TABLE
employee_salary BASE TABLE
students_attendance BASE TABLE
students_fees BASE TABLE
students_remarks BASE TABLE

显示不同数据库中的表

在 MySQL 中,我们可以检索另一个数据库中存在的表列表。为此,我们需要将 IN 运算符或 FROM 子句与 SHOW TABLES 语句结合使用。

示例

在下面的查询中,我们将使用带有 IN 运算符的 SHOW TABLES 命令来获取另一个数据库 testdb2 中存在的表列表。

SHOW TABLES IN testdb2;

输出

以下是 testdb2 数据库中存在的表的名称 -

Tables_in_testdb2
employee_age
students_marks

示例

我们也可以使用带有 FROM 子句的 SHOW TABLES 命令执行上述操作。

SHOW TABLES FROM testdb2;

输出

我们可以观察到,两个输出是相同的。

Tables_in_testdb2
employee_age
students_marks

使用模式匹配显示表

在某些情况下,数据库中存在大量表,而我们只想检索特定表,我们会使用带有通配符(例如"%")的 LIKE 运算符。这些通配符将过滤并仅显示与特定模式匹配的表。

示例

在下面的查询中,我们将 LIKE 运算符与 SHOW TABLES 命令结合使用,以选择名称以"stud"开头的所有表(位于 testdb1 数据库中)。

SHOW TABLES IN testdb1 LIKE "stud%";

输出

以下是 testdb1 数据库中名称以"stud"开头的表 -

Tables_in_testdb1 (stud%)
students_attendance
students_fees
students_remarks

示例

这里,我们尝试从 testdb2 数据库中检索名称以"stud"开头的表 -

SHOW TABLES IN testdb2 LIKE "stud%";

输出

这将产生以下结果 -

Tables_in_testdb2 (stud%)
学生成绩

示例

我们使用 SHOW TABLES 和 WHERE 子句来检查 testdb1 数据库中是否存在名为"employee_remarks"的表 -

SHOW TABLES FROM testdb1 WHERE Tables_in_testdb1 = "employee_remarks";

输出

这将产生以下结果 -

Tables_in_testdb1
employee_remarks

使用客户端程序显示表

除了使用 MySQL 查询显示 MySQL 数据库中的表列表外,我们还可以使用客户端程序执行 SHOW TABLES 操作。

语法

以下是使用各种编程语言显示 MySQL 数据库中表列表的语法 -

要通过 PHP 程序显示 MySQL 数据库中的表列表,我们需要使用 mysqli 函数 query() 执行 SHOW TABLES 语句,如下所示:-

$sql = "SHOW TABLES FROM DATABASE";
$mysqli->query($sql);

要通过 Node.js 程序显示 MySQL 数据库中的表列表,我们需要使用 mysql2 库中的 query() 函数执行 SHOW TABLES 语句,如下所示:-

sql= "SHOW TABLES FROM DATABASE";
con.query(sql);

要通过 Java 程序显示 MySQL 数据库中的表列表,我们需要使用 JDBC 函数 executeUpdate() 执行 SHOW TABLES 语句,如下所示:-

String sql = "SHOW TABLES FROM DATABASE";
statement.executeQuery(sql);

要通过 Java 程序显示 MySQL 数据库中的表列表,我们需要使用 MySQL Connector/Python 的 execute() 函数执行 SHOW TABLES 语句,如下所示:-

show_table_query = "SHOW TABLES FROM DATABASE"
cursorObj.execute(show_table_query);

示例

以下是程序示例:-

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$dbname = 'TUTORIALS';
$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);

if ($mysqli->connect_errno) {
    printf("Connect failed: %s
", $mysqli->connect_error); exit(); } // printf('Connected successfully.
'); $sql = "SHOW TABLES FROM TUTORIALS"; if ($result = $mysqli->query($sql)) { printf("Show table executed successfully.
"); while ($row = mysqli_fetch_array($result)) { print_r($row); } } if ($mysqli->errno) { printf("Could not show table: %s
", $mysqli->error); } $mysqli->close();

输出

获得的输出如下 -

Show table executed successfully.
Array
(
    [0] => articles
    [Tables_in_tutorials] => articles
)
Array
(
    [0] => courses
    [Tables_in_tutorials] => courses
)
Array
(
    [0] => customers
    [Tables_in_tutorials] => customers
)
Array
(
    [0] => customerss
    [Tables_in_tutorials] => customerss
)
Array
(
    [0] => demo_table
    [Tables_in_tutorials] => demo_table
)
Array
(
    [0] => employee
    [Tables_in_tutorials] => employee
)
Array
(
    [0] => films
    [Tables_in_tutorials] => films
)
Array
(
    [0] => films_watched
    [Tables_in_tutorials] => films_watched
)
Array
(
    [0] => myplayers
    [Tables_in_tutorials] => myplayers
)
Array
(
    [0] => new_tutorial_tbl
    [Tables_in_tutorials] => new_tutorial_tbl
)
Array
(
    [0] => orders
    [Tables_in_tutorials] => orders
)
Array
(
    [0] => persons
    [Tables_in_tutorials] => persons
)
Array
(
    [0] => products
    [Tables_in_tutorials] => products
)
Array
(
    [0] => sales
    [Tables_in_tutorials] => sales
)
Array
(
    [0] => students
    [Tables_in_tutorials] => students
)        
var mysql = require('mysql2');
var con = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "Nr5a0204@123"
});
 //连接到 MySQL
  con.connect(function (err) {
  if (err) throw err;
  console.log("Connected!");
  console.log("--------------------------");
 sql = "CREATE DATABASE TUTORIALS;"
  con.query(sql);
 sql = "USE TUTORIALS;"
  con.query(sql);
 sql = "CREATE TABLE CUSTOMERS (ID INT NOT NULL, NAME VARCHAR(20) NOT NULL);"
  con.query(sql);
 sql = "CREATE TABLE ORDERS (OID INT NOT NULL, CUSTOMER_ID INT);"
  con.query(sql);
 sql = "SHOW TABLES;"
  con.query(sql, function(err, result){
    if (err) throw err
    console.log(result);
  });
});               

输出

生成的输出如下 -

Connected!
--------------------------
[
  { Tables_in_tutorials: 'customers' },
  { Tables_in_tutorials: 'orders' }
]         
import java.sql.*;
public class JDBCconnection {
public static void main(String[] args){
    String url = "jdbc:mysql://localhost:3306/TUTORIALS";
    String username = "root";
    String password = "password";
    try{
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection = DriverManager.getConnection(url, username, password);
        Statement statement = connection.createStatement();
        System.out.println("Connected successfully...!");
       //show table statement...!
        String sql = "SHOW TABLES FROM TUTORIALS";
        ResultSet resultSet = statement.executeQuery(sql);
        System.out.println("Tables in the current database: ");
        while(resultSet.next()) {
            System.out.print(resultSet.getString(1));
            System.out.println();
        }
        connection.close();
    }
    catch(Exception e){
        System.out.println(e);
    }
}
}                              

输出

获得的输出如下所示 -

Connected successfully...!
Tables in the current database: 
articles
blackpink
blog
courses
customer
customers
data   
import mysql.connector
#建立连接
connection = mysql.connector.connect(
    host='localhost',
    user='root',
    password='password',
    database='tut'
)
#创建游标对象
cursorObj = connection.cursor()
cursorObj.execute("SHOW TABLES")
tables = cursorObj.fetchall()
print("Tables in the database are:")
for table in tables:
    print(table[0])
cursorObj.close()
connection.close()                                         

输出

以下是上述代码的输出 -

Tables in the database are:
books
novel
novels
tutorials_tbl