0% found this document useful (0 votes)
7 views

11 October - 17 October - Creating - and - Maintaining - Database

The document discusses how to create and maintain databases in SQL Server. It defines what a database is and its key objects like tables and files. It explains how to create a database, specifying the primary and transaction log files. It also covers resizing the files, adding new files, and renaming or dropping databases.

Uploaded by

sbair1984
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

11 October - 17 October - Creating - and - Maintaining - Database

The document discusses how to create and maintain databases in SQL Server. It defines what a database is and its key objects like tables and files. It explains how to create a database, specifying the primary and transaction log files. It also covers resizing the files, adding new files, and renaming or dropping databases.

Uploaded by

sbair1984
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Creating and Maintaining

Database
By: SBD Team
What is a Database?

 Collection of tables and objects.


 Related to a process
 May or may not related in other
database
Objects in a database

 Tables
 Views
 Indexes
 Stored Procedures
 Triggers
Files
 Primary
 A database must consist of a primary data file and one
transaction log file.
 Contain the database objects
 Can be used for system tables and objects
 The primary data file has a .mdf extension
 Secondary
 Databases need have secondary data files if the primary data
file is not large enough to hold all the data in the database.
 Very large databases may need multiple secondary data files.
 Can be used to store user data and objects
 The secondary data file has a .ndf extension
Files

 Transaction Log
 Transaction log files hold the log information
used to recover the database.
 There must be at least one transaction log
file for each database, although there may
be more than one.
 The minimum size for a transaction log file
is 512 KB.
 The log files have an extension of .ldf
Creating a Database
 Creating a Database Defines:
 The name of the database
 The size of the database
 The files where the database will reside

CREATE DATABASE database_name


CREATE DATABASE database_name
ON
ON
PRIMARY (NAME == logical_file_name,
PRIMARY (NAME logical_file_name,
FILENAME =‘os_file_name',
FILENAME =‘os_file_name',
SIZE == size,
SIZE size,
MAXSIZE == max_size,
MAXSIZE max_size,
FILEGROWTH == growth_increment)
FILEGROWTH growth_increment)
LOG ON
LOG ON
(NAME == logical_file_name
(NAME logical_file_name ,,
FILENAME =‘os_file_name',
FILENAME =‘os_file_name',
SIZE = size,
SIZE = size,
MAXSIZE == max_size,
MAXSIZE max_size,
FILEGROWTH == growth_increment
FILEGROWTH growth_increment ))
Size
This parameter specifies the size of the data or log file. You can
specify sizes in megabytes (MB)—the default value—or kilobytes
(KB). The minimum size is 512 KB for both the data and log file.

MaxSize
This parameter specifies the maximum size to which the file can
grow. You can specify sizes in megabytes—the default value—or
kilobytes.
If you do not specify a size, the file grows until the disk is full.
FileGrowth

This parameter specifies the growth increment of the file.


The FILEGROWTH setting for a file cannot exceed the MAXSIZE
setting. A value of 0 indicates no growth.
The value can be specified in megabytes—the default—in kilobytes,
or as a percentage (%).
The default value if FILEGROWTH is not specified is 10 percent,
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. The percentage only applies to
file growth, not maximum size.
Example (Creating A Database)
 CREATE
CREATEDATABASE
DATABASESample
Sample
ON
ON
PRIMARY
PRIMARY((NAME
NAME==SampleData,
SampleData,
FILENAME
FILENAME ='c:\ProgramFiles\Data\Sample.mdf',
='c:\Program Files\Data\Sample.mdf',
SIZE = 10MB,
SIZE = 10MB,
MAXSIZE
MAXSIZE==15MB,
15MB,
 FILEGROWTH
FILEGROWTH==20% 20%))
LOG
LOGON ON
((NAME
NAME==SampleLog,
SampleLog,
FILENAME
FILENAME=‘c:\Program
=‘c:\ProgramFiles\Data\Sample.ldf',
Files\Data\Sample.ldf',
SIZE
SIZE==3MB,
3MB,
MAXSIZE
MAXSIZE==5MB,
5MB,
FILEGROWTH
FILEGROWTH==1MB 1MB))
Retrieving Database Information

 Determine Database Properties by Using


the DATABASEPROPERTYEX Function
 Use System Stored Procedures to
Display Information About Databases
and Database Parameters
 sp_helpdb
 sp_helpdb database_name
FileGroups

A FileGroup is a Collection of files. The


Files may or may not be on the same
Disk Drive.
Purpose
FileGroups improve the performance of
the database .
For Better database performance, files and
filegroups should be created on different
disks.
Filegroups
Northwind Database
sys...
sys... ...
...
sys...
sys... Orders
Orders
sysusers
sysusers OrdHistYear2
OrdHistYear2
Customers
Customers
sysobjects
sysobjects Products OrdHistYear1
OrdHistYear1
Products

C:\ D:\ E:\

OrdHist1.ndf
OrdHist1.ndf
Northwind.mdf
Northwind.mdf OrdHist2.ndf
OrdHist2.ndf Northwind.Idf
Northwind.Idf

Default Filegroup OrderHistoryGroup


Managing Data and Log File Growth
(Modifying Database)

 Using Automatic File Growth


 Expanding Database Files
 Adding Secondary Database Files
Syntax SQL
 ALTER DATABASE database
( ADD FILE < filespec > [ ,...n ] [ TO
FILEGROUP filegroup_name ]
| ADD LOG FILE < filespec > [ ,...n ]
| REMOVE FILE logical_file_name
| ADD FILEGROUP filegroup_name
| REMOVE FILEGROUP filegroup_name
| MODIFY FILE < filespec >
| MODIFY NAME = new_dbname
| MODIFY FILEGROUP filegroup_name
filegroup_property
)
Example
 ALTER
ALTERDATABASE
DATABASESample
Sample
MODIFY
MODIFYFILE
FILE((NAME
NAME=='SampleLog',
'SampleLog',
SIZE = 15MB)
SIZE = 15MB)
GO
GO
 ALTER
ALTERDATABASE
DATABASESample
Sample
ADD
ADDFILE
FILE
((NAME
NAME==SampleData2,
SampleData2,
FILENAME
FILENAME='c:\Program
='c:\ProgramFiles\..\..\Data\Sample2.ndf',
Files\..\..\Data\Sample2.ndf',
SIZE
SIZE==15MB,
15MB,
MAXSIZE
MAXSIZE==20MB
20MB))
GO
GO
Renaming a Database

The database should not be in use when it is


being renamed .

Syntax:
sp_renamedb ‘old_Dbname’, ‘new_Dbname’
Example:
sp_renamedb ‘employee’,’emp’
Dropping a Database
Methods of Dropping a Database
 SQL Server Enterprise Manager
 DROP DATABASE statement
DROP
DROP DATABASE
DATABASE Northwind,
Northwind, pubs
pubs

Restrictions on Dropping a Database


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

You might also like