Bienvenue

Introduction à SQL Server

John MacKintosh

Instructor

shutterstock_1255111450.jpg

shutterstock_418087330.jpg

Introduction à SQL Server

SQL Server et Transact-SQL

  • SQL Server : système de base de données relationnelle développé par Microsoft

  • Transact-SQL (T-SQL) : implémentation de SQL par Microsoft, avec des fonctions en plus

  • Dans ce cours : maîtriser les bases de T-SQL

  • Apprendre à écrire des requêtes

Introduction à SQL Server

shutterstock_274642010.jpg

Introduction à SQL Server

Bases des requêtes

  • SQL Server : le « magasin » avec bases et tables

  • Requêtes : comment « choisir » des éléments de différents rayons et remplir le panier

  • SELECT : mot-clé pour récupérer des données

Introduction à 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           |
| ...                                 |
+-------------------------------------+
Introduction à SQL Server

Sélectionner plusieurs colonnes

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         |
+-----------+----------------------+
Introduction à SQL Server

Mise en forme des requêtes

SELECT description, event_year, event_date
FROM grid;
SELECT 
  description, 
  event_year, 
  event_date
FROM 
  grid;
Introduction à SQL Server

SELECT TOP()

-- Retourner 5 lignes
SELECT TOP(5) artist
FROM artists;
-- Retourner les 5 % premiers
SELECT TOP(5) PERCENT artist 
FROM artists;
+-----------------------+
| artist                |
|-----------------------|
| AC/DC                 |
| Accept                |
| Aerosmith             |
| Alanis Morissette     |
| Alice in Chains       |
+-----------------------+
Introduction à SQL Server

SELECT DISTINCT

-- Retourner toutes les lignes de la table
SELECT nerc_region 
FROM grid;
+-------------+
| nerc_region |
|-------------|
| RFC         |
| RFC         |
| MRO         |
| MRO         |
| ....        |
+-------------+
-- Retourner les lignes uniques
SELECT DISTINCT nerc_region 
FROM grid;
+-------------+
| nerc_region |
|-------------|
| NPCC        |
| NPCC RFC    |
| RFC         |
| ERCOT       |
| ...         | 
+-------------+
Introduction à SQL Server

SELECT *

-- Retourner toutes les lignes
SELECT * 
FROM grid;
  • À NE PAS utiliser sur de grandes tables
Introduction à SQL Server

Donner des alias avec 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  |
+------------------------------+
Introduction à SQL Server

Écrivons un peu de T-SQL !

Introduction à SQL Server

Preparing Video For Download...