SlideShare a Scribd company logo
1 of 78
Download to read offline
SQL 新⼿手入⾨門
Roy@NISRA
1
whoami
• Roy / 羅伊 / 鍾秉桓 / roy4801
• NISRA 108 會長
• 資⼯工三⼄乙
• roy@nisra.net 👈 email 沒事不要亂寄(x
• blog.roy4801.tw 👈 個⼈人blog
2
Agenda
• SQL 介紹
• 基本語法
• Labs
• ⼀一點點 SQL injection
3
SQL 是什什麼
4
5
Structured
Query
Language
資料庫是什什麼
6
資料庫是什什麼
7
存資料的地⽅方
要怎麼拿資料?
8
要怎麼拿資料?
9
⽤用 SQL
資料庫長怎樣?
10
要怎麼拿資料?
11
Database
id name age verify
0 Roy 20 1
1 Theo 22 1
2 ABC 0 0
Member
id INT
name VARCHAR(20)
age INT
verify BOOLEAN
SQL
12
• 是⼀一種資料操作語⾔言(DML)
• ⼀一定會有以下功能
• 增 Create
• 查 Request
• 改 Update
• 刪 Delete
實驗環境
13
實驗環境
14
• SQL是語⾔言,要有engine來來驅動
• 常⾒見見engine:
• MySQL 👈 Lab 會⽤用的
• SQLite
• PostgreSQL
• 還有很多
• https://db-engines.com/en/ranking
實驗環境
15
• 交互介⾯面
• MySQL 👉 純⽂文字介⾯面
• 為了了⽅方便便學習,使⽤用phpMyAdmin
實驗環境
16
databases
data
指令輸入框
基本語法
17
Database
18
• SHOW DATABASES; // 列列出 db
• CREATE DATABASE db_name; // 新建 db
• USE db_name; // 使⽤用 db
• DROP DATABASE db_name; // 刪除 db
Table
19
• SHOW tables; // 列列出 tables
Table
20
• CREATE TABLE table_name(
col1 type,
col2 type,
…
); // 新建⼀一張 table
• DROP TABLE table_name; // 刪除 table
引號
21
• table, database 名稱可⽤用(`)包裹
• USE `password`;
• 數值⽤用單引號(')包裹
• SELECT … WHERE name = 'kurisu';
Table - example
22
• CREATE TABLE person(
pid INT,
name VARCHAR(50), // 名字
bir_date DATE, // ⽣生⽇日
height FLOAT, // ⾝身⾼高
weight FLOAT, // 體重
PRIMARY KEY (pid);
); // 個⼈人資料
Table - type
23
pid INT,
name VARCHAR(50),
bir_date DATE,
height FLOAT,
weight FLOAT
INT, VARCHAR(50), FLOAT?
Table - type
24
pid INT,
name VARCHAR(50),
bir_date DATE,
height FLOAT,
weight FLOAT
資料型態
Table - type
25
• 整數 bytes
• TINYINT - 1
• SMALLINT - 2
• INT - 4
• BIGINT - 8
Table - type
26
• 浮點數 bytes
• FLOAT - 4
• DOUBLE - 8
Table - type
27
• 字串串(⽂文字)
• CHAR(N) 固定長度(較快)最長255
• VARCHAR(N) 變動長度 最長 65535
• TEXT 固定長度 最長 65535
Table - type
28
• CHAR VARCHAR 差別
Value CHAR(4) VARCHAR(4)
' ' ' ' ''
'ab' 'ab ' 'ab'
'abc' 'abc ' 'abc'
'abcd' 'abcd' 'abcd'
Table - type
29
• CHAR 拿出來來會⾃自動去掉空⽩白
• 'abc ' -> 'abc'
• VARCHAR 不會
• 'abc ' -> 'abc '
Table - type
30
• ⽇日期
• DATE '0000-00-00'
• TIME '00:00:00'
• DATETIME '0000-00-00 00:00:00’
• TIMESTAMP unix time stamp
• '2038-01-19 03:14:07'
select 查詢
31
• SELECT * FROM table_name

WHERE condition // 篩選條件

ORDER BY condition // 排序

GROUP BY condition; // 分組

• SELECT col1, col2, … FROM table_name;

• 「*」 是拿所有欄欄位
select 查詢 WHERE
32
• SELECT * FROM `employees`
WHERE emp_no = '0';
lab - select where
33
• 找出所有年年齡⼤大於20的⼈人







• SELECT * FROM `employees` WHERE
birth_date < '1999-01-01';
select 查詢 AND OR NOT
34
• SELECT * FROM `employees`

WHERE condition1 [AND/OR] condition2;

• WHERE NOT condition;
AND 與 a AND b
OR 或 a OR b
NOT 非 NOT a
select 查詢 AND OR NOT
35
• SELECT * FROM `employees`

WHERE birth_date < '1999-01-01'

AND first_name = 'Roy';
• 「⼤大於20歲」且「名字是Roy」的員⼯工
lab - AND OR NOT
36
• 找出所有年年齡⼤大於20的⼈人且是女性
• SELECT * FROM `employees`

WHERE birth_date < '1999-01-01'

AND gender = 'F';
select 查詢 ORDER BY
37
• SELECT * FROM `employees`

ORDER BY condition [ASC/DESC]
ASC 遞增 (ascending) 0 1 2 3 4 5
DESC 遞減 (descending) 5 4 3 2 1 0
預設是 ASC
select 查詢 ORDER BY
38
• SELECT * FROM `employees`

WHERE gender = 'M'

ORDER BY birth_date;
• 男性員⼯工以⽣生⽇日排序
lab - select order by
39
• 找出所有年年齡⼤大於20的⼈人且是女性「以年年齡排序」
• SELECT * FROM `employees`

WHERE birth_date <= '1999-01-01'

AND gender = 'F'

ORDER BY birth_date;
insert 插入
40
• INSERT INTO table_name (col1, …)
VALUES (val1, …)
• 插入資料到 table_name 表裡
insert 插入
41
• INSERT INTO employees
(birth_date, first_name, last_name, gender,
hire_date)
VALUES (’1998-12—24’, 'Roy', 'Zhong',
'M', ’2019-08-19’)
insert 插入 - lab
42
• 插入⾃自⼰己的個資吧(X
update 更更新
43
• UPDATE table_name
SET col1 = 'val1' // 設定值
, col2 = 'val2'
WHERE condition; // 過濾條件
update 更更新
44
• UPDATE `employees`
SET emp_no = '87878787'
WHERE emp_no = '0';
lab - update 更更新
45
• 隨便便找⼀一個⼈人來來改他的年年齡吧
lab - update 更更新
46
• UPDATE `employees`

SET birth_date = '1932-01-01'

WHERE emp_no = '0';
• 把emp_no = 0的⼈人的年年齡改成87歲
delete 刪除
47
• DELETE FROM table_name
WHERE condition;
• DELETE FROM employee
WHERE first_name = 'Roy';
lab - delete 刪除
48
• 隨便便刪掉⼀一個⼈人吧
更更多 select
49
select LIMIT
50
• SELECT * FROM `employees`

LIMIT 10;
• 限制輸出數量量
select 統計
51
• MIN() 最⼩小
• MAX() 最⼤大
• COUNT() 記數
• AVG() 平均
• SUM() 總和
select 統計
52
• 作⽤用在「拿出的欄欄位」上
• 也就是在 SELECT 跟 FROM 之間出現
• 例例如
• SELECT COUNT(col1), AVG(col2), … FROM
table_name;
select MIN() MAX()
53
• SELECT MIN(salary) FROM `salary`;
• 薪⽔水最低
lab - MIN() MAX()
54
• 找出年年紀最⼤大的是多少
• SELECT MIN(birt_date) FROM employees;
select COUNT()
55
• SELECT COUNT(*) FROM employees;
• 員⼯工數量量
lab - COUNT()
56
• 找出年年齡⼤大於25歲的有幾個⼈人
• SELECT COUNT(*) FROM employees

WHERE birth_date <= ’1994-01-01’;
select AVG()
57
• SELECT AVG(salary) FROM salary;
• 找薪⽔水平均值
select SUM()
58
• SELECT SUM(salary) FROM salary;
• 所有⼈人的薪⽔水總合
select GROUP BY
59
• SELECT * FROM employees

GROUP BY gender;
• 依照欄欄位分組
• 通常跟 統計的function⼀一起⽤用

(MIN(), MAX(), COUNT() …)
⼀一些 Lab
60
lab 01
61
• 找出「最年年輕」的員⼯工







• SELECT * FROM `employees`

ORDER BY birth_date DESC LIMIT 1
lab 02
62
• 找出20到40歲的員⼯工







• SELECT * FROM `employees`

WHERE birth_date <= '1999-01-01'

AND birth_date >= '1979-01-01'
lab 03
63
• 找出男性員⼯工跟女性員⼯工有幾⼈人







• SELECT COUNT(*), gender

FROM `employees`

GROUP BY gender;
lab 04
64
• 找出所有的職稱







• SELECT * FROM titles

GROUP BY title

ORDER BY title;
lab 05
65
• 找出薪⽔水最⾼高的員⼯工資料



• SELECT * FROM employees 

WHERE emp_no = (

SELECT emp_no 

FROM salaries 

ORDER BY salary DESC LIMIT 1

)
SQL Injection
66
SQL Injection
67
• Injection?
SQL Injection
68
沒有過濾使⽤用者的輸入

直接帶入
SQL Injection
69
https://xkcd.com/327/
SQL Injection - PHP
70
$name = "Robert";

$sql = "INSERT INTO Sutdents (name)
VALUES ('".$name."')";
INSERT INTO Students (name) VALUES

('Robert')
SQL Injection - PHP
71
$name = "Robert');DROP TABLE Student --";

$sql = "INSERT INTO Sutdents (name) VALUES
('".$name."')";
INSERT INTO Students (name) VALUES

('Robert');
DROP TABLE Student --
SQL Injection - PHP
72
• 帳號密碼輸入?
SQL Injection - PHP
73
• SELECT * FROM account

WHERE username = 'xxx'

AND password = 'xxx';
SQL Injection - PHP
74
• SELECT * FROM account

WHERE username = 'admin OR 1=1 -- ’

AND password = 'xxx';
SQL Injection - PHP
75
• SELECT * FROM account

WHERE username = 'admin' OR 1=1 -- ’

AND password = 'xxx';
Login as admin
SQL Injection - PHP
76
• 如何防禦?
• escape all user input
• url encode
• hex encode
• PHP
• MySQLi
• PDO
• 上WAF
SQL Injection - PHP
77
• admin%20OR%201%3D1%20--%20
• 解析失敗
SQL Injection
78
•千萬別相信使⽤用者的輸入。

More Related Content

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Featured (20)

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 

sql-intro