Download presentation
Presentation is loading. Please wait.
Published byLawrence Parker Modified over 9 years ago
1
Session 7 Creating and Managing Databases
2
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.
3
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.
4
RDBMS and Data Management/ Session 7/4 of 27 System Databases SQL Server 2005 provides and supports these system databases: DatabaseDescription masterThe database records all system-level information of an instance of SQL Server. msdbThe database is used by SQL Server Agent for scheduling database alerts and various jobs modelThe database is used as the template for all databases to be created on the particular instance of SQL Server 2005. resourceThe database is a read-only database. It contains system objects included with SQL Server 2005. tempdbThe database holds temporary objects or intermediate result sets.
5
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
6
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 Ettreeryreyr Ertetertetet tettwet Viewing System data
7
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 ] [ [,...n ] [, [,...n ] ] [ LOG ON { [,...n ] } ] ] [ COLLATE collation_name ] ] [;] Syntax for creating a database:
8
RDBMS and Data Management/ Session 7/8 of 27 User-defined Databases User database size can be expanded, shrunk or be dropped as per the need of the user. ALTER DATABASE database_name { | | 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:
9
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 TypeDescription Automatic optionsControls automatic behavior of database. Cursor optionsControls cursor behavior. Recovery optionsControls recovery models of database. State optionsControls American National Standards Institute (ANSI) compliance.
10
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
11
RDBMS and Data Management/ Session 7/11 of 27 User-defined Databases Example for changing database owner: USE CUST_DB EXEC sp_changedbowner ‘sa’ USE CUST_DB; ALTER DATABASE CUST_DB SET AUTO_SHRINK ON Example for setting database options:
12
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
13
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:
14
RDBMS and Data Management/ Session 7/14 of 27 Filegroups and Transaction Logs 2-7 FilegroupDescription PrimaryThe filegroup that consists of the primary file. All system tables are placed inside the primary filegroup. User-definedAny 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.
15
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 ] [ [,...n ] [, [,...n ] ] [ LOG ON { [,...n ] } ] ] [ COLLATE collation_name ] ] [;]
16
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 { | | MODIFY NAME = new_database_name | COLLATE collation_name } [;]
17
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: FunctionsDescription DB_IDThis function returns the database identification number. DB_NAMEThis function returns the database name. FILE_IDThis functions returns the file identification number for a logical file name in the current database FILE_NAMEThis function returns the logical name for the given file identification number.
18
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 Transaction Log supports
19
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 FILE [,...n ] [ TO FILEGROUP { filegroup_name | DEFAULT } ] | ADD LOG FILE [,...n ] | REMOVE FILE logical_file_name | MODIFY FILE }
20
RDBMS and Data Management/ Session 7/20 of 27 Database Modification Methods 1-2 Type of modifications Modification 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 databaseDBCC 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.
21
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 }
22
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_name ON ( NAME = logical_file_name, FILENAME = ‘os_file_name’ ) [,...n ] AS SNAPSHOT OF source_database_name [;]
23
RDBMS and Data Management/ Session 7/23 of 27 Database Snapshots Database created Snapshot created Data gets modifiedSnapshot is restored
24
RDBMS and Data Management/ Session 7/24 of 27 Database Snapshots AdvantagesDisadvantages 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.
25
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.
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.