Contents

Database - 在 MariaDB 中使用 SQL 語言

透過 SQL 語言可以在 MariaDB 做 CRUD 創建 (INSERT)、查詢 (SELECT)、更新 (UPDATE) 和刪除 (DELETE) 等資料操作。

練習環境
可以使用 HowHow 的創建 Lab 練習環境,來實作 MariaDB 安裝及操作,使用方式請參閱 HowHow 網站 - Linux 練習 Lab 第二版

1. MariaDB 簡介

MariaDB 是一個開源的關聯式資料庫管理系統,它是 MySQL 的分支版本,提供了更多的功能和性能優化。它支援多種作業系統,包括 Linux、Windows 和 macOS 等,並且能夠與許多不同的程式語言進行互動,例如 PHP、Java 和 Python 等。它還支援多種儲存引擎,包括 InnoDB、MyISAM、Memory 等,使用者可以根據需要進行選擇。 MariaDB 還提供了許多高級功能,例如支援 JSON、GIS 和虛擬列等,這些功能能夠讓使用者更加方便地進行資料管理和查詢。

2. 存取 MariaDB 資料庫

mariadb 套件提供 mysql 指令,能透對 MariaDB 或 MySQL 資料庫進行互動式或是非互動式存取,其查詢的結果會以 ASCII Table 格式來顯示。

1
mysql -u root -h localhost  -p
  1. -u : 用來指定帳號的選項。

  2. -h : 用來指定的主機選項,預設為 localhost。

  3. -p : 用來輸入帳號密碼的選項。

  4. 連接本地資料庫

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
[root@servera ~]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 18
Server version: 10.3.35-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

3. 資料庫操作

3.1. 顯示所有資料庫

1
2
3
4
5
6
7
8
9
MariaDB [(none)]> SHOW databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.001 sec)
SQL 大小寫
SQL 語法不區分大小寫,通常大寫會表示其為關鍵字。

3.2. 使用資料庫

1
2
3
4
5
MariaDB [(none)]> 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

3.3 創建資料庫

創建 inventory 並使用其資料庫。

1
2
3
4
5
MariaDB [mysql]> CREATE DATABASE inventory;
Query OK, 1 row affected (0.001 sec)

MariaDB [mysql]> use inventory;
Database changed

3.4. 刪除資料庫

1
2
MariaDB [inventory]> DROP DATABASE inventory;
Query OK, 1 row affected (0.188 sec)
警告
使用 DROP DATABASE 時要十分小心,會刪除指定的整個資料庫及資料表。

4. 創建資料表

SQL 常用的操作 -> CRUD ,CRUD 表示創建 (INSERT)、查詢 (SELECT)、更新 (UPDATE) 和刪除 (DELETE)。

4.1. 創建 product 資料表

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
MariaDB [inventory]> CREATE TABLE product
    -> ( id INT(11) NOT NULL AUTO_INCREMENT,
    -> name VARCHAR(100) NOT NULL,
    -> price DOUBLE NOT NULL,
    -> stock INT(11) NOT NULL,
    -> id_category INT(11) NOT NULL,
    -> id_manufacturer INT(11) NOT NULL,
    -> CONSTRAINT id_pk PRIMARY KEY (id)
    -> );
Query OK, 0 rows affected, 1 warning (0.173 sec)

4.2. 顯示資料表

1
2
3
4
5
6
7
MariaDB [inventory]> SHOW tables;
+---------------------+
| Tables_in_inventory |
+---------------------+
| product             |
+---------------------+
1 row in set (0.001 sec)

4.3. 顯示資料表詳情

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
MariaDB [inventory]> DESCRIBE product;
+-----------------+--------------+------+-----+---------+----------------+
| Field           | Type         | Null | Key | Default | Extra          |
+-----------------+--------------+------+-----+---------+----------------+
| id              | int(11)      | NO   | PRI | NULL    | auto_increment |
| name            | varchar(100) | NO   |     | NULL    |                |
| price           | double       | NO   |     | NULL    |                |
| stock           | int(11)      | NO   |     | NULL    |                |
| id_category     | int(11)      | NO   |     | NULL    |                |
| id_manufacturer | int(11)      | NO   |     | NULL    |                |
+-----------------+--------------+------+-----+---------+----------------+
6 rows in set (0.002 sec)

4.4. 插入資料

1
2
3
4
MariaDB [inventory]> INSERT INTO
    -> product (name,price,stock,id_category,id_manufacturer)
    -> VALUES ('RHEL8',100,86,1,1);
Query OK, 1 row affected (0.046 sec)

4.5. 顯示資料

1
2
3
4
5
6
7
MariaDB [inventory]> SELECT * FROM product ;
+----+-------+-------+-------+-------------+-----------------+
| id | name  | price | stock | id_category | id_manufacturer |
+----+-------+-------+-------+-------------+-----------------+
|  1 | RHEL8 |   100 |    86 |           1 |               1 |
+----+-------+-------+-------+-------------+-----------------+
1 row in set (0.001 sec)

4.5.1 選擇指定的欄位

1
2
3
4
5
6
7
MariaDB [inventory]> SELECT name,price FROM product;
+-------+-------+
| name  | price |
+-------+-------+
| RHEL8 |   100 |
+-------+-------+
1 row in set (0.001 sec)

4.5.2 指定條件查詢

1
2
MariaDB [inventory]> SELECT * FROM product WHERE price > 1000;
Empty set (0.001 sec)
常見的運算式
運算式 說明
= 等於
< > 不等於( 有些版本為 !=)
> 大於
< 小於
>= 大於等於
<= 小於等於
BETWEEN 介於之間
LIKE 模糊查詢
IN 一個或多個值

4.6. 更新資料

更新品名為 RHEL9 價格為 2000。

1
2
3
4
5
MariaDB [inventory]> UPDATE product
    -> SET price=2000, name='RHEL9'
    -> WHERE id = 1;
Query OK, 1 row affected (0.112 sec)
Rows matched: 1  Changed: 1  Warnings: 0

顯示資料。

1
2
3
4
5
6
7
MariaDB [inventory]> SELECT * FROM product ;
+----+-------+-------+-------+-------------+-----------------+
| id | name  | price | stock | id_category | id_manufacturer |
+----+-------+-------+-------+-------------+-----------------+
|  1 | RHEL9 |  2000 |    86 |           1 |               1 |
+----+-------+-------+-------+-------------+-----------------+
1 row in set (0.001 sec)

4.7. 刪除資料

1
2
3
4
5
MariaDB [inventory]> DELETE FROM product WHERE id = 1;
Query OK, 1 row affected (0.030 sec)

MariaDB [inventory]> SELECT * FROM product ;
Empty set (0.001 sec)

4. 小結

SQL 除了基礎的 CURD 之外還有更多進階的操作 : 子查詢、 Join、 Group by、 Aggregate、 Stored Procedure、 View、 Transaction、Window Function 與 Full Text Search 等。



如果你還沒有註冊 Like Coin,你可以在文章最下方看到 Like 的按鈕,點下去後即可申請帳號,透過申請帳號後可以幫我的文章按下 Like,而 Like 最多可以點五次,而你不用付出任何一塊錢,就能給我寫這篇文章的最大的回饋!