SQL 十四分鐘速成班!沒錯不要懷疑,資料庫語法比中午決定吃什麼還要簡單!

PAPAYA 電腦教室
28 Sept 202413:36

Summary

TLDRThis video provides an introduction to SQL, a crucial language for interacting with databases. Using the Sqliteviz website for hands-on practice, viewers learn to query, update, and manage data without software installation. Key SQL concepts such as SELECT, WHERE, JOIN, and functions like AVG, SUM, and COUNT are covered, along with tips on improving code readability and performance. The tutorial also includes creating tables, inserting data, and performing cross-table queries, making it a comprehensive guide for beginners looking to enhance their data management skills.

Takeaways

  • 💡 SQL is a critical language for interacting with databases, allowing users to query, update, and manage data efficiently.
  • 🌐 SQLiteViz is a web-based platform where beginners can practice SQL without needing to install any software.
  • 📋 You can load data into SQLiteViz by dragging a file into the interface, and it automatically creates a table based on the file's content.
  • 🔍 The SELECT statement is used to retrieve data, where '*' selects all columns, or you can specify columns by name.
  • 📊 The LIMIT and OFFSET clauses are useful for controlling the number of rows returned and for pagination, especially when dealing with large datasets.
  • 🔎 The WHERE clause is essential for filtering data based on conditions, and ORDER BY is used for sorting the results.
  • ✨ Wildcard characters like '%' and '_' can be used with the LIKE keyword for flexible text matching in SQL queries.
  • 🧮 SQL functions such as AVG, SUM, MAX, MIN, and COUNT help in aggregating data, and aliasing columns improves readability.
  • 📊 GROUP BY and HAVING allow users to group data and filter aggregated results, respectively, providing more structured data analysis.
  • 🔗 SQL JOINs, such as LEFT JOIN and INNER JOIN, enable combining data from multiple tables, making cross-table queries possible.

Q & A

  • What is SQL used for?

    -SQL is used for interacting with databases, including querying, adding, or updating data. It is a key skill for anyone involved in data analysis and decision-making.

  • What are some popular database management systems that use SQL?

    -Some popular database management systems that use SQL include MySQL, PostgreSQL, Microsoft SQL Server, and Oracle.

  • What is Sqliteviz, and why is it useful for beginners?

    -Sqliteviz is an online tool that allows users to practice SQL directly in a browser without installing any software. It's intuitive and beginner-friendly, making it easy to learn SQL.

  • What does the SELECT statement do in SQL?

    -The SELECT statement in SQL is used to retrieve data from a database. It can return all columns (using *) or specific ones from a table.

  • What is the purpose of the LIMIT and OFFSET clauses in SQL?

    -The LIMIT clause restricts the number of results returned by a query, while OFFSET allows you to skip a certain number of rows, enabling pagination of large datasets.

  • How do you filter results in SQL using the WHERE clause?

    -The WHERE clause is used to set conditions for filtering the results, such as showing only data that meets specific criteria, like rows with a specific value in a column.

  • How does the ORDER BY clause function in SQL?

    -The ORDER BY clause is used to sort the query results based on one or more columns, either in ascending (ASC) or descending (DESC) order.

  • What are SQL aggregate functions, and can you give examples?

    -SQL aggregate functions perform calculations on multiple rows of data. Examples include AVG (average), SUM (sum), MAX (maximum), MIN (minimum), and COUNT (row count).

  • What is the difference between GROUP BY and HAVING clauses in SQL?

    -GROUP BY groups rows that have the same values in specified columns, while HAVING is used to filter groups based on a condition after aggregation.

  • How can you join tables in SQL, and what is the difference between LEFT JOIN and INNER JOIN?

    -SQL joins are used to combine rows from two or more tables based on a related column. LEFT JOIN returns all records from the left table and matching records from the right table, while INNER JOIN only returns records that have matching values in both tables.

Outlines

00:00

💻 Introduction to SQL and Sqliteviz

This section introduces SQL as a crucial programming language used for interacting with databases, such as querying, adding, and updating data. The speaker focuses on Sqliteviz, a website that allows users to practice SQL directly in a browser, making it accessible for beginners. A sample student data file is provided for the tutorial, and the speaker demonstrates how to import it into Sqliteviz. The section also explains the syntax of basic SQL commands like SELECT, including how to fetch all or specific columns from a table, and the importance of using comments and semicolons for clarity and code completeness.

05:00

🔍 Filtering Data with WHERE and LIMIT Clauses

This paragraph expands on how SQL can filter data using the WHERE clause. Examples include filtering by specific classes, sorting results using ORDER BY, and limiting the number of rows returned using the LIMIT clause. The OFFSET clause is also introduced to paginate results, allowing for more control when working with large datasets. The speaker emphasizes best practices for writing readable SQL code, such as separating clauses onto different lines.

10:01

🔢 Using Logical Operators and Pattern Matching in SQL

Here, the speaker demonstrates how SQL can use logical operators like AND and OR to perform more complex filtering, such as selecting students within specific score ranges. Additionally, pattern matching with LIKE and wildcard characters is explained, allowing for flexible text-based queries. The speaker also highlights the importance of parentheses to control operator precedence and introduces the IN keyword for simplifying OR conditions. The section concludes by discussing functions like AVG, SUM, MAX, and COUNT for data aggregation.

📊 GROUP BY and HAVING for Aggregating Data

This section focuses on aggregating data using GROUP BY to calculate averages or sums for different categories (e.g., class averages). The HAVING clause is introduced to filter grouped data, and the speaker emphasizes the importance of correct clause order in SQL statements to avoid errors. Examples include filtering classes with an average score above a certain threshold and applying ROUND to round numbers to a specific decimal place for better readability.

🛠 Creating and Modifying Tables in SQL

The speaker explains how to create new tables in SQL with the CREATE TABLE command, defining columns and specifying data types such as INT and VARCHAR. The importance of setting a primary key to ensure unique and non-null values is emphasized. They demonstrate inserting data into the table with the INSERT INTO command, updating records with the UPDATE command, and safely deleting records or entire tables using DELETE and DROP TABLE. The speaker highlights common pitfalls, such as forgetting WHERE clauses when updating or deleting, which could result in unintended changes.

🔗 Joining Tables for Complex Queries

The final section introduces SQL table joins, focusing on LEFT JOIN to combine data from two tables (students and clubs). It explains how to specify the relationship between tables using shared columns (e.g., club IDs) and describes how LEFT JOIN will return all rows from the left table, even if there’s no match in the right table. INNER JOIN is also discussed for cases where only matched rows are needed. The section concludes by encouraging further practice with JOINs and summarizing the SQL concepts covered in the video.

Mindmap

Keywords

💡SQL

SQL, or Structured Query Language, is the programming language used to interact with databases, allowing users to perform tasks such as querying, inserting, or updating data. In the video, SQL is presented as a core skill for data analysis and decision-making, with practical examples shown using a student database. The video emphasizes how SQL is used for retrieving specific information from databases.

💡SQLiteviz

SQLiteviz is a web-based tool that allows users to practice SQL queries directly in their browser without needing to install software. The video demonstrates how this tool is particularly intuitive and user-friendly for beginners, allowing them to easily practice SQL queries on a sample student dataset by uploading a file.

💡SELECT statement

The SELECT statement is a fundamental SQL command used to retrieve data from a database. In the video, it is illustrated as a query that selects all columns from the 'students' table by using the asterisk (*) symbol. The example showcases how SELECT works to return data from specific fields such as student names, classes, and grades.

💡WHERE clause

The WHERE clause is an SQL keyword used to filter results based on specific conditions. The video explains how this clause is used to limit the data retrieved, such as filtering students by class or grade. It is also highlighted that WHERE can be combined with other operators, such as ‘AND’ or ‘OR’, to refine searches further.

💡ORDER BY

ORDER BY is an SQL clause used to sort query results by one or more columns in ascending or descending order. The video illustrates this by showing how student records can be sorted by class or exam scores, either in ascending (lowest to highest) or descending (highest to lowest) order.

💡JOIN

JOIN is a keyword used in SQL to combine rows from two or more tables based on a related column. In the video, the LEFT JOIN is demonstrated by linking student data with their corresponding club names from another table. The concept of INNER JOIN is also introduced, where only records with matches in both tables are returned.

💡GROUP BY

GROUP BY is an SQL clause used to group rows that have the same values in specified columns, often used with aggregate functions like COUNT, AVG, or SUM. In the video, it is used to show how to calculate the average score of students within each class, grouping the data by the 'class' column.

💡HAVING clause

The HAVING clause is similar to WHERE, but it is used to filter the results after the data has been grouped using the GROUP BY clause. In the video, it is shown to filter out groups (e.g., classes) where the average score is less than 80, demonstrating its role in refining grouped results.

💡LIMIT and OFFSET

LIMIT is used to restrict the number of rows returned by a query, while OFFSET skips a specified number of rows before starting to return rows. In the video, these clauses are demonstrated when working with larger datasets to avoid retrieving all data at once, which can be inefficient. For example, LIMIT 5 returns only five records, while OFFSET allows skipping the first few records.

💡INSERT INTO

INSERT INTO is the SQL statement used to add new data into a table. The video shows how to use this command to insert club information into a newly created 'clubs' table. The importance of ensuring that the data matches the columns of the table is emphasized, along with the role of VALUES to input actual data.

Highlights

Introduction to SQL as a language for interacting with databases, essential for data analysis and decision-making.

Common databases where SQL is used include MySQL, PostgreSQL, Microsoft SQL Server, and Oracle.

Demonstration using the Sqliteviz website to practice SQL without installing software, ideal for beginners.

Explanation of basic SQL syntax, such as SELECT, FROM, and how to retrieve all columns from a table.

Clarification on using or omitting double quotes around table names depending on the database system.

Introduction to using LIMIT and OFFSET in SQL queries to control the number of returned rows.

Overview of WHERE clause for filtering data based on specific conditions, such as class or exam scores.

Explanation of ORDER BY for sorting data, including ascending and descending order for columns like scores.

Introduction to wildcard characters like % and _ for flexible text matching using the LIKE keyword.

Explanation of AND, OR, and parentheses to control logical precedence in complex queries.

Introduction to aggregate functions such as AVG, SUM, MAX, MIN, and COUNT for summarizing data.

Overview of GROUP BY for calculating grouped aggregates, and HAVING for filtering grouped data.

Using DISTINCT to retrieve unique values from a table, and handling NULL values with WHERE.

Creating and managing new tables using CREATE TABLE, specifying data types and primary keys.

Performing joins between tables, such as using LEFT JOIN and INNER JOIN to combine related data from different tables.

Transcripts

play00:03

嗨,大家好

play00:04

今天我們要來聊聊 SQL

play00:06

它是一個專門用來和資料庫互動的程式語言

play00:10

像是從資料庫中查詢、新增或更新資料等

play00:14

對於常需要做資料分析和決策的人士來說

play00:17

是一個非常重要的核心技能

play00:19

SQL 主要是運行在所謂的「資料庫管理系統」

play00:23

像是 MySQL

play00:24

PostgreSQL

play00:26

Microsoft SQL Server 和 Oracle 等

play00:29

不過今天的教學

play00:30

我們主要是使用 Sqliteviz 這個網站

play00:34

它能讓用戶直接在瀏覽器內練習 SQL 的語法

play00:37

不需要安裝任何軟體

play00:38

對於初學者來說非常地直觀且容易上手

play00:42

同時配合今天的練習

play00:44

我在影片下方的說明欄有提供了一個表格檔案

play00:47

裡面有一些學生的資料

play00:49

像是姓名、班級、考試成績和參加的社團編號

play00:54

我把這個檔案拖曳到 Sqliteviz 的頁面

play00:57

它會以該檔案自動建立一個同名的表格

play01:01

然後我到右下角點擊「匯入」

play01:03

按下「完成」

play01:05

就會來到 Sqliteviz 的主要操作介面

play01:08

那在上面的窗格

play01:09

它有預先載入了一些 SQL 語法

play01:12

其中由斜線和星號包覆的灰色文字是所謂的「註解」

play01:16

主要是用來說明和解釋 SQL 的程式碼

play01:19

而如果你的註解只有一行的話

play01:21

你也可以直接在開頭加上兩條橫線 (--) 就 OK 了

play01:25

那接下來的這一行是一句標準的 SQL 語法

play01:29

其中 SELECT 是「選取」的意思

play01:31

星號 (*) 代表的是表格內所有的欄位

play01:34

然後後面這兩個字

play01:35

是指我們要從 students 這個表格來檢索數據

play01:39

那你會發現在這行範例語法中

play01:42

表格名稱有使用「雙引號」來做標注

play01:44

不過是否要用雙引號

play01:46

其實是取決於你使用的是哪一種資料庫軟體

play01:50

而在這裡雙引號是可以省略的

play01:52

不會影響到 SQL 程式碼的執行

play01:55

最後我在 SQL 語法的結尾加上一個分號 (;)

play01:58

雖然有些資料庫軟體允許省略末尾的分號

play02:01

但一般來說

play02:02

使用分號來表示語句的結束是一個良好的編寫習慣

play02:07

好,我們來試試看執行這一行 SQL 語法

play02:10

你可以到右側工具列點擊「執行」按鈕

play02:13

或是按下快捷鍵 Ctrl +Enter

play02:15

如此在底部視窗就會顯示表格內的所有資料了

play02:19

當然平常我們在做資料檢索時

play02:21

可能不需要系統回傳表格中的所有欄位

play02:25

遇見這種情形時

play02:26

你可以把語法中的星號刪除掉

play02:29

改輸入你想要檢視的欄位

play02:31

譬如姓名、班級和成績

play02:33

按下 Ctrl +Enter

play02:35

這樣 SQL 就只會回傳這三個欄位的資料囉

play02:39

OK,目前這個表格的資料筆數並不多

play02:42

總共只有 18 筆

play02:43

但在檢索龐大的數據庫

play02:45

像是數十萬甚至上百萬筆時

play02:48

如果每次都要回傳大量資料會影響到系統的效能

play02:52

這時你不妨可以使用 LIMIT 語法

play02:54

來限制查詢結果的筆數

play02:56

例如我輸入 5

play02:58

按下 Ctrl +Enter

play03:00

而檢索完畢這些資料後

play03:02

如果你要繼續看接下來的五筆資料

play03:04

你可以使用 OFFSET 語法

play03:06

讓 SQL 跳過前五筆

play03:08

直接顯示 6 到 10 筆的資料

play03:11

同理如果想要看第 11 到第 15 筆的資料

play03:14

只要把 OFFSET 設為 10 就 OK 了

play03:17

那這些語法雖然可以寫在同一行

play03:19

但為了提高程式碼的可讀性

play03:21

通常會建議將每個關鍵字做分行

play03:24

這樣不但能讓語法結構一目了然

play03:27

之後在維護程式碼時也會變得更為簡單方便喔

play03:31

好,接著我們要來看的 SQL 語法是 WHERE

play03:34

它可以用來設定查詢時的「篩選」條件

play03:37

譬如我們可以讓系統在顯示成績時

play03:40

只顯示「1 年 2 班」的成績

play03:42

按下 Ctrl +Enter

play03:44

而如果我們把中間的「等於」改成 「不等於」的話

play03:47

SQL 就會回傳 1 年 2 班「以外」的班級

play03:50

以這個表格來說就是 1 年 1 班和 1 年 3 班

play03:54

那目前這兩個班級並沒有把同班的同學擺在一起

play03:58

對此你可以在語法最末行輸入 ORDER BY

play04:01

它可以針對一個或多個欄位的值進行排序

play04:05

如此表格內就會先顯示 1 年 1 班

play04:07

接著顯示 1 年 3 班的同學

play04:10

那我們還可以納入其它的欄位來做排序

play04:13

譬如同學們的考試成績

play04:15

按下 Ctrl +Enter

play04:17

這樣在同一個班級內

play04:19

成績就會由最低分排到最高分

play04:22

而如果你希望把分數的排序改成由高排到低的話

play04:25

你只要在語法的最後面輸入 DESC (降序) 就 OK 囉

play04:30

好,假設今天我想要查詢的對象不是整個班級

play04:33

而是某位同學的成績

play04:35

那麼我們可以把 WHERE 的篩選條件改成該同學的姓名

play04:39

如果你不確定同學全名的話也沒有關係

play04:42

因為 SQL 支援萬用字元的輸入

play04:45

譬如百分比符號 (%) 代表的是「零個或多個」任意字元

play04:49

前面的等號要改成 LIKE (符合)

play04:51

這樣系統就會把所有姓「張」的同學列出來了

play04:55

另外一個常用的萬用字元是「底線」

play04:57

它代表的是「一個」字元

play05:00

因此用這個條件下去做檢索的話

play05:02

SQL 就只會回傳名字只有兩個字的「張靜」同學囉

play05:07

那 WHERE 語法除了能進行文字比對之外

play05:09

它也能做數字大小的判斷

play05:12

譬如我們可以列出所有成績在 80 分以上的同學

play05:16

也可以配合 AND (且) 語法來查詢一個分數區間

play05:19

例如 80 到 90 分

play05:21

那這一行「成績大於等於 80」

play05:23

且「成績小於 90」有個比較精簡的寫法

play05:26

就是改用 BETWEEN 的關鍵字

play05:28

來選取介於兩值之間的資料

play05:31

按下 Ctrl +Enter 的話會得到相同的結果

play05:34

那我們可以用 AND 來加入更多的篩選條件

play05:38

例如同學就讀的「班級」

play05:40

這樣就只會列出「1 年 1 班」成績介於 80 到 90 分之間的同學

play05:45

那假設我也想要讓系統列出「1 年 2 班」符合條件的同學

play05:49

於是我在後面接續輸入 OR (或)

play05:52

代表班級可以是「1 年 1 班」或著是「1 年 2 班」

play05:56

不過你會發現此時檢索的結果有些問題

play05:59

1 年 2 班中有些同學的分數不在 80 到 90 分之間

play06:02

卻也被列了出來

play06:04

這是因為在 SQL 中 AND 語法的優先層級比 OR 來得高

play06:09

因此 SQL 會先處理這個條件

play06:11

來找出 1 年 1 班分數介於 80 到 90 分的同學

play06:15

接著再處理 OR 後面的條件

play06:17

如此就導致 1 年 2 班的同學

play06:19

無論成績好壞都會被篩選出來

play06:22

而要解決這個問題

play06:24

你可以使用「圓括號」來調整邏輯的順序

play06:27

譬如讓 SQL 先處理後面的 OR

play06:29

再處理前面的 AND

play06:31

這樣系統就能列出正確的查詢結果了

play06:34

同樣地

play06:35

這段語法也有一個比較精簡的表達方式

play06:37

就是改用 IN 關鍵字來簡化多個 OR 條件的使用

play06:41

這樣能讓查詢變得更容易閱讀和管理喔

play06:45

好,SQL 跟 Excel 有個很類似的地方

play06:48

就是它們都能用一些函數針對多筆資料進行匯總和計算

play06:53

像是我如果把 AVG (Average 平均) 函數

play06:55

套用在「成績」的欄位

play06:56

就能查出此次測驗全部同學的平均分數

play07:00

除了 AVG 之外

play07:01

其它常見的函數還包含了 SUM 加總

play07:04

MAX 求最大值

play07:06

MIN 求最小值和 COUNT 計算數量

play07:10

那在使用這些函數時

play07:11

如果你覺得欄位名稱看起來不夠直觀

play07:14

你可以為這些欄位設定所謂的「別名」

play07:17

譬如我把 AVG 改命名為「成績平均」

play07:20

Max 命名為「最高分」等

play07:22

這樣查詢結果的可讀性就會比較高了

play07:26

好,目前成績平均的分數包含了好幾位小數

play07:29

如果你想要把數值做個四捨五入的話

play07:32

你可以在第一行加入 ROUND 語法

play07:35

ROUND 語法能讓你指定要保留的小數位數

play07:38

例如保留一位小數

play07:40

而如果要四捨五入到整數位的話

play07:43

你只要省略後面的數字就 OK 了

play07:46

那這些匯總函數經常和 GROUP BY (分組) 的語法配合使用

play07:51

例如你想要知道的不是全班同學的成績平均

play07:53

而是「各班」的平均

play07:55

那麼你就可以在最後一行輸入 GROUP BY 班級

play07:59

有需要的話

play08:00

你還可以應用剛才學到的 ORDER BY 語法

play08:02

把成績列表改成由最高分排到最低分

play08:06

那先前我們有提到

play08:07

使用 WHERE 語法可以為資料進行篩選

play08:10

不過對於經過 GROUP BY 分組後的資料

play08:13

要做篩選的話必須改用 HAVING 這個關鍵字

play08:16

例如列出平均分數大於等於 80 分的班級

play08:20

而此時你會發現 SQL 跑出了一則錯誤訊息

play08:24

這是因為我們在撰寫 SQL 語法時

play08:26

關鍵字之間有一個特定的順序

play08:29

這個順序會影響到 SQL 語法是否能正確地執行

play08:33

譬如 ORDER BY 要排在 LIMIT 之前

play08:36

因為資料必須完成排序後才能限制回傳的筆數

play08:40

而 GROUP BY 必須排在 HAVING 之前

play08:42

因為 HAVING 是用來篩選分組的結果

play08:45

於是這裡我把 HAVING 反白後執行剪下

play08:49

貼到 GROUP BY 的後面

play08:51

這樣再去執行 SQL 語法時就能顯示正確的查詢結果囉

play08:55

OK,除了使用 AVG 來求平均值

play08:58

COUNT (計數) 也是一個很常用的函數

play09:01

譬如我如果在 COUNT 裡面輸入一個星號 (*)

play09:04

SQL 會去計算整個表格的總行數

play09:07

而你也可以在圓括號內輸入欄位的名稱

play09:10

例如「社團」

play09:12

那你會發現「社團」的行數跟表格的總行數

play09:14

數字並不一樣

play09:16

這是因為 COUNT 在針對單一欄位進行計數時

play09:19

會自動忽略「空白」的儲存格

play09:21

因此總行數 18 在減去兩個空格之後

play09:24

就會得到 16 這個數值了

play09:27

此外 COUNT 函數

play09:28

也常常會配合 DISTINCT 這個關鍵字來進行檢索

play09:32

DISTINCT 可用來排除表格中重複的資料

play09:35

執行後結果會顯示為 4

play09:37

代表這間學校總共有四個不同的社團

play09:41

而如果你想要知道是哪四個的話

play09:43

你可以把 COUNT 函數拿掉

play09:45

僅保留 DISTINCT

play09:47

那要注意 DISTINCT 不會自動排除 NULL (空值)

play09:50

如果想要把空值隱藏起來的話

play09:52

只要另外加上一行 WHERE 的語法

play09:55

指定社團欄位中不要有 NULL 就 OK 囉

play09:58

好,這個表格目前只有登記社團的編號

play10:01

但是沒有社團的名稱

play10:03

所以最後我們來練習一下用 SQL 建立一個表格

play10:06

來補齊社團相關的資訊

play10:08

那建立表格的語法是 CREATE TABLE

play10:11

後面接續表格的名稱

play10:13

例如 clubs (社團)

play10:15

接著再輸入一組圓括號

play10:17

在圓括號內我們要指定這個表格有哪些欄位

play10:21

例如第一欄是「社團編號」

play10:23

第二欄是「社團名稱」

play10:25

除此之外

play10:26

我們還要指定欄位儲存的資料是什麼類型

play10:29

像「社團編號」我們可以設為 INT (整數)

play10:33

而「社團名稱」可以設成 VARCHAR

play10:35

也就是一般的文字字串

play10:37

字數上限我設為 15

play10:39

對於一般的社團名稱來說應該足夠了

play10:42

最後我們還要在表格內指定一個欄位為「主鍵」(Primary Key)

play10:46

「主鍵」的功能是用來識別每一筆資料

play10:49

因此主鍵不能有重複的數值

play10:51

也不能是空值 (Null)

play10:53

有點類似我們的身份證字號

play10:55

語法寫好後

play10:56

我按下 Ctrl +Enter 鍵送出

play10:59

一個新表格就會顯示在旁邊的側邊欄了

play11:02

而如果你在資料庫中

play11:04

不小心建立了多餘的表格也沒有關係

play11:07

你只要用 DROP TABLE 的語法

play11:09

後面接續表格的名稱

play11:11

就能把該表格從資料庫中刪除掉囉

play11:14

好,那我們已經有了一個「社團」表格

play11:17

只是裡面還沒有儲存任何資料

play11:19

那要在表格內輸入新資料的語法是 INSERT INTO

play11:23

後面接續表格的名稱

play11:25

和要插入資料的欄位標題

play11:28

然後我們可以在第二行輸入 VALUES

play11:30

來把資料一筆一筆的輸入進去

play11:32

並且記得圓括號內的資料順序

play11:35

必須和欄位的名稱相互對應

play11:37

而如果想要確認資料是否已成功輸入到表格內的話

play11:41

你可以在底下加上一行 SELECT 全部的語法

play11:45

按下 Ctrl +Enter 送出

play11:47

這樣「社團」表格內就會有四筆資料了

play11:50

那假設後來我們收到通知

play11:52

知道編號 104 的社團名稱叫做「舞蹈社」

play11:55

那麼我們可以使用 UPDATE 的語法來更新資料

play11:59

那要注意使用 UPDATE 時

play12:01

後面一定要用 WHERE 語法說明你要更新哪一筆記錄

play12:05

如果沒有指定的話

play12:06

會導致全部社團的名稱都變成舞蹈社

play12:09

而這個情形同樣也適用於 DELETE 語法

play12:13

DELETE 能夠「刪除」表格中的某筆記錄

play12:16

如果忘記用 WHERE 設定篩選條件的話

play12:18

會導致表格內的所有資料都被刪除掉喔

play12:22

OK,最後我們來看一下

play12:23

如何用 SQL 進行跨表格的查詢

play12:26

由於不同表格可能會有相同名稱的欄位

play12:30

因此這裡我們要在欄位名稱前

play12:32

另外標注「表格」的名稱

play12:34

查詢的主要資料來源是 students 表格

play12:37

然後我們要用到一個叫做 LEFT JOIN 的語法

play12:40

來把 clubs 表格加入進來

play12:43

那在結合兩個表格的時候

play12:45

我們必須說明這兩個表格是如何進行連接的

play12:48

以目前這個例子來說

play12:50

我們是用 students 表格中的「社團」

play12:52

連接到 clubs 表格中「社團編號」

play12:55

那這邊我們先查詢 1 年 1 班的同學就好了

play12:59

我按下 Ctrl +Enter

play13:01

那使用 LEFT JOIN 的方式來連結表格時

play13:04

系統會列出左側表格的所有記錄

play13:06

也就是 1 年 1 班全部的同學

play13:09

儘管有的同學可能沒報名社團

play13:11

或是填寫的社團編號找不到對應的名稱

play13:15

而如果你想要把這些沒對應上的記錄隱藏起來

play13:19

你可以把 JOIN 的方式改成 INNER (內部聯結)

play13:22

這樣就只有成功對應到社團名稱的同學才會被列出來喔

play13:27

好,那希望今天介紹的 SQL 基礎語法

play13:29

能對有需要的朋友們有些幫助

play13:32

那我們就下回再見囉,拜拜

Rate This

5.0 / 5 (0 votes)

الوسوم ذات الصلة
SQL basicsData managementSqlitevizDatabase queriesSQL tutorialData analysisFiltering dataSQL practiceBeginner-friendlySQL syntax
هل تحتاج إلى تلخيص باللغة الإنجليزية؟