iT邦幫忙

0

Cyber security -4 Module4 關聯資料庫 SQL

  • 分享至 

  • xImage
  •  

SQL(Structured Query Language)

  • Query:A request for data from a database table or a combination of tables is called a query
  • SQL is a programming language used to create, interact with, and
  • request information from a database.
  • SQL's filtering can be used to find data to support security-related decisions.
分類 Linux SQL
Purpose 管理文件和目錄 filters data in the context of files and directories on a computer system 查詢和操作數據庫中的數據 filter data within a database management system.
Syntax 使用特定命令,如find, sed, grep 使用標準語法,如WHERE, SELECT, JOIN
Structure 構造較少,數據為文本行 結構化程度高,數據組織清晰
Joining Tables 不支持多表連接 允許將多個表連接以返回數據
Best Uses 適用於文本格式的日誌過濾 適用於數據庫格式的數據查詢

Relational databases

  • contain tables that are related to each other through primary and foreign keys.
  • A relational database is a structured database containing tables that are related to each other through primary and foreign keys.

SQLite 是什麼?

  • SQLite 是一個輕量級的關聯式資料庫管理系統(RDBMS)。
  • 它是一個軟體庫(library),可以嵌入到應用程式中,無需獨立伺服器,資料會以單一檔案儲存在本地端。
  • 它支援 SQL 語言來操作資料庫。
  • 查詢是對一個資料庫表格或多個表格的數據請求。
  • SQL 是一種語言,SQLite 則是一個資料庫系統。
    • SQLite 使用 SQL 作為查詢和操作資料的語言。
    • 就像「中文」是一種語言,而「Line聊天」是一個應用程式,Line 支援用中文溝通一樣;SQLite 是一個支援 SQL 語言的資料庫。

主鍵(Primary Key)與外鍵(Foreign Key)的關聯

  1. 主鍵(Primary Key)
  • 定義:主鍵是表格中用來唯一識別每一筆資料(每一列)的欄位。
  • 特點:
    • 每個值都唯一(不重複)。
    • 不允許空值(NULL)。
    • 每個表只能有一個主鍵。
    • 範例:在 employees 表中,employee_id 是主鍵,每位員工都有一個獨一無二的 employee_id。
  1. 外鍵(Foreign Key)
  • 定義:外鍵是表格中的一個欄位,它的值對應到另一個表的主鍵,用來建立兩張表之間的關聯。
  • 特點:
    • 可以有重複值或空值。
    • 一個表可以有多個外鍵。
    • 範例:在 machines 表中,employee_id 是外鍵,它參照 employees 表的主鍵 employee_id,用來表示這台機器屬於哪位員工。
關鍵字 / Syntax 用法說明 範例 SQL 語句 說明
SELECT 指定要查詢(返回)的欄位 SELECT customerid FROM customers; 查詢 customers 表的 customerid 欄位
SELECT * 查詢所有欄位 SELECT * FROM customers; 查詢 customers 表的所有欄位
SELECT 多個欄位 查詢多個欄位,欄位間以逗號分隔 SELECT customerid, city FROM customers; 查詢 customerid 與 city 兩欄位
FROM 指定要查詢的資料表 SELECT * FROM employees; 查詢 employees 表
ORDER BY 依據指定欄位排序(預設為升序 ASC) SELECT city FROM customers ORDER BY city; 按 city 欄位升序排序
ORDER BY DESC 依據指定欄位降序排序 SELECT city FROM customers ORDER BY city DESC; 按 city 欄位降序排序
ORDER BY 多個欄位 依據多個欄位排序,先依第一欄,若相同再依第二欄 SELECT country, city FROM customers ORDER BY country, city; 先依 country,再依 city 排序

圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言