歡迎

SQL Server 入門

John MacKintosh

Instructor

超市貨架與購物車

鍵盤與資料庫圖示

SQL Server 入門

SQL Server 與 Transact-SQL

  • SQL Server-Microsoft 開發的關聯式資料庫系統

  • Transact-SQL(T-SQL)-Microsoft 的 SQL 實作,並擴充功能

  • 本課程:掌握 T-SQL 基礎

  • 學會撰寫查詢

SQL Server 入門

打開筆電開始學習

SQL Server 入門

查詢入門 101

  • SQL Server:存放資料庫與資料表的「商店」

  • 查詢:像在不同走道「挑選」商品,放進購物車

  • SELECT:擷取資料的關鍵詞

SQL Server 入門
SELECT description 
FROM grid;
+-------------------------------------+
| description                         |
|-------------------------------------|
| Severe Weather Thunderstorms        |
| Severe Weather Thunderstorms        |
| Severe Weather Thunderstorms        |
| Fuel Supply Emergency Coal          |
| Physical Attack Vandalism           |
| Physical Attack Vandalism           |
| Physical Attack Vandalism           |
| Severe Weather Thunderstorms        |
| Severe Weather Thunderstorms        |
| Suspected Physical Attack           |
| Physical Attack Vandalism           |
| ...                                 |
+-------------------------------------+
SQL Server 入門

選取多個欄位

SELECT 
  artist_id, 
  artist_name 
FROM 
  artist;
+-----------+----------------------+
| artist_id | artist_name          |
|-----------+----------------------|
| 1         | AC/DC                |
| 2         | Accept               |
| 3         | Aerosmith            |
| 4         | Alanis Morissette    |
| 5         | Alice In Chains      |
| 6         | Antônio Carlos Jobim |
| 7         | Apocalyptica         |
| 8         | Audioslave           |
| 9         | BackBeat             |
| 10        | Billy Cobham         |
+-----------+----------------------+
SQL Server 入門

查詢格式化

SELECT description, event_year, event_date
FROM grid;
SELECT 
  description, 
  event_year, 
  event_date
FROM 
  grid;
SQL Server 入門

選取 TOP ()

-- 只回傳 5 列
SELECT TOP(5) artist
FROM artists;
-- 回傳前 5% 的列
SELECT TOP(5) PERCENT artist 
FROM artists;
+-----------------------+
| artist                |
|-----------------------|
| AC/DC                 |
| Accept                |
| Aerosmith             |
| Alanis Morissette     |
| Alice in Chains       |
+-----------------------+
SQL Server 入門

選取 DISTINCT

-- 回傳資料表中的所有列
SELECT nerc_region 
FROM grid;
+-------------+
| nerc_region |
|-------------|
| RFC         |
| RFC         |
| MRO         |
| MRO         |
| ....        |
+-------------+
-- 回傳不重複的列
SELECT DISTINCT nerc_region 
FROM grid;
+-------------+
| nerc_region |
|-------------|
| NPCC        |
| NPCC RFC    |
| RFC         |
| ERCOT       |
| ...         | 
+-------------+
SQL Server 入門

選取 *

-- 回傳所有列
SELECT * 
FROM grid;
  • 不適合大型資料表
SQL Server 入門

用 AS 更名欄位

SELECT demand_loss_mw AS lost_demand 
FROM grid;
+-------------+
| lost_demand |
|-------------|
| 424         |
| 217         |
| 494         |
| 338         |
| 3900        |
| 3300        |
+-------------+
SELECT description AS cause_of_outage 
FROM grid;
+------------------------------+
| cause_of_outage              |
|------------------------------|
| Severe Weather Thunderstorms |
| Fuel Supply Emergency Coal   |
| Physical Attack Vandalism    |
| Suspected Physical Attack    |
| Electrical System Islanding  |
+------------------------------+
SQL Server 入門

來寫些 T-SQL 吧!

SQL Server 入門

Preparing Video For Download...