2778A_01

download 2778A_01

of 37

Transcript of 2778A_01

  • 7/31/2019 2778A_01

    1/37

    Module 1: Getting Startedwith Databases and

    Transact-SQL in SQLServer 2008

  • 7/31/2019 2778A_01

    2/37

    Module 1: Getting Started with Databases andTransact-SQL in SQL Server 2008

    Overview of SQL Server 2008

    Overview of SQL Server Databases

    Overview and Syntax Elements of T-SQL

    Working with T-SQL Scripts

    Using T-SQL Querying Tools

  • 7/31/2019 2778A_01

    3/37

    Lesson 1: Overview of SQL Server 2008

    Overview of Client/Server Architecture

    SQL Server Components

    SQL Server Management Tools

    SQL Server Database Engine Components

  • 7/31/2019 2778A_01

    4/37

    Client Computer

    Overview of Client/Server Architecture

    Two-tier

    OLE DB Components

    Multitier

    SQL Database Server

    Client Computer

    SQL Database Server

    Business Logic

    Display Code

    Application Server

    Display Code

    Business Logic

    OLE DB Components

  • 7/31/2019 2778A_01

    5/37

    SQL Server Components

    Server Components Description

    SQL Server Database

    EngineCore service for storing and processing data

    Analysis ServicesTools for creating and managing analytical

    processing

    Reporting ServicesComponents for creating and deploying

    reports

    Integration ServicesTools for moving, copying, and transforming

    data

    The Database Engine also features these components:

    Service Broker

    Full-Text Search Replication

    Notification Services

  • 7/31/2019 2778A_01

    6/37

    SQL Server Management Tools

    Management tools Description

    SQL Server

    Management Studio

    An environment to access, configure,

    manage, and administer SQL components

    SQL Server

    Configuration Manager

    An interface to provide management for

    SQL services, protocols, and client aliases

    SQL Server ProfilerA GUI tool to profile and trace the

    Database Engine and Analysis Services

    Database Engine

    Tuning Advisor

    An application to create an optimal sets

    of indexes, indexed views, and partitions

    Business Intelligence

    Development Studio

    An IDE for creating Analysis Services,

    Reporting Services, and Integration

    Services solutions

  • 7/31/2019 2778A_01

    7/37

    SQL Server Database Engine Components

    Components Description

    ProtocolsWays to implement the external interface to the SQL

    Server

    Relational

    Engine

    Interface into the storage engine, composed of

    services to interact with the underlying database

    storage components and features

    Storage

    Engine

    Core of SQL Server, a highly scalable and available

    service for data storage, processing, and security

    SQLOS

    Operating system with a powerful API, which brings

    together all system components, enabling innovation of

    SQL Servers scalability and performance, providing

    manageability and supportability features

  • 7/31/2019 2778A_01

    8/37

    Lesson 2: Overview of SQL Server Databases

    Overview of Relational Databases

    What Is Normalization?

    The Normalization Process

    SQL Server Database Objects

    Overview of Data Types

  • 7/31/2019 2778A_01

    9/37

    Overview of Relational Databases

    Orders

    OrderID

    CustomerID

    EmployeeID

    OrderDate

    ShippedDate

    ShipVia

    Freight

    Order Details

    OrderID

    ProductID

    UnitPrice

    Quantity

    Products

    ProductID

    ProductName

    SupplierIDUnitPrice

    UnitsInStock

    Discontinued

    Relational databases store data in multiple interrelated tables

    The tables have one-to-many relationships

  • 7/31/2019 2778A_01

    10/37

    What Is Normalization?

    Benefits

    Accelerates sorting and indexing

    Allows more clustered indexes

    Helps UPDATE performance

    More compact databases

    The process for removing redundant data from a database

    Disadvantages

    Insertion of code in tables

    Increase in tables to join Slower data retrieval

    Difficulty in data model query

  • 7/31/2019 2778A_01

    11/37

    First Normal Form

    Second Normal Form

    Third Normal Form

    The Normalization Process

    Order Details

    ProdCategory

    Product1

    Product2

    Order Details

    ProdCat_ID

    ProductID

    Accounts

    Address

    PostCode

    City

    Accounts

    Accountnumber

    Address

    PostCode

    PostCode

    PostCode

    City

    State

    Orders

    Quantity

    Price

    Total

    Orders

    Quantity

    Price

  • 7/31/2019 2778A_01

    12/37

    SQL Server Database Objects

    Objects Notes

    Tables Contain all the data in SQL Server databases

    Views Act like a virtual table or a stored query

    IndexesEnable fast retrieval, built from one or more columns in

    table or view

    TriggersExecute a batch of SQL code when an insert, update or

    delete command is executed against a specific table

    Procedures Accept parameters, contain statements, and return values.

    Constraints Prevent inconsistent data from being placed in a column

    Rules Specify acceptable values that can be inserted in column

  • 7/31/2019 2778A_01

    13/37

    Overview of Data Types

    Approximate numerics

    Date and time

    Exact numerics Unicode character strings

    Binary strings

    Other data types

    Character strings

    Data types are organized into the following categories:

    Each column, variable, expression, parameter has a data type

    A data type specifies the type of data the object can hold:

    integers, characters, monetary data, date and time, binary

  • 7/31/2019 2778A_01

    14/37

    Lesson 3: Overview and Syntax Elements of T-SQL

    A History and Definition of ANSI SQL and T-SQL

    Categories of SQL Statements

    Introduction to Basic T-SQL Syntax

    Types of T-SQL Operators

    What Are T-SQL Functions?

    What Are T-SQL Variables?

    What Are T-SQL Expressions?

    Control-of-flow Statements

  • 7/31/2019 2778A_01

    15/37

    A History and Definition of ANSI SQL and T-SQL

    ANSI-SQL defined by the American National StandardsInstitute

    Microsoft implementation is T-SQL, or Transact SQL

    Other implementations include PL/SQL and SQL ProceduralLanguage.

    Developed in the early 1970s

  • 7/31/2019 2778A_01

    16/37

    GRANT CONTROL SERVER TO user_name;

    Categories of SQL Statements

    DML Data Manipulation Language

    SELECT ProductID, Name, ListPriceFROM Production.Product

    COMMIT TRANSACTION Inner2;

    CREATE USER user_name

    INSERT Table1 VALUES (1, 'Row #1')

    DQL - SQL Select Statements

    DCL Data Control Language

    DDL Data Definition Language

    TCL - Transactional Control Language

    GRANT CONNECT TO guest;

  • 7/31/2019 2778A_01

    17/37

    Introduction to Basic T-SQL Syntax

    SELECT ProductID, Name, ListPriceFROM Production.ProductWHERE ListPrice > $40ORDER BY ListPrice ASC

    There are four primary properties to the SELECT statement

    The number and attributes of the columns in the result set1

    The tables from which the result set data is retrieved2

    The conditions the rows in the source tables must meet3

    The sequence which the rows of the result set are ordered4

  • 7/31/2019 2778A_01

    18/37

    Types of T-SQL Operators

    Type Operators

    Arithmetic operators +, -, *, /, %Vacation + SickLeave AS 'Total PTO'

    Assignment operator =SET @MyCounter = 1

    Comparison operators =, , , !, >=,

  • 7/31/2019 2778A_01

    19/37

    What Are T-SQL Functions?

    Functions Notes

    Rowset Return objects that can be used as table references

    Examples: CONTAINSTABLE, OPENDATASOURCE, OPENQUERY

    Aggregate Operate on a collection but returns a single value

    Examples: AVG, CHECKSUM_AGG, SUM, COUN

    Ranking Return a ranking value for each row in a partition

    Examples: RANK, DENSE_RANK

    Scalar Operate on a single value and then return a single value

    Examples: CREATE FUNCTION dbo.ufn_CubicVolume

  • 7/31/2019 2778A_01

    20/37

    What Are T-SQL Variables?

    declare @food varchar(20)set @food = 'ice cream'WHERE Description = @food

    DECLARE @local_variable as

    Variables in batches and scripts are typically used to:

    Count the number of times a loop is performed

    Hold data to be tested by a control-of-flow statement

    Save data values to be returned by a function return value

    A Transact-SQL local variable is an object that can hold asingle data value of a specific type

  • 7/31/2019 2778A_01

    21/37

    What Are T-SQL Expressions?

    Expressions can be combined if one of these is true:

    The expressions have the same data type

    The data type with the lower precedence can be converted

    to the data type with the higher precedence

    Symbols and operators evaluated to obtain a single value

    SELECT ProductID, Variable_N +2

  • 7/31/2019 2778A_01

    22/37

    Control-of-flow Statements

    These are the control-of-flow keywords:

    GOTO

    IF...ELSE

    BEGIN...END BREAK

    CONTINUE

    WHILE

    RETURN WAITFOR

    IF Boolean_expression

    BEGIN{ sql_statement | statement_block }ENDELSE

    { sql_statement | statement_block }

  • 7/31/2019 2778A_01

    23/37

    Lesson 4: Working with T-SQL Scripts

    What Are Batch Directives?

    Structured Exception Handling

    Commenting T-SQL Code

  • 7/31/2019 2778A_01

    24/37

    What Are Batch Directives?

    IF @cost

  • 7/31/2019 2778A_01

    25/37

    Structured Exception Handling

    BEGIN TRY-- Generate divide-by-zero error.SELECT 1/0;

    END TRYBEGIN CATCH

    -- Execute error retrieval routine.

    EXECUTE usp_GetErrorInfo;END CATCH;

    RAISERROR (N'This is message %s %d.', -- Message text.

    10, -- Severity,1, -- State,N'number', -- First argument.5); -- Second argument.

    -- The message text returned is: This is message number 5.GO

    TRY/CATCH

    RAISERROR

  • 7/31/2019 2778A_01

    26/37

    Commenting T-SQL Code

    The use of a beginning /* and ending */ creates comments

    The double dash comments to the end of line

    /*This is a comment

    */

    --This is a comment

    Comments are statements about the meaning of the code

    When used, there is no execution performed on the text

    There are two ways to comment code using T-SQL:

  • 7/31/2019 2778A_01

    27/37

    Lesson 5: Using T-SQL Querying Tools

    Tools for Querying SQL Server 2008 Databases

    An Introduction to SQL Server Management Studio

    What Is a SQL Server Solution?

    Creating SQL Server Solutions

    Executing Queries in SQL Server Management Studio

    Generating Reports in Microsoft Office Excel

  • 7/31/2019 2778A_01

    28/37

    Tools for Querying SQL Server 2008 Databases

    Tool Description

    SQL ServerManagement Studio

    Used for interactive creation of T-SQL scripts

    To access, configure, manage, and create

    many other SQL Server Objects

    Microsoft Office Excel A spreadsheet used by financial and

    business professional to retrieve data

    SQLCMD

    A command used by administrators for

    command line and batch files processing

    SQLCMD S server\instance i C:\script

    PowerShell An environment used by administrators forcommand line and batch processing

  • 7/31/2019 2778A_01

    29/37

    An Introduction to SQL Server Management Studio

    Support for writing and editing queries or scripts

    Integrated source control for solution and script projects

    Supports most administrative tasks for SQL Server

    An integrated Web browser for quick browsing

  • 7/31/2019 2778A_01

    30/37

    SQL Server Management Studio provides two

    containers for managing database projects:

    What is a SQL Server Solution?

    A project is a set of files, plus related metadata

    A solution includes projects and files that define the solution

    When you create a project, a solution is created to contain it

  • 7/31/2019 2778A_01

    31/37

    Creating SQL Server Solutions

    Use these containers to:

    Implement source control on queries and scripts

    Manage settings for your solution

    Solutions contain scripts, queries, connection informationand files that you need to create your database solution

    Handle the details of file management

    Add items useful to multiple projects in to one solution

    Work on miscellaneous files independent from solutions

  • 7/31/2019 2778A_01

    32/37

    Executing Queries in SQL Server Management Studio

    Executing queries occurs when in a query session by:

    Create queries by interactively entering them in a querywindow

    Load a file that contains T-SQL and then executecommands or modify then execute

    Selecting the Execute Icon

    Pressing the F5 key

  • 7/31/2019 2778A_01

    33/37

    Generating Reports in Microsoft Office Excel

    Connect to Database Server1

    Select Database and Table2

    Save Data Connection File3

    Import Data and Select Format4

    Lab: Using SQL Server Management Studio and

  • 7/31/2019 2778A_01

    34/37

    Exercise 1: Explore the components and execute queries

    in SQL Server Management Studio

    Exercise 2: Start and use sqlcmd

    Exercise 3: Generate a report from a SQL Server database

    using Microsoft Office Excel

    Lab: Using SQL Server Management Studio andSQLCMD

    Logon information

    Virtual machine NY-SQL-01User name Student

    Password Pa$$w0rd

    Estimated time: 60 minutes

  • 7/31/2019 2778A_01

    35/37

    Lab Scenario

    You are the database administrator of Adventure Works.

    The Human Resources department requires that yougenerate several reports by using SQL Server 2008. You

    need to generate reports with a list of employee

    addresses sorted by departments or a list of addresses

    of employees residing in the United States. You also

    need to analyze the details of the newly hired employees

    by using a database diagram in SQL Server Management

    Studio.

    To do this, you will explore and execute queries in SQLServer Management Studio, generate reports from the

    AdventureWorks database, and examine the database

    diagram in SQL Server Management Studio.

  • 7/31/2019 2778A_01

    36/37

    Lab Review

    Why would we execute a T-SQL command using sqlcmd?

    What tools did we use in the lab to query the database?

    How is Excel useful when querying SQL Server 2008databases?

  • 7/31/2019 2778A_01

    37/37

    Module Review and Takeaways

    Review Questions

    Best Practices

    Tools