3. SQL-Server
▪ Central part of the Microsoft Data
Platform
▪ Operational Database Management
System (ODBMS)
▪ Cross-Platform (Windows, Linux, Docker
& Kubernetes)
▪ Providing advanced data services
(Database Engine, SSIS, MDS, SSAS,
SSRS & Machine Learning Services)
https://www.microsoft.com/en-us/sql-server
4. SQL-Server Components
▪ SQL Server Database Engine Services
▪ SQL Server Integration Services (SSIS)
▪ SQL Server Master Data Services (MDS)
▪ SQL Server Analysis Services (SSAS)
▪ SQL Server Reporting Services (SSRS)
▪ SQL Server Machine Learning Services
5. SQL-Server Connectors
▪ MicrosoftADO.NET for SQL Server
▪ Microsoft JDBC Driver for SQL Server
▪ Microsoft ODBC Driver for SQL Server
▪ Node.js Driver for SQL Server
▪ Python Driver for SQL Server
▪ Ruby Driver for SQL Server
9. SQL-Server Platforms
▪ Linux (RHEL, Ubuntu & SLES)
▪ Windows
▪ Windows Server
▪ Virtual Machine in Azure
▪ Docker
▪ macOS
▪ Kubernetes (Next)
Windows
Docker
Linux
13. SQL-Server GUI Tools
▪ Azure Data Studio (ADS)
▪ SQL Server Management Studio (SSMS)
▪ Visual Studio Code (VS Code)
▪ SQL Server DataTools (SSDT)
▪ Power BI
SSMS
VS Code
ADS
16. Database
A database is made up of a collection of tables that stores a specific set
of structured data. A table contains a collection of rows, also referred to
as records or tuples, and columns, also referred to as attributes. Each
column in the table is designed to store a certain type of information,
for example, dates, names, amounts, and numbers.
17. Relational Data Model in DBMS
Id Name AccountName Status
1 Bilal Google Active
2 Khalil Amazon Active
3 Ahmad Microsoft Inactive
4 Rizwan Amazon Active
Column/Attribute/Field (Domain)
Entity/Relation (Table)
(Primary Key)
Entity Integrity
Row
/Tuple
(Record)
18. Methods of Data Storage
▪ Text File (CSV)
▪ Spreadsheet (MS Excel)
▪ Database Software (MS Access, Oracle, MS SQL Server, MySQL etc.)
19. SQL-Server Database
A computer can have one or more than one instance of SQL Server
installed. Each instance of SQL Server can contain one or many
databases. Within a database, there are one or many object ownership
groups called schemas. Within each schema there are database objects
such as tables, views, and stored procedures. Some objects such as
asymmetric keys are contained within the database, but are not
contained within a schema.
20. Database-as-a-Service (DBaaS) Solutions
▪ MicrosoftAzure
▪ MongoDB – Atlas
▪ AmazonWeb Services (AWS) – Aurora
▪ Google Cloud Platform (GCP) – BigQuery
▪ IBM – Db2
▪ SAP
21. Types of Database
NoSQL
▪ UnStructured Query Language
▪ Non-Relational Database
▪ Document based Databases
▪ Based on CAPTheorem
▪ Used for OLAP
▪ Not a good for complex queries
SQL
▪ Structured Query Language
▪ Relational Database
▪ Table based Databases
▪ Based on ACID Properties
▪ Used for OLTP
▪ Good for complex queries
27. @@VERSION – T-SQL Configuration Function
▪ Returns system and build information for the current installation of
SQL Server.
– SQL Server version
– Processor architecture
– SQL Server build date
– Copyright statement
– SQL Server edition
– Operating system version
▪ SELECT @@VERSION AS ' Column Name';
35. What is Transact-SQL?
▪ Structured Query Language (SQL)
– Developed by IBM in 1970s
– SQL common RDBMS database language
– Widely used in industry
– SQL is declarative, not procedural
– Describe what you want, don’t specify steps
▪ Transact-SQL
– Microsoft implementation
– Referred to asT-SQL
– Query language for SQL-Server andAzure DB
37. Transact-SQL Data Types
▪ Exact Numerics
▪ Approximate Numerics
▪ Character Strings
▪ Unicode Character Strings
▪ Binary Strings
▪ Date andTime
▪ Other data types
38. Exact Numerics Data Types
Transact-SQL Exact Numeric Data Types
bigint -2^63 (-9,223,372,036,854,775,808) to 2^63-1 (9,223,372,036,854,775,807)
(8-Bytes)
int -2^31 (-2,147,483,648) to 2^31-1 (2,147,483,647) (4-Bytes)
smallint -2^15 (-32,768) to 2^15-1 (32,767) (2-Bytes)
tinyint 0 to 255 (1-Byte )
bit 0 to 1 (1-Byte )
decimal -10^38+1 to 10^38-1 (5 to 17 Bytes)
numeric -10^38+1 to 10^38-1 (5 to 17 Bytes)
money -922,337,203,685,477.5808 to +922,337,203,685,477.5807 (8-Bytes)
smallmoney - 214,748.3648 to 214,748.3647 (4-Bytes)
39. Approximate Numerics Data Types
Transact-SQL Approximate Numeric Data Types
float -1.79E+308 to 1.79E+308
(Memory depends on the value of n) (Precision 7-Digit)
real -3.40E+38 to 3.40E+38
(Memory 4-Bytes) (Precision 15-Digit)
40. Character Strings Data Types
Transact-SQL Character Strings Data Types
char
text
varchar
41. Unicode Character Strings Data Types
Transact-SQL Unicode Character Strings Data Types
nchar
ntext
nvarchar
42. Binary Strings Data Types
Transact-SQL Binary Strings Data Types
binary
image
varbinary
43. Date and Time Data Types
Transact-SQL Date and Time Data Types
date
datetime2
datetime
datetimeoffset
smalldatetime
time
44. Other data types
Transact-SQL Other data types
cursor
hierarchyid
sql_variant
Spatial Geometry
Types
table
rowversion
uniqueidentifier
xml
Spatial
Geography Types
45. Transact-SQL Statements
▪ Query Statements
▪ Data Definition Language (DDL) statements
▪ Data Manipulation Language (DML) statements
▪ Data Control Language (DCL) statements
▪ Transaction Control Language (TCL) statements
46. Transact-SQL Language Elements
▪ Core elements
▪ Batches, Control-of-flow andVariables
▪ Operators
▪ Wildcard characters to match
▪ Functions
▪ SET statements
▪ Procedures
47. ▪ We will start with common query
statements.
Transact-SQL Common Query Statements
▪ SELECT
▪ FROM
▪ WHERE
▪ GROUP BY
▪ HAVING
▪ ORDER BY
48. ▪ Statements for defining database objects
Transact-SQL Data Definition Language
▪ CREATE
▪ ALTER
▪ DROP
49. ▪ Statements for querying and modifying
data
Transact-SQL Data Manipulation Language
▪ SELECT
▪ INSERT
▪ MERGE
▪ UPDATE
▪ DELETE
50. ▪ Statements for assigning security
permissions
Transact-SQL Data Control Language
▪ GRANT
▪ REVOKE
▪ DENY
51. ▪ Statements for mange and control
database transaction
Transact-SQL Transaction Control Language
▪ BEGIN
TRANSACTION
▪ COMMIT
TRANSACTION
▪ ROLLBACK
TRANSACTION
▪ SAVE
TRANSACTION
53. Transact-SQL Common Query Statements
Element Expression Role
SELECT <select list> Defines which columns to return
FROM <table source> Defines table(s) to query
WHERE <search condition> Filters rows using a predicate
GROUP BY <group by list> Arranges rows by groups
HAVING <search condition> Filters groups using a predicate
ORDER BY <order by list> Sorts the output
SELECT OrderDate, COUNT(OrderID)
FROM Sales.SalesOrder
WHERE Status = 'Shipped'
GROUP BY OrderDate
HAVING COUNT(OrderID) > 1
ORDER BY OrderDate DESC;
5
1
2
3
4
6
54. Transact-SQL Basic SELECT Query Examples
SELECT * FROM Production.Product;
SELECT Name, ListPrice
FROM Production.Product;
SELECT Name AS Product, ListPrice * 0.9 AS SalePrice
FROM Production.Product;
▪ All columns
▪ Specific columns
▪ Expressions and Aliases
55. Transact-SQL System Databases
▪ master – Records all the system-level information for an instance of SQL-Server .
▪ model – Is used as the template for all databases created on the instance.
▪ msdb – Is used by SQL-Server Agent for scheduling alerts and jobs.
▪ tempdb – Is a workspace for holding temporary objects or intermediate result sets.
60. Transact-SQL ALTER DATABASE
▪ Modifies certain configuration options of a database.
▪ ALTER DATABASE { database_name | CURRENT }
{ MODIFY NAME = new_database_name
| COLLATE collation_name
| <file_and_filegroup_options>
| SET <option_spec> [… n] [WITH <termination>]
} [;]
62. Transact-SQL DROP DATABASE
▪ Removes one or more user databases or database snapshots from an
instance of SQL Server.
▪ DROP DATABASE [IF EXISTS] { database_name
| database_snapshot_name } [, … n] [;]
68. Transact-SQL ALTER SCHEMA
▪ Transfers a securable between schemas.
▪ ALTER SCHEMA schema_name
TRANSFER [ <entity_type> :: ] securable_name [;]
▪ <entity_type> :: =
{OBJECT | TYPE | XML Schema Collection}
74. Transact-SQL Schemas and Object Names
▪ Schemas are namespaces for database objects
▪ Fully-qualified names:
[server_name.][database_name.][schema_name.][object_name]
▪ Best practice: [schema_name.][object_name]
Sales Production
Order
Customer
Product
Order
Sales.Order
Sales.Customer
Production.Product
Production.Order
75. Transact-SQL CREATE TABLE (1st Method)
▪ Creates a new table in SQL-Server.
▪ CREATE TABLE {schema_name.table_name | table_name}
(
column_name_1 data_type,
column_name_2 data_type
[, … n]
) [;]