module 03_creating and managing databases

Upload: sathish-kumar-r

Post on 29-May-2018

229 views

Category:

Documents


1 download

TRANSCRIPT

  • 8/9/2019 Module 03_Creating and Managing Databases

    1/31

    Module 3:Creating and Managing

    Databases

    Vidya Vrat Agarwal. | MCT, MCSD

  • 8/9/2019 Module 03_Creating and Managing Databases

    2/31

    SQL Server Database

    A Database is a collection of Tables and Objects such as Views,Triggers, Stored Procedures.

    The Data and Objects in a database are stored as a set of Operating

    System files.

    The types of files that are used to store a database in SQL Server are:

    Primary

    Secondary

    Transaction Log

    A database must consist of a Primary data file and one Transaction

    log file.

    The Primary data file can be used for database objects, and

    Secondary data files can be used to store User data and objects.

    Contd..

  • 8/9/2019 Module 03_Creating and Managing Databases

    3/31

    SQL Server Database

    Database need not have Secondary data file, if the PrimaryData file is Large Enough to hold the data in Database .

    The Transaction log records all the modification that haveoccurred in the database and which transaction performed

    the modifications. E.g-Insert, Update, Delete.The Transaction log files hold the log information used torecover a database. There can be more than onetransaction log file.

    The Primary data file has a .mdfextensionThe Secondary data file has a .ndfextension

    The Transaction log file has a .ldfextension

  • 8/9/2019 Module 03_Creating and Managing Databases

    4/31

    Creating Database

    CREATE DATABASE Sample

    ONPRIMARY ( NAME = SampleData,FILENAME ='c:\Program Files\Data\Sample.mdf',SIZE = 10MB,MAXSIZE = 15MB,FILEGROWTH = 20% )

    LOG ON

    ( NAME = SampleLog,FILENAME =c:\Program Files\Data\Sample.ldf',SIZE = 3MB,MAXSIZE = 5MB,FILEGROWTH = 1MB )

    CREATE DATABASE Sample

    ONPRIMARY ( NAME = SampleData,FILENAME ='c:\Program Files\Data\Sample.mdf',SIZE = 10MB,MAXSIZE = 15MB,FILEGROWTH = 20% )

    LOG ON( NAME = SampleLog,FILENAME =c:\Program Files\Data\Sample.ldf',SIZE = 3MB,MAXSIZE = 5MB,FILEGROWTH = 1MB )

    Creating a Database Defines:The name of the databaseThe size of the databaseThe files where the database will reside

  • 8/9/2019 Module 03_Creating and Managing Databases

    5/31

    SizeThis parameter specifies the size of the data or log file. Youcan specify sizes in megabytes (MB)the default valueorkilobytes (KB). The minimum size is 512 KB for both thedata and log file.

    MaxSize

    This parameter specifies the maximum size to which the filecan grow. You can specify sizes in megabytesthe default

    valueor kilobytes.

    If you do not specify a size, the file grows until the disk isfull.

  • 8/9/2019 Module 03_Creating and Managing Databases

    6/31

    FileGrowthThis parameter specifies the growth increment of the file.

    The FILEGROWTH setting for a file cannot exceed theMAXSIZE setting. A value of 0 indicates no growth.

    The value can be specified in megabytesthe defaultinkilobytes, or as a percentage (%).

    The default value if FILEGROWTH is not specified is 10percent, and the minimum value is 64 KB (one extent).

    The specified size is rounded to the nearest 64 KB. in three

    ways: in megabytes, in kilobytes, or as a percentage. Thepercentage only applies to file growth, not maximum size.

  • 8/9/2019 Module 03_Creating and Managing Databases

    7/31

    Retrieving Database Information

    Determine Database Properties by Using theDATABASEPROPERTYEX Function

    Use System Stored Procedures to Display InformationAbout Databases and Database Parameters

    sp_helpdb

    sp_helpdb database_name

    sp_spaceused [objname]

  • 8/9/2019 Module 03_Creating and Managing Databases

    8/31

    FileGroups

    A FileGroup is a Collection of files. The Files may or may notbe on the same Disk Drive.

    Purpose

    FileGroups improve the performance of the database .

    For Better database performance, files and filegroups shouldbe created on different disks.

  • 8/9/2019 Module 03_Creating and Managing Databases

    9/31

    Filegroups

    Northwind Database

    Default Filegroup OrderHistoryGroup

    sys...sys...sys...

    sys...sys...sys...

    sysuserssysuserssysusers

    sysobjectssysobjectssysobjects

    .........

    OrdersOrdersOrders

    CustomersCustomersCustomers

    ProductsProductsProducts

    OrdHistYear2OrdHistYear2OrdHistYear2

    OrdHistYear1OrdHistYear1OrdHistYear1

    Northwind.mdfNorthwind.mdf

    C:\ D:\

    OrdHist1.ndfOrdHist1.ndf

    OrdHist2.ndfOrdHist2.ndf Northwind.IdfNorthwind.Idf

    E:\

  • 8/9/2019 Module 03_Creating and Managing Databases

    10/31

    Managing Data and Log File Growth

    ALTER DATABASE Sample

    MODIFY FILE ( NAME = 'SampleLog',SIZE = 15MB)

    GO

    ALTER DATABASE Sample

    ADD FILE

    ( NAME = SampleData2,FILENAME ='c:\Program Files\..\..\

    Data\Sample2.ndf',

    SIZE = 15MB,MAXSIZE = 20MB )

    GO

    ALTER DATABASE Sample

    MODIFY FILE ( NAME = 'SampleLog',SIZE = 15MB)GO

    ALTER DATABASE Sample

    ADD FILE

    ( NAME = SampleData2,FILENAME ='c:\Program Files\..\..\

    Data\Sample2.ndf',

    SIZE = 15MB,MAXSIZE = 20MB )

    GO

    Using Automatic File Growth Expanding Database Files

    Adding Secondary Database Files

  • 8/9/2019 Module 03_Creating and Managing Databases

    11/31

    Renaming a Database

    The database should not be in use when it is being renamed .

    sp_renamedb old_Dbname, new_Dbname

    Example sp_renamedb employee,emp

  • 8/9/2019 Module 03_Creating and Managing Databases

    12/31

    Dropping a Database

    DROP DATABASE Northwind, pubsDROP DATABASE Northwind, pubs

    Methods of Dropping a Database

    SQL Server Enterprise Manager

    DROP DATABASE statement

    Restrictions on Dropping a Database

    When a user is connected to it If it is a system database

  • 8/9/2019 Module 03_Creating and Managing Databases

    13/31

    DatabaseDatabase

    How Data Is Stored

    Extent

    (8 contiguous8-KB pages)

    Page (8 KB)

    Tables,

    Indexes

    Data

    1 2 3 5 6 7 8

    4

    Data (file)

    .mdf or .ndf

    Log (file)

    .Idf

  • 8/9/2019 Module 03_Creating and Managing Databases

    14/31

    SQL Server stores, reads, and writes data in 8-KBblocks of contiguous disk space called Pages.

    All pages are stored in extents. An extent is eightcontiguous pages, or 64 KB.

    This means that a database can store 128 pages permegabyte.

    1 MB = 1024 KiloBytes

    1024 KB / 8 KB (Page) = 128 Pages per MB

    1024 KB / 64 Kb (Extent) = 16 Extents per MB

  • 8/9/2019 Module 03_Creating and Managing Databases

    15/31

    Pages

    Pages and extents are the primary data structures in theSQL Server physical database.

    Types of Pages

    SQL Server uses several types of pages:

    some track space allocation, and

    some contain user and index data.

  • 8/9/2019 Module 03_Creating and Managing Databases

    16/31

    Extents

    Types of Extents

    SQL Server uses two types of extents:

    Mixed Extents

    Extents that contain pages from two or more objects arecalled Mixed Extents.

    Every table starts as a mixed extent. Database

    use mixed extents primarily for pages that

    track space and contain small objects.

    MixedExtent

  • 8/9/2019 Module 03_Creating and Managing Databases

    17/31

    Extents

    Uniform Extents

    Extents that have all eight pages allocated to a single objectare called uniform extents. They are used when tables orindexes need more than 64 KB of space.

    UniformExtents

  • 8/9/2019 Module 03_Creating and Managing Databases

    18/31

    Check Your Understanding.

  • 8/9/2019 Module 03_Creating and Managing Databases

    19/31

    Q.1. What are the types of files to storea Database in SQL Server. ?

  • 8/9/2019 Module 03_Creating and Managing Databases

    20/31

    Q.2. The Primary data file has a _______ extension. ?

  • 8/9/2019 Module 03_Creating and Managing Databases

    21/31

    Q.3. The Secondary data file has a __________ extension.

  • 8/9/2019 Module 03_Creating and Managing Databases

    22/31

    Q.4. The Transaction log file has a ________ extension.

  • 8/9/2019 Module 03_Creating and Managing Databases

    23/31

    Q.5. What is the Command to create a Database inSQL Server. ?

  • 8/9/2019 Module 03_Creating and Managing Databases

    24/31

  • 8/9/2019 Module 03_Creating and Managing Databases

    25/31

    Q.7. What is the difference between Name andFilename. ?

  • 8/9/2019 Module 03_Creating and Managing Databases

    26/31

    Q.8. What is the difference between Size andMaxsize. ?

  • 8/9/2019 Module 03_Creating and Managing Databases

    27/31

    Q.9. What is the SQL Statement to delete adatabase. ?

  • 8/9/2019 Module 03_Creating and Managing Databases

    28/31

    Q.10. What is FileGroup and what is its Purpose. ?

  • 8/9/2019 Module 03_Creating and Managing Databases

    29/31

    Q.11. What is the Command to renamea database?

  • 8/9/2019 Module 03_Creating and Managing Databases

    30/31

    Q.12. What is the difference between Mixed andUniform extents .

  • 8/9/2019 Module 03_Creating and Managing Databases

    31/31

    Winners dont dodifferent thingsthey do thingsdifferently.Thank You.