25
Session 7 Creating and Managing Databases

Session 7 Creating and Managing Databases. RDBMS and Data Management/ Session 7/2 of 27 Session Objectives Describe the system and user-defined databases

Embed Size (px)

Citation preview

Page 1: Session 7 Creating and Managing Databases. RDBMS and Data Management/ Session 7/2 of 27 Session Objectives Describe the system and user-defined databases

Session 7

Creating and Managing Databases

Page 2: Session 7 Creating and Managing Databases. RDBMS and Data Management/ Session 7/2 of 27 Session Objectives Describe the system and user-defined databases

RDBMS and Data Management/ Session 7/2 of 27

Session Objectives Describe the system and user-defined

databases in SQL Server 2005. Outline the key features of the

AdventureWorks sample database. Describe how to add filegroups and

transaction logs. List types of database modifications and

describe how to drop a database. Describe database snapshots.

Page 3: Session 7 Creating and Managing Databases. RDBMS and Data Management/ Session 7/2 of 27 Session Objectives Describe the system and user-defined databases

RDBMS and Data Management/ Session 7/3 of 27

SQL Server Supported Databases

SQL Server 2005 supports the following databases:

System Database

User-defined databases

Sample Databases

Created by SQL Server to store information

about SQL Server 2005. Also manages user-defined databases. SQL Server 2005 users create user-

defined databases. Stores user data.

SQL Server provides sample databases for the users to work with SQL Server 2005.

A new sample database, AdventureWorks, has been introduced in SQL Server 2005.

Page 4: Session 7 Creating and Managing Databases. RDBMS and Data Management/ Session 7/2 of 27 Session Objectives Describe the system and user-defined databases

RDBMS and Data Management/ Session 7/4 of 27

System Databases

SQL Server 2005 provides and supports these system databases:

Database Description

master The database records all system-level information of an instance of SQL Server.

msdb The database is used by SQL Server Agent for scheduling database alerts and various jobs

model The database is used as the template for all databases to be created on the particular instance of SQL Server 2005.

resource The database is a read-only database. It contains system objects included with SQL Server 2005.

tempdb The database holds temporary objects or intermediate result sets.

Page 5: Session 7 Creating and Managing Databases. RDBMS and Data Management/ Session 7/2 of 27 Session Objectives Describe the system and user-defined databases

RDBMS and Data Management/ Session 7/5 of 27

Modifying System Data

Users are not allowed to directly manipulate the information in system tables, system stored procedures, and catalog views.

Users can administer the system and manage all users and database objects through:

SQL Server Management Studio SQL Server Management Objects (SMO) application

programming interface Transact-SQL scripts and stored procedures

modify System data

Page 6: Session 7 Creating and Managing Databases. RDBMS and Data Management/ Session 7/2 of 27 Session Objectives Describe the system and user-defined databases

RDBMS and Data Management/ Session 7/6 of 27

Viewing System Data

Database applications view catalog and system information by using any of the following:

System catalog views SQL Server management objects Catalog functions, methods, attributes, or properties of the

data API Stored Procedures and Functions

EttreeryreyrErtetertetet

tettwet

Viewing System data

Page 7: Session 7 Creating and Managing Databases. RDBMS and Data Management/ Session 7/2 of 27 Session Objectives Describe the system and user-defined databases

RDBMS and Data Management/ Session 7/7 of 27

User-defined Databases The information needed to create a user-defined database:

Name of the database Owner of the database Size of the database Files and Filegroups used to store it

CREATE DATABASE database_name [ ON [ PRIMARY ] [ <filespec> [ ,...n ] [ , <filegroup> [ ,...n ] ] [ LOG ON { <filespec> [ ,...n ] } ] ] [ COLLATE collation_name ] ][;]

Syntax for creating a database:

Page 8: Session 7 Creating and Managing Databases. RDBMS and Data Management/ Session 7/2 of 27 Session Objectives Describe the system and user-defined databases

RDBMS and Data Management/ Session 7/8 of 27

User-defined DatabasesUser database size can be expanded, shrunk or be dropped as per the need of the user.

ALTER DATABASE database_name{<add_or_modify_files>| <add_or_modify_filegroups>| <set_database_options>| MODIFY NAME = new_database_name| COLLATE collation_name}[;]

The ownership of a user database can be changed using spchangedbowner stored procedure.

sp_changedbowner [ @loginame = ] ‘login’

Syntax for modifying a database:

Syntax for changing the owner using sp_changedbowner:

Page 9: Session 7 Creating and Managing Databases. RDBMS and Data Management/ Session 7/2 of 27 Session Objectives Describe the system and user-defined databases

RDBMS and Data Management/ Session 7/9 of 27

User-defined Databases For each database, the database options are unique. The database options can be set using the SET clause of the

ALTER DATABASE statement. Following table shows details of options for SQL Server 2005:

Option Type Description

Automatic options

Controls automatic behavior of database.

Cursor options Controls cursor behavior.

Recovery options

Controls recovery models of database.

State options Controls American National Standards Institute (ANSI) compliance.

Page 10: Session 7 Creating and Managing Databases. RDBMS and Data Management/ Session 7/2 of 27 Session Objectives Describe the system and user-defined databases

RDBMS and Data Management/ Session 7/10 of 27

User-defined Databases Example for creating database:

CREATE DATABASE [Customer_DB] ON PRIMARY( NAME = N’Customer_DB’, FILENAME = N’C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\Customer_DB.mdf’)LOG ON( NAME = N’Customer_DB_log’, FILENAME = N’C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\Customer_DB_log.ldf’)COLLATE SQL_Latin1_General_CP1_CI_AS

Example for modifying database:

ALTER DATABASE Customer_DB MODIFY NAME = CUST_DB

Page 11: Session 7 Creating and Managing Databases. RDBMS and Data Management/ Session 7/2 of 27 Session Objectives Describe the system and user-defined databases

RDBMS and Data Management/ Session 7/11 of 27

User-defined Databases Example for changing database owner:

USE CUST_DBEXEC sp_changedbowner ‘sa’

USE CUST_DB;ALTER DATABASE CUST_DBSET AUTO_SHRINK ON

Example for setting database options:

Page 12: Session 7 Creating and Managing Databases. RDBMS and Data Management/ Session 7/2 of 27 Session Objectives Describe the system and user-defined databases

RDBMS and Data Management/ Session 7/12 of 27

AdventureWorks Databases AdventureWorks is the new addition to SQL Server

2005 sample databases. Adventure Works Cycles is a fictitious large,

multinational cycle manufacturing company introduced as a scenario in the database.

The sample database consists of the following parts: AdventureWorks: Sample Online Transaction

Processing (OLTP) database AdventureWorksDW: Sample Data warehouse AdventureWorksAS: Sample Analysis Services

database

Page 13: Session 7 Creating and Managing Databases. RDBMS and Data Management/ Session 7/2 of 27 Session Objectives Describe the system and user-defined databases

RDBMS and Data Management/ Session 7/13 of 27

Filegroups and Transaction Logs 1-7

FILES: Actual physical storage units Contain database objects Files have a logical and a physical

name Filegroups:

Collections of files that are grouped for better performance and easier management.

A single logical filegroup can contain multiple physical files.

Each filegroup is used to group related files that together store a database object.

Database objects like tables and indexes can be created on specific filegroups.

Filegroups can be taken as back ups and restored separately.

The following table shows the details of filegroups supported by SQL Server 2005:

Page 14: Session 7 Creating and Managing Databases. RDBMS and Data Management/ Session 7/2 of 27 Session Objectives Describe the system and user-defined databases

RDBMS and Data Management/ Session 7/14 of 27

Filegroups and Transaction Logs 2-7

Filegroup Description

Primary The filegroup that consists of the primary file. All system tables are placed inside the primary filegroup.

User-defined Any filegroup that is created by the user at the time of creating or modifying databases.

Adding Filegroups to database: Filegroups can be added for a new database at the

time of database creation or for an existing database. A file cannot be a member of more than one filegroup. Files cannot be moved to a different filegroup after

files have been added to the database. Filegroups can contain only data files and no

transaction log files.

Page 15: Session 7 Creating and Managing Databases. RDBMS and Data Management/ Session 7/2 of 27 Session Objectives Describe the system and user-defined databases

RDBMS and Data Management/ Session 7/15 of 27

Filegroups and Transaction Logs 3-7

Adding Filegroups during creation of database

Syntax for filegroup addition while creating a database:

CREATE DATABASE database_name[ ON[ PRIMARY ] [ <filespec> [ ,...n ][ , <filegroup> [ ,...n ] ][ LOG ON { <filespec> [ ,...n ] } ]][ COLLATE collation_name ]][;]

Page 16: Session 7 Creating and Managing Databases. RDBMS and Data Management/ Session 7/2 of 27 Session Objectives Describe the system and user-defined databases

RDBMS and Data Management/ Session 7/16 of 27

Filegroups and Transaction Logs 4-7

Adding Filegroups to an existing database

Syntax for filegroup addition to an existing database:

ALTER DATABASE database_name{<add_or_modify_files>| <add_or_modify_filegroups>| <set_database_options>| MODIFY NAME = new_database_name| COLLATE collation_name}[;]

Page 17: Session 7 Creating and Managing Databases. RDBMS and Data Management/ Session 7/2 of 27 Session Objectives Describe the system and user-defined databases

RDBMS and Data Management/ Session 7/17 of 27

Filegroups and Transaction Logs 5-7

Viewing Filegroup Information Database, file, partition, and filegroup properties can

be viewed using a variety of catalog views, system functions, and stored procedures.

Details of these functions are summarized in the table:

Functions Description

DB_ID This function returns the database identification number.

DB_NAME This function returns the database name.

FILE_ID This functions returns the file identification number for a logical file name in the current database

FILE_NAME This function returns the logical name for the given file identification number.

Page 18: Session 7 Creating and Managing Databases. RDBMS and Data Management/ Session 7/2 of 27 Session Objectives Describe the system and user-defined databases

RDBMS and Data Management/ Session 7/18 of 27

Filegroups and Transaction Logs 6-7

Transaction Transaction is a set of operations that must happen

together. Actions are first recorded to transaction log. When that is complete, operations applied to

database. Rollback and data recovery

Recovery of individual transactions Recovery of all incomplete transactions when

SQL Server is started Rolling a restored database, file, filegroup, or

page forward to the point of failure Supporting transactional replication Standby server solutions

TransactioTransaction Log n Log

supportssupports

Page 19: Session 7 Creating and Managing Databases. RDBMS and Data Management/ Session 7/2 of 27 Session Objectives Describe the system and user-defined databases

RDBMS and Data Management/ Session 7/19 of 27

Filegroups and Transaction Logs 7-7

Adding Log files to a database

Syntax for log file addition to a database:

ALTER DATABASE database_name{...}[;]<add_or_modify_files>::={ADD FILE <filespec> [ ,...n ][ TO FILEGROUP { filegroup_name | DEFAULT } ]| ADD LOG FILE <filespec> [ ,...n ]| REMOVE FILE logical_file_name| MODIFY FILE <filespec>}

Page 20: Session 7 Creating and Managing Databases. RDBMS and Data Management/ Session 7/2 of 27 Session Objectives Describe the system and user-defined databases

RDBMS and Data Management/ Session 7/20 of 27

Database Modification Methods 1-2Type of

modificationsModification methods

Increasing the size of a database

ALTER DATABASE statement or the database properties in SQL Server Management Studio.

Changing the physical location of a database

ALTER DATABASE statement.

Adding data or transaction log files

ALTER DATABASE statement or the database properties in SQL Server Management Studio.

Shrinking a database

DBCC SHRINKDATABASE statement or the Shrink Database option in SQL Server Management Studio, accessed through the node for the specific database.

Shrinking a database file

DBCC SHRINKFILE statement.

Deleting data or log files

ALTER DATABASE statement or the database properties in SQL Server Management Studio.

Adding a filegroup to a database

ALTER DATABASE statement or the database properties in SQL Server Management Studio.

Page 21: Session 7 Creating and Managing Databases. RDBMS and Data Management/ Session 7/2 of 27 Session Objectives Describe the system and user-defined databases

RDBMS and Data Management/ Session 7/21 of 27

Database Modification Methods 2-2

Type of modifications

Modification methods

Changing the default filegroup

ALTER DATABASE statement.

Changing database options

ALTER DATABASE statement or the database properties in SQL Server Management Studio.

Changing database owner

sp_changedbowner system stored procedure.

Dropping a database The files and the data associated with the database is

automatically deleted from the disk when the database is deleted.

Syntax:

DROP DATABASE { database_name }

Page 22: Session 7 Creating and Managing Databases. RDBMS and Data Management/ Session 7/2 of 27 Session Objectives Describe the system and user-defined databases

RDBMS and Data Management/ Session 7/22 of 27

Database Snapshots Creating a database snapshot:

Database snapshot is a new feature in Microsoft SQL Server 2005 which provides a read-only, static view of a SQL Server database.

If user makes a mistake in the source database, the source database can be reverted to the previous state when the snapshot was created.

Syntax:CREATE DATABASE database_snapshot_nameON(NAME = logical_file_name,FILENAME = ‘os_file_name’) [ ,...n ]AS SNAPSHOT OF source_database_name[;]

Page 23: Session 7 Creating and Managing Databases. RDBMS and Data Management/ Session 7/2 of 27 Session Objectives Describe the system and user-defined databases

RDBMS and Data Management/ Session 7/23 of 27

Database Snapshots

Database createdDatabase createdSnapshot createdData gets modifiedSnapshot is restored

Page 24: Session 7 Creating and Managing Databases. RDBMS and Data Management/ Session 7/2 of 27 Session Objectives Describe the system and user-defined databases

RDBMS and Data Management/ Session 7/24 of 27

Database Snapshots

Advantages Disadvantages

Provide a convenient, read-only copy of data.

When queried, no deterioration of performance.

Snapshot files are small and are very quick to create.

Snapshot backup cannot be created.

Snapshot must exist on the same database server as that of the source database.

A new user cannot be granted access to the data in a snapshot.

Page 25: Session 7 Creating and Managing Databases. RDBMS and Data Management/ Session 7/2 of 27 Session Objectives Describe the system and user-defined databases

RDBMS and Data Management/ Session 7/25 of 27

Summary SQL Server supports system as well as user-

defined databases. System databases are used to store information

about SQL Server 2005, and manage user databases.

User-defined databases are used to store user information.

User-defined databases can be created using the CREATE DATABASE statement.

SQL Server 2005 filegroups contain data files. A transaction log present in SQL Server 2005

records all transactions and modifications in the database, made by each transaction.

A database snapshot is a read-only, static view of a source database.