Microsoft PowerPoint - MS SQL Server
Microsoft PowerPoint - 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
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.
Machine 1 Machine 2
Server
Machine 3 Machine 4
Microsoft SQL Server
• 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
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
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.
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.
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
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.
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
• 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.
For Example:
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