欢迎

SQL Server 入门

John MacKintosh

Instructor

shutterstock_1255111450.jpg

shutterstock_418087330.jpg

SQL Server 入门

SQL Server 与 Transact-SQL

  • SQL Server:微软开发的关系型数据库系统

  • Transact-SQL(T-SQL):微软的 SQL 实现,功能扩展版

  • 本课程:掌握 T-SQL 基础

  • 学习编写查询

SQL Server 入门

shutterstock_274642010.jpg

SQL Server 入门

查询入门

  • 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 入门

SELECT TOP ()

-- Return 5 rows
SELECT TOP(5) artist
FROM artists;
-- Return top 5% of rows
SELECT TOP(5) PERCENT artist 
FROM artists;
+-----------------------+
| artist                |
|-----------------------|
| AC/DC                 |
| Accept                |
| Aerosmith             |
| Alanis Morissette     |
| Alice in Chains       |
+-----------------------+
SQL Server 入门

SELECT DISTINCT

-- Return all rows in the table
SELECT nerc_region 
FROM grid;
+-------------+
| nerc_region |
|-------------|
| RFC         |
| RFC         |
| MRO         |
| MRO         |
| ....        |
+-------------+
-- Return unique rows
SELECT DISTINCT nerc_region 
FROM grid;
+-------------+
| nerc_region |
|-------------|
| NPCC        |
| NPCC RFC    |
| RFC         |
| ERCOT       |
| ...         | 
+-------------+
SQL Server 入门

SELECT *

-- Return all rows
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...