Introduction to SQL Server 2008

download Introduction to SQL Server 2008

of 21

Transcript of Introduction to SQL Server 2008

  • 8/2/2019 Introduction to SQL Server 2008

    1/21

    Introduction to Microsoft SQL Server 2008Introduction to Microsoft SQL Server 2008

    - Pradeep K Dash

  • 8/2/2019 Introduction to SQL Server 2008

    2/21

    2

    SQL Server 2008

    Manage any data, any place, anytime.

    Store data from structured, semi-structured, and unstructureddocuments, such as images andrich media, directly within thedatabase.

    SQL Server 2008 delivers a rich set

    of integrated services that enableyou to do more with your datasuch as query, search, synchronize,

    report, and analyze.

  • 8/2/2019 Introduction to SQL Server 2008

    3/21

    3

    SQL Server 2008 New Features

    SQL Server Integration Service:

    SSIS (SQL Server Integration Services) is a built in application for developing andexecuting ETL (extraction, transformation, and load) packages.

    SSIS replaced SQL 2000 DTS. Integration Services includes the necessary wizards,tools, and tasks for creating both simple import export packages, as well very

    complex data cleansing operations.

    FILESTREAM Data Type:

    My favorite new feature is the FILESTREAM data type. It takes binary large objects(BLOBs), such as the varchar (MAX) data type, out of the database files and movesthem to standalone files on the Windows file system. This helps cut down on thedatabase size and actually provides great performance (as it turns out, Windows is

    pretty good at reading and writing files all by itself).

  • 8/2/2019 Introduction to SQL Server 2008

    4/21

    4

    SQL Server 2008 New Features

    Policy-Based Management:

    Makes managing multiple SQL Server instances easier. It allows you to definecentralized configuration policies, and SQL Server will enforce and report on thosesett ings throughout the enterprise. Policy-Based Management, which requires nochanges to your applications or databases to function, also allows you to run a T-

    SQL query against multiple servers from a centralized management server. This is(sort of) a part of the Policy-Based Management framework.

    Transparent data encryption:

    If you deal with sensitive data, transparent data encryption improves SQL Server'sencryption capabilities by transparently encrypting the entire database file ratherthan specific columns.

    Data auditing: Finally, if you're dealing with compliance and auditing concerns, the new data

    auditing features help audit events like logons, password changes, data access,data modification, schema modification and more.

  • 8/2/2019 Introduction to SQL Server 2008

    5/21

    5

    SQL Server 2008 New Features

    MERGE:

    SQL 2008 includes the TSQL command MERGE. Using this statement allows asingle statement to UPDATE, INSERT, or DELETE a row depending on its condit ion.

    MERGE InventoryMaster AS im

    USING (SELECT InventoryID, Descr FROM NewInventory) AS src

    ON im. InventoryID = src. InventoryIDWHEN MATCHED THEN

    UPDATE SET im.Descr = src.Descr

    WHEN NOT MATCHED THEN

    INSERT (InventoryID, Descr) VALUES (src. InventoryID, src.Descr);

    Declare and Initialize Variables

    Microsoft SQL Server2008 enables you to initialize variables inline as part of thevariable declaration statement instead of using separate DECLAREand SET

    statements. This enhancement helps you abbreviate your code. The followingcode example demonstrates inline initializations using a literal and a function:

    DECLARE @i AS INT = 0, @d AS DATETIME = CURRENT_TIMESTAMP;

    SELECT @i AS [@i], @d AS [@d];

  • 8/2/2019 Introduction to SQL Server 2008

    6/21

    6

    SQL Server 2008 New Features

    Compound Assignment Operators

    Compound assignment operators help abbreviate code that assigns a value to acolumn or a variable. The new operators are:

    += (plus equals)

    -= (minus equals)

    *= (multiplication equals)

    /= (division equals)

    %= (modulo equals)

    You can use these operators wherever assignment is normally allowedforexample, in the SET clause of an UPDATE statement or in a SET statement that

    assigns values to variables. The following code example demonstrates the use ofthe += operator:DECLARE @price AS MONEY = 10.00;

    SET @price += 2.00;

    SELECT @price;

    This code sets the variable @price to its current value, 10.00, plus 2.00, resultingin 12.00.

  • 8/2/2019 Introduction to SQL Server 2008

    7/21

    7

    SQL Server 2008 New Features

    Table Value Constructor Support through the VALUES Clause

    SQL Server 2008 introduces support for table value constructors through theVALUES clause. You can now use a single VALUES clause to construct a set of rows.One use of this feature is to insert multiple rows based on values in a singleINSERT statement, as follows:

    CREATE TABLE dbo.Customers(

    custid INT NOT NULL, companynameVARCHAR(25) NOT NULL,

    phone VARCHAR(20) NOT NULL, address VARCHAR(50) NOTNULL,

    CONSTRAINT PK_CustomersPRIMARY KEY(custid)

    );

    INSERT INTO dbo.Customers(custid, companyname, phone, address)VALUES

    (1, 'cust 1', '(111) 111-1111', 'address 1'),

    (2, 'cust 2', '(222) 222-2222', 'address 2'),

    (3, 'cust 3', '(333) 333-3333', 'address 3');

  • 8/2/2019 Introduction to SQL Server 2008

    8/21

    8

    SQL Server 2008 New Features

    To read more about SQL Server 2008 new features like new Date data types,enhanced CONVERT and CAST features etc visit, http://technet.microsoft.com/en-us/ library/cc721270.aspx

  • 8/2/2019 Introduction to SQL Server 2008

    9/21

    9

    Database Migration to SQL Server 2008

    Chapter: II

  • 8/2/2019 Introduction to SQL Server 2008

    10/21

    10

    Using Detach and Attach Features of SQL Server

    Detaching a database removes it from the instance of the Microsoft SQL Server DatabaseEngine but leaves intact the database, with its data files and transaction log files.

    After detaching a SQL Server 2005 database, you can reattach it to the same or anotherinstance of SQL Server 2005.

    To detach a database

    In SQL Server Management Studio Object Explorer, connect to the instance of the SQLServer Database Engine and then expand the instance.

    Expand Databases, and select the name of the user database you want to detach.

    Detaching a database requires exclusive access to the database. If the database is in use,restrict access to a single user:

    Right-click the database name and point to Properties.

    In the Select a page pane, select Options.

    In the Other options pane, scroll down to the State options.

    Select the Restrict Access option, and in its drop-down list, select Single.

    Click OK.

    A message box appears to inform you that this action will close all connections to thedatabase. To proceed, click OK.

    Right-click the database name, point to Tasks, and then click Detach. The Detach

    Database dialog box appears.

  • 8/2/2019 Introduction to SQL Server 2008

    11/21

    11

    Using Detach and Attach Features of SQL Server

    The Databases to detach grid displays the name of the selected database in theDatabase Name column. Verify that this is the database you want to detach.

    By default, the detach operation retains any out-of-date optimization statistics whendetaching the database; to update the existing optimization statistics, click the UpdateStatistics check box.

    By default, the detach operation keeps any full-text catalogs that are associated withthe database. To remove them, clear the Keep Full-Text Catalogs check box.

    The Status column displays the current database state (either Ready or Not Ready).

    If the status is Not Ready, the Message column displays hyperlinked information aboutthe database. When a database is involved with replication, the Message columndisplays Database replicated. When a database has one or more active connections, theMessage column displays Active connections; forexample, 1 Active connection(s). Before you can detach the database, you mustdisconnect any active connections by selecting the Drop Connections check box.

    To obtain more information about a message, click the hyperlink.

    When you are ready to detach the database, click OK.

  • 8/2/2019 Introduction to SQL Server 2008

    12/21

    12

    Using Detach and Attach Features of SQL Server

    Attaching a database places it in exactly the same state that it was in when it wasdetached.

    To attach a database

    In SQL Server Management Studio Object Explorer, connect to an instance of theMicrosoft SQL Server Database Engine, and then expand that instance.

    Right-click Databases and click Attach. In the Attach Databases dialog box, to specify the database to be attached, click Add;

    and in the Locate Database Files dialog box, select the disk drive where the databaseresides and expand the directory tree to find and select the .mdf file of the database; forexample:

    C:\Program Files\Microsoft SQLServer\MSSQL.1\ MSSQL\DATA\AdventureWorks_Data.mdf

    Important: Trying to select a database that is already attached generates an error.

    Optionally, to specify a different name for the database to attach as, enter the name inthe Attach as column of the Attach Databases dialog box.

    Optionally, change the owner of the database by selecting a different entry in theOwner column.

    When you are ready to attach the database, click OK.

  • 8/2/2019 Introduction to SQL Server 2008

    13/21

  • 8/2/2019 Introduction to SQL Server 2008

    14/21

    14

    Using Copy Database Wizard Feature

    The Copy Database Wizard lets you move or copy databases and their objectseasily from one server to another, with no server downtime. Using this wizard, youcan do the following:

    Pick a source and destination server.

    Select databases to move or copy.

    Specify the file location for the databases.

    Create logins on the destination server.

    Copy additional supporting objects, jobs, user-defined stored procedures, anderror messages.

    Schedule when to move or copy the databases.

    In addit ion to copying databases, you can copy associated metadata, for example,logins and objects from the master database that are required by a copieddatabase.

    Also, you can move and copy databases between different instances of SQL

    Server, and you can upgrade databases from SQL Server 2000 to SQL Server 2005or later. The destination server must be SQL Server 2005 or later.

  • 8/2/2019 Introduction to SQL Server 2008

    15/21

    15

    Demonstration

    Demonstration Session 7

  • 8/2/2019 Introduction to SQL Server 2008

    16/21

    16

    Back Up and Restore

    Back up the database in the old server and restore it in the destination server.

    Microsoft SQL Server enables you to back up and restore your databases. The SQLServer backup and restore component provides an important safeguard forprotecting crit ical data stored in SQL Server databases. A well-planned backup andrestore strategy helps protect databases against data loss caused by a variety of

    failures. A copy of data that can be used to restore and recover the data is called a backup.

    Backups let you restore data after a failure. With good backups, you can recover

    from many failures, such as:

    Media failure.

    User errors, for example, dropping a table by mistake.

    Hardware failures, for example, a damaged disk drive or permanent loss of a server. Natural disasters.

    Additionally, backups of a database are useful for routine administrative purposes,such as copying a database from one server to another, sett ing up databasemirroring, and archiving.

  • 8/2/2019 Introduction to SQL Server 2008

    17/21

    17

    Demonstration

    Demonstration Session 8

  • 8/2/2019 Introduction to SQL Server 2008

    18/21

    18

    Create Script and Run in New Database Server

    The last type is to generate the create script using Generate Script Wizard (SSMS)and execute it in the destination server.

    Select the database in the source server in SSMS

    Right click ->Tasks->Generate Scripts Wizard to launch the wizard.

    Select the various scripting options needed and select the objects needed to

    generate the scripts for them. Make sure script data = true in the scripting optionto generate script for data as well (INSERT statements)

    click next ->next and finish to generate the script (new query window or clip boardor file)

    connect to the destination server and create the new database in it .

    Click new query window and paste the script generated using GSW above andexecute them with the destination database context.

  • 8/2/2019 Introduction to SQL Server 2008

    19/21

    19

    Demonstration

    Demonstration Session 9

  • 8/2/2019 Introduction to SQL Server 2008

    20/21

    20

  • 8/2/2019 Introduction to SQL Server 2008

    21/21

    Thank You!