changed 6 months ago
Linked with GitHub

計算機概論Lab-12

SQL

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

https://hackmd.io/@IMOK/Lab12


Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

講師: 賴昱有


BEFORE CLASS

http://140.121.197.13/tutorial
先下載解壓縮後執行安裝
SQLServer2017-SSEI-Expr.exe


什麼是資料庫

  • 結構
    • Database
    • Table
    • Field
    • Record
  • CRUD
    • Create
    • Read
    • Update
    • Delete

image


關聯式資料庫

Relational Database Management System(RDBMS)

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

非關聯式資料庫

NoSQL (Not Only SQL)

  • Key-Value Database
  • Large-Scale Database
  • Document Database
  • Graph Database
  • Time series database

匯入範例資料庫

  • 打開並複製 instnwnd.sql 中所有內容
  • 打開 SSMS
  • 連接到 MYSQL Server
  • Ctrl+N 打開指令編輯器
  • 貼上範例資料庫內容
  • F5 執行所有指令

image


CRUD IN MYSQL

  • Create
    • INSERT
  • Read
    • SELECT
  • Update
    • UPDATE
  • Delete
    • DELETE

SELECT

基本指令形式

SELECT column1, column2 FROM table_name [WHERE conditions] [ORDER BY column1 order];

e.g.

SELECT CategoryID, CategoryName, Description, Picture FROM Northwind.dbo.Categories

SELECT * FROM Northwind.dbo.Categories WHERE CategoryID=1;
Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

查找 Description 是 S 開頭的

SELECT * FROM Northwind.dbo.Categories WHERE Description LIKE 'S%';

降序排序(DESC)、升序排列(ASC)

SELECT * FROM Northwind.dbo.Categories WHERE Description LIKE 'S%' ORDER BY CategoryName DESC;

數資料筆數

SELECT COUNT(*) FROM Northwind.dbo.Categories

INNER JOIN

SELECT * FROM Northwind.dbo.Categories INNER JOIN Northwind.dbo.Products ON Northwind.dbo.Categories.CategoryID = Northwind.dbo.Products.CategoryID

  • Categories

    CategoryID CategoryName
    1 Drink
    2 Food
  • Products

    CategoryID CategoryName CategoryID
    1 Beer 1
    2 Cola 1
    3 Bread 2
  • Inner Join on CategoryID

    CategoryID CategoryName ProductID ProductName
    1 Drink 1 Beer
    1 Drink 2 Cola
    2 Food 3 Bread

INSERT

基本指令形式

INSERT INTO table_name(column1, column2, ...) VALUES ('value1', 'value2', ...);

e.g.

INSERT INTO Northwind.dbo.Categories VALUES ('test', 'test', 'test');

UPDATE

基本指令形式

UPDATE table_name SET column1='value1', column2='value2' [WHERE conditions];

e.g.

UPDATE Northwind.dbo.Categories SET CategoryName='test2' WHERE CategoryName='test'

DELETE

基本指令形式

DELETE FROM table_name [WHERE conditions];

e.g.

DELETE FROM Northwind.dbo.Categories WHERE CategoryName='test2';

延伸閱讀

w3schools-sql


作業網站

作業做完請上傳到 http://140.121.197.13/tutorial
並且注意上傳時間限制、檔名、其他規範

上課時提前做完可以直接給助教 demo 登記
就不需要上傳 demo 過的部分


Question 1

使用 SQL 指令查詢 Northwind 資料庫內的 Order 資料表 EmployeeID=5 的資料

Question 2

使用 SQL 指令查詢 Northwind 資料庫內的 Order 資料表
EmployeeID 介於 3 到 6 的資料
並且將查詢結果根據 EmployeeID 進行由大至小的排序

  • [HINT] >= AND <= or BETWEEN…AND…

Question 3

使用 SQL 指令計算 Northwind 資料庫內的
Order 資料表共有幾筆訂單。

  • [HINT] SELECT COUNT

Question 4

使用 SQL 指令在 Northwind 資料庫內的 Products 資料表
新增一筆 Products 的資料,內容不限。

  • [HINT] 需注意資料欄位內的資料型態

Question 5

使用 SQL 指令在剛剛新增的 Products 資料將
ProductName 設定為 Egg,UnitPrice設定為 12.00。


Question 6

使用 SQL 指令刪除 Northwind 資料庫內的
Products 資料表你所新增的 Products 資料


Question 7

使用 SQL 指令將 Northwind 資料庫內
Categories、Order Details 以及 Products 資料表進行結合
查詢出每筆訂單詳細的
產品名稱 (ProductName)、種類名稱(CategoryName) 及單位價格 (UnitPrice)

  • [HINT] (nested) INNER JOIN
Select a repo