0% found this document useful (0 votes)
140 views

Microsoft PowerPoint - MS SQL Server

This 6 hour training covers foundational concepts for working with Microsoft SQL Server including: 1) Introductions to SQL, basic database concepts like schemas and ER diagrams, and how to work with SQL Server Management Studio. 2) Learning how to create and modify database tables using commands like CREATE TABLE, ALTER TABLE, and DROP TABLE. 3) Understanding how to define relationships between tables and enforce data integrity using constraints like primary keys, foreign keys, check constraints and default values. 4) Learning techniques for querying data from multiple tables using joins, indexes and other methods.

Uploaded by

finoke5571
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
140 views

Microsoft PowerPoint - MS SQL Server

This 6 hour training covers foundational concepts for working with Microsoft SQL Server including: 1) Introductions to SQL, basic database concepts like schemas and ER diagrams, and how to work with SQL Server Management Studio. 2) Learning how to create and modify database tables using commands like CREATE TABLE, ALTER TABLE, and DROP TABLE. 3) Understanding how to define relationships between tables and enforce data integrity using constraints like primary keys, foreign keys, check constraints and default values. 4) Learning techniques for querying data from multiple tables using joins, indexes and other methods.

Uploaded by

finoke5571
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

MS SQL Server

Foundation Training

Disclaimer: This material is protected under copyright act AnalytixLabs ©, 2011-2016. Unauthorized use and/ or duplication of this material or any part of this material
including data, in any form without explicit and written permission from AnalytixLabs is strictly prohibited. Any violation of this copyright will attract legal actions
MS SQL Server
Total Duration: 6 hours

What is SQL – A Quick Introduction Data manipulation – Reading & Manipulating Optimizing your work
a Single Table • Sub-queries vs. Temp Tables vs. Joins
Getting started • Basic Select statement • Stored Procedures
• SQL Management Studio • Additional components – Where, Group • Optimizing for Composite keys & Non-
• Utilizing the Object Explorer By, Order by & Having clauses numeric Primary keys

Understanding basic RDBMS concepts Data manipulation – Case Study-1


• Schema –Meta Data –ER Diagram • Creating a 360 Deg. Customer view
• Looking at an example Database design
• Data Integrity Constraints & types of Data based objects creation(DDL Commands) Data manipulation - Case Study -2
Relationships • Creating, Modifying & Deleting Tables • Deciling using the n-tile function
• Basic concepts – Queries, Data types & • Create Table & Create Index statements • Row number & Partition by clause
NULL Values, Operators and Comments in • Insert, Update & Delete statements
SQL • Drop & Truncate statements – Uses &
• Rest of the story – Joins, Indexes, Differences
Functions & Views • Alter Table & alter Column statements
These slides are just for reference purpose to
be used along with video based training and
should not considered as substitute for
complete training by itself.
Microsoft SQL Server

Lesson 1

• MS SQL Server
• SQL Management Studio
• Introduction to SSMS GUI
• Utilization of Object Explorer
• How to create Tables
• Primary Key
• Foreign Key
Microsoft SQL Server

Microsoft SQL Server is a Relational Database Management System (RDBMS) developed by Microsoft. It is a
highly scalable product that can be run on anything from a single laptop, to a network of high-powered cloud
servers.

Some common tasks of SSMS(SQL Server Management Studio)

• Create & maintain databases


• Create & maintain tables
• Create & maintain other database objects such as stored procedures, views, triggers etc
• Create & maintain and schedule data backups
• Replication (example: create a copy of the database)
• Create & maintain users, roles, etc
• Optimization tasks
SQL Server Management Studio (SSMS)

Machine 1 Machine 2

Server

Machine 3 Machine 4
Microsoft SQL Server

Lesson 2 – RDBMS(Relational Database Management System) Concepts

• Schema Diagram
• ER Diagram
• Creating tables using query
• Modifying Tables using ALTER query
• Adding Columns to existing table
• Removing tables with Drop Table command
Schema
a representation of a plan or theory in the form of an outline or model.

tbldept-emp
tblSalary
• Employee ID • Employee ID
(PK)
(PK)
• Department
tbldepartment Number
• Salary
tblemployee
• Employee ID (PK)
• Department No.
• First Name
(PK) • Last Name
• Department • DOB
• Manager • Gender tbldesignation
• DOJ • Employee ID
• Address (PK)
• Image • Designation
ER Diagram (Entity Relationship Diagram)
A database model that describes the attributes of entities and relationship occurs between two or
more entities.
String Data Types

Data type Description Storage


char(n) Fixed width character string. Maximum 8,000 characters Defined width

varchar(n) Variable width character string. Maximum 8,000 characters 2 bytes + number of chars

varchar(max) Variable width character string. Maximum 1,073,741,824 characters 2 bytes + number of chars

text Variable width character string. Maximum 2GB of text data 4 bytes + number of chars

nchar Fixed width Unicode string. Maximum 4,000 characters Defined width x 2

nvarchar Variable width Unicode string. Maximum 4,000 characters

nvarchar(max) Variable width Unicode string. Maximum 536,870,912 characters

ntext Variable width Unicode string. Maximum 2GB of text data

bit Allows 0, 1, or NULL


binary(n) Fixed width binary string. Maximum 8,000 bytes
varbinary Variable width binary string. Maximum 8,000 bytes
varbinary(max) Variable width binary string. Maximum 2GB
image Variable width binary string. Maximum 2GB
Numeric Data Types

Data type Description Storage


tinyint Allows whole numbers from 0 to 255 1 byte
smallint Allows whole numbers between -32,768 and 32,767 2 bytes
int Allows whole numbers between -2,147,483,648 and 2,147,483,647 4 bytes
bigint Allows whole numbers between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807 8 bytes
decimal(p,s) Fixed precision and scale numbers.Allows numbers from -10^38 +1 to 10^38 –1. 5-17 bytes
The p parameter indicates the maximum total number of digits that can be stored (both to the left and to
the right of the decimal point). p must be a value from 1 to 38. Default is 18.
The s parameter indicates the maximum number of digits stored to the right of the decimal point. s must
be a value from 0 to p. Default value is 0
numeric(p,s) Fixed precision and scale numbers.Allows numbers from -10^38 +1 to 10^38 –1. 5-17 bytes
The p parameter indicates the maximum total number of digits that can be stored (both to the left and to
the right of the decimal point). p must be a value from 1 to 38. Default is 18.
The s parameter indicates the maximum number of digits stored to the right of the decimal point. s must
be a value from 0 to p. Default value is 0
smallmoney Monetary data from -214,748.3648 to 214,748.3647 4 bytes
money Monetary data from -922,337,203,685,477.5808 to 922,337,203,685,477.5807 8 bytes
float(n) Floating precision number data from -1.79E + 308 to 1.79E + 308.The n parameter indicates whether the 4 or 8 bytes
field should hold 4 or 8 bytes. float(24) holds a 4-byte field and float(53) holds an 8-byte field. Default
value of n is 53.
real Floating precision number data from -3.40E + 38 to 3.40E + 38 4 bytes
Microsoft SQL Server

Lesson 3 – Protecting data integrity with constraints

• Guaranteeing uniqueness with primary key constraints


• Enforcing integrity with foreign key constraints
• Imposing business rules with check constraints
• Check Constraint for Alphabetic values
• Default Value Constraints
• Enabling and disabling check constraints
• Removing constraints with ALTER TABLE
Microsoft SQL Server

Lesson 4 – Querying Multiple Tables

• Matching related rows with INNER JOIN


• Including nonmatched rows with OUTER JOIN
• Creating a Cartesian product with CROSS JOIN
INNER JOIN - Matching rows between two tables

LEFT OUTER JOIN– All records from left hand side table and matching records between two tables

RIGHT OUTER JOIN- All records from Right hand side table and matching records between two tables
FULL OUTER JOIN – All rows from left and right hand side table.

Cross Join – Cartesian product of two tables.


Microsoft SQL Server

Lesson 5 – Data Manipulation and Functions

• Basic Select statement


• Where, Group By, Order by & Having clauses
• Aggregate functions - SUM, Count, AVG, Max and Min
Where Clause
Filter the result from a select statement.

SELECT column_name, column_name


FROM table_name
WHERE column_name operator value;

Order By
The ORDER BY keyword is used to sort the result-set by one or more columns.
SELECT column_name, column_name
FROM table_name
ORDER BY column_name ASC|DESC, column_name ASC|DESC;

Group By
The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns.

SELECT column_name, aggregate_function(column_name)


FROM table_name
WHERE column_name operator value
GROUP BY column_name;
HAVING Clause
The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions.

SELECT column_name, aggregate_function(column_name)


FROM table_name
WHERE column_name operator value
GROUP BY column_name
HAVING aggregate_function(column_name) operator value;
Microsoft SQL Server

Lesson 6 – Null Value Treatment and Union Operator

• Ways to Replace Null Value


• ISNULL Function
• Case Statement
• COALESCE() Function
• Union and Union All
• Difference between Union and Joins
• Aliases
Syntax of ISNULL Function:

Replaces NULL with the specified replacement value.

ISNULL ( check_expression , replacement_value )

Syntax of COALESCE Function:

COALESCE( expression1, expression2, ... expression_n )

Where Expression1 to Expression_n is expression to test the null value.

Difference between COALESCE and ISNULL:

In COALESCE function we can pass more than two parameters whereas in ISNULL function only supports two
parameters.
Difference Between a Joins and UNION

Joins and Unions can be used to combine data from one or more tables.

Joins combine data into new columns. If two tables are joined together, then the data from the first table is shown in
one set of column alongside the second table’s column in the same row.

Unions combine data into new rows. If two tables are “unioined” together, then the data from the first table is in one
set of rows, and the data from the second table in another set. The rows are in the same result.
Microsoft SQL Server

Lesson 7 – Data based objects creation(DDL Commands)

• Create Index statements


• Cluster and Non Clustered
• Unique and Non Unique
• Insert, Update & Delete statements
• Drop & Truncate statements – Uses & Differences
• Alter Command
Indexes
An index can be created in a table to find data more quickly and efficiently.
The users cannot see the indexes, they are just used to speed up searches/queries.

When there are thousands of records in a table, retrieving information will take a long time.
Therefore indexes are created on columns which are accessed frequently, so that the
information can be retrieved quickly. Indexes can be created on a single column or a group
of columns. When a index is created, it first sorts the data and then it assigns a ROWID for
each row.

SQL CREATE INDEX Syntax


Creates an index on a table. Duplicate values are allowed:
CREATE INDEX index_name
ON table_name (column_name)

Create Index IN_Tblemployee


On employeetbl (Region)
Clustered
• Clustered indexes sort and store the data rows in the table or view based on their key values. These are the
columns included in the index definition. There can be only one clustered index per table, because the data
rows themselves can be sorted in only one order.
• The only time the data rows in a table are stored in sorted order is when the table contains a clustered index.
When a table has a clustered index, the table is called a clustered table. If a table has no clustered index, its data
rows are stored in an unordered structure called a heap.

Non clustered
• Non clustered indexes have a structure separate from the data rows. A non clustered index contains the non
clustered index key values and each key value entry has a pointer to the data row that contains the key value.
• The pointer from an index row in a non clustered index to a data row is called a row locator. The structure of the
row locator depends on whether the data pages are stored in a heap or a clustered table. For a heap, a row
locator is a pointer to the row. For a clustered table, the row locator is the clustered index key.
• You can add nonkey columns to the leaf level of the non clustered index to by-pass existing index key limits, 900
bytes and 16 key columns, and execute fully covered, indexed, queries. For more information, see Create
Indexes with Included Columns.
Microsoft SQL Server

Lesson 8 – Optimizing Work

• Sub Queries
• Sub Queries Examples – Where, From Clause and Select statement
• Temp Tables
• Local Temporary Tables
• Global Temporary Tables
• Stored Procedures
• Optimizing for Composite keys & Non-numeric Primary keys
Sub Queries
Sub Queries also called as Nested Subqueries. A subquery is a query within a query. You can create subqueries
within your SQL statements. These subqueries can reside in the WHERE clause, the FROM clause, or the
SELECT clause.

With WHERE clause

Most of the time we create query with where clause.

For Example:

Select E.Email_ID, E.Employee_Name


from Employeetbl E
where E.Department_ID In
(Select D.DepartmentID from Departmenttbl as D
where Department_ID < 4)
With From clause

When we write subqueries with Form clause we called them inline views.

For Example:

Select Max(a.TotalCount)
from (Select Sum(Salary) as TotalCount from Employeetbl group by Region) as a

With Select

A subquery can also be found in the SELECT clause. These are generally used when you wish to retrieve a calculation using
an aggregate function such as the SUM, COUNT, MIN, or MAX function

For Example:

Select Department_Name,
(Select Count(EmployeeID) from Employeetbl where Department_ID = Departmenttbl.DepartmentID)
from Departmenttbl

You might also like