- MySQL 基礎
- MySQL - 首頁
- MySQL - 簡介
- MySQL - 特性
- MySQL - 版本
- MySQL - 變數
- MySQL - 安裝
- MySQL - 管理
- MySQL - PHP 語法
- MySQL - Node.js 語法
- MySQL - Java 語法
- MySQL - Python 語法
- MySQL - 連線
- MySQL - Workbench
- MySQL 資料庫
- MySQL - 建立資料庫
- MySQL - 刪除資料庫
- MySQL - 選擇資料庫
- MySQL - 顯示資料庫
- MySQL - 複製資料庫
- MySQL - 資料庫匯出
- MySQL - 資料庫匯入
- MySQL - 資料庫資訊
- MySQL 使用者
- MySQL - 建立使用者
- MySQL - 刪除使用者
- MySQL - 顯示使用者
- MySQL - 修改密碼
- MySQL - 授予許可權
- MySQL - 顯示許可權
- MySQL - 收回許可權
- MySQL - 鎖定使用者帳戶
- MySQL - 解鎖使用者帳戶
- MySQL 表
- MySQL - 建立表
- MySQL - 顯示錶
- MySQL - 修改表
- MySQL - 重命名錶
- MySQL - 克隆表
- MySQL - 清空表
- MySQL - 臨時表
- MySQL - 修復表
- MySQL - 描述表
- MySQL - 新增/刪除列
- MySQL - 顯示列
- MySQL - 重新命名列
- MySQL - 表鎖
- MySQL - 刪除表
- MySQL - 派生表
- MySQL 查詢
- MySQL - 查詢
- MySQL - 約束
- MySQL - INSERT 查詢
- MySQL - SELECT 查詢
- MySQL - UPDATE 查詢
- MySQL - DELETE 查詢
- MySQL - REPLACE 查詢
- MySQL - INSERT IGNORE
- MySQL - INSERT ON DUPLICATE KEY UPDATE
- MySQL - INSERT INTO SELECT
- 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 - 不等於運算子
- MySQL - IS NULL 運算子
- MySQL - IS NOT NULL 運算子
- MySQL - BETWEEN 運算子
- MySQL - UNION 運算子
- MySQL - UNION vs UNION ALL
- MySQL - MINUS 運算子
- MySQL - INTERSECT 運算子
- MySQL - INTERVAL 運算子
- MySQL 連線
- MySQL - 使用連線
- MySQL - INNER JOIN
- MySQL - LEFT JOIN
- MySQL - RIGHT JOIN
- MySQL - CROSS JOIN
- MySQL - FULL JOIN
- MySQL - 自連線
- MySQL - DELETE JOIN
- MySQL - UPDATE JOIN
- MySQL - UNION vs JOIN
- MySQL 觸發器
- MySQL - 觸發器
- MySQL - 建立觸發器
- MySQL - 顯示觸發器
- MySQL - 刪除觸發器
- MySQL - BEFORE INSERT 觸發器
- MySQL - AFTER INSERT 觸發器
- MySQL - BEFORE UPDATE 觸發器
- MySQL - AFTER UPDATE 觸發器
- MySQL - BEFORE DELETE 觸發器
- MySQL - AFTER DELETE 觸發器
- MySQL 資料型別
- 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 - 聚合函式
- MySQL 其他概念
- MySQL - NULL 值
- MySQL - 事務
- MySQL - 使用序列
- MySQL - 處理重複項
- MySQL - SQL 注入
- MySQL - 子查詢
- MySQL - 註釋
- MySQL - CHECK 約束
- MySQL - 儲存引擎
- MySQL - 將表匯出到 CSV 檔案
- MySQL - 將 CSV 檔案匯入資料庫
- MySQL - UUID
- MySQL - 通用表表達式
- MySQL - ON DELETE CASCADE
- 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 - SHOW PROCESSLIST
- MySQL - 更改列型別
- MySQL - 重置自動遞增
- MySQL - COALESCE() 函式
- MySQL 有用資源
- MySQL - 有用函式
- MySQL - 語句參考
- MySQL - 快速指南
- MySQL - 有用資源
- MySQL - 討論
MySQL − 顯示錶
MySQL SHOW TABLES 語句
在 MySQL 中,我們使用SHOW TABLES命令來檢索特定資料庫中存在的表的名稱。此命令在各種情況下都非常有用,例如:
當我們想要檢視資料庫中存在的表名以驗證特定表是否存在時。
當我們想要顯示資料庫中每個表的附加資訊時,我們使用帶有 MySQL FULL 修飾符的 SHOW TABLES 命令。
此外,我們可以使用帶有萬用字元的 SHOW TABLES 命令來過濾並僅顯示與特定模式匹配的表。
語法
以下是 MySQL SHOW TABLES 命令的語法:
SHOW TABLES;
在繼續示例之前,假設以下表存在於兩個資料庫testdb1和testdb2中:
| 資料庫 | testdb1 | testdb2 |
|---|---|---|
| 表 | 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 修飾符的 SHOW TABLES
在 MySQL 中,我們使用可選的FULL修飾符以及 SHOW TABLES 命令來顯示第二個輸出列,其中包含有關資料庫中存在的表的附加資訊,例如它們的型別:表的BASE TABLE、檢視的VIEW或 INFORMATION_SCHEMA 表的SYSTEM VIEW。
示例
在下面的查詢中,我們使用 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 |
在不同的資料庫中使用 SHOW TABLES
在 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 |
使用模式匹配的 SHOW TABLES
在某些情況下,資料庫中存在大量表,而我們只想檢索特定表,我們可以使用帶有萬用字元(例如“%”)的 LIKE 運算子。這些萬用字元將過濾並僅顯示與特定模式匹配的表。
示例
在下面的查詢中,我們使用帶有 SHOW TABLES 命令的 LIKE 運算子來選擇 testdb1 資料庫中名稱以“stud”開頭的所有表。
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%) |
|---|
| students_marks |
示例
我們使用 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"
});
//Connecting to 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://: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
#establishing the connection
connection = mysql.connector.connect(
host='localhost',
user='root',
password='password',
database='tut'
)
#Creating a cursor object
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