Bun venit

Introducere în SQL Server

John MacKintosh

Instructor

shutterstock_1255111450.jpg

shutterstock_418087330.jpg

Introducere în SQL Server

SQL Server și Transact-SQL

  • SQL Server – sistem de baze de date relaționale dezvoltat de Microsoft

  • Transact-SQL (T-SQL) – implementarea Microsoft a SQL, cu funcționalități suplimentare

  • În acest curs: fundamentele T-SQL

  • Cum să scrieți interogări

Introducere în SQL Server

shutterstock_274642010.jpg

Introducere în SQL Server

Interogări 101

  • SQL Server: magazinul care conține baze de date și tabele

  • Interogări: cum selectăm articole din diferite rafturi și le adăugăm în coș

  • SELECT: termenul cheie pentru extragerea datelor

Introducere în 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           |
| ...                                 |
+-------------------------------------+
Introducere în SQL Server

Selectarea mai multor coloane

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         |
+-----------+----------------------+
Introducere în SQL Server

Formatarea interogărilor

SELECT description, event_year, event_date
FROM grid;
SELECT 
  description, 
  event_year, 
  event_date
FROM 
  grid;
Introducere în 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       |
+-----------------------+
Introducere în 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       |
| ...         | 
+-------------+
Introducere în SQL Server

Select *

-- Return all rows
SELECT * 
FROM grid;
  • NU este potrivit pentru tabele mari
Introducere în SQL Server

Redenumirea coloanelor cu 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  |
+------------------------------+
Introducere în SQL Server

Să scriem T-SQL!

Introducere în SQL Server

Preparing Video For Download...