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

Databases

Uploaded by

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

Databases

Uploaded by

Shashank Reddy
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Databases

User databases:

User databases are created by users and used for storing business data.. We can
create user databases by GUI or T-SQL

Create Database <Database Name>

System Databases:

The system database are some thing that gets created along with your SQL server
installation.. and the system database contains system level information and
these are not meant for storing users/business data

Types od Syste Database:

 Master
 Model
 Msdb
 TempDB
 Resource DB
 Distribution Database
 Report Server
 Report Server TempDb

Master:

It is a master of all the databases, it maintains system level information such


logins, linked servers, end points and also master database maintains information
about all other databases like database names, database ids, database properties
(recovery model, status, datbase id, comapatabilty ..etc) and also maintain files
infirmations like file name, file location , file size and other file settings..
Sys.syslogins – one of the table in master database maintains all the logins
informations

Sys.sysservers – this table in master database maintains linked servers created

Sys.endpoints – this is the table maintains all the end points informations

Select * from Sys.databases – it maitains all the database metadata like names,
id , properties.. etc

Sys.master_files – it contains information about all the database files.

Sys.syaltfiles – this will also contains database files information

Sys.dm_exec_requests – this will give you all open sessions information on a


perticular sql server

Sp_who or sp_who2 – These are the two system stored procedures gives you
open sessions information

With out master database (either master.mdf or master.ldf got corrupted), SQL
Server will not start

When ever we restart sql server, it reads the startup parameters for the master
database locations and then it will check whether the corresponding files exist
or not.. if the files are exist then SQL server will start with out any issues..

Configuration manager  Run  sqlservermanager15.msc

Right click on the instance name  properties  startup parameters


Model:

It is a template database in sql server; whenever you create a new user


database or tempdb it will inherit all the properties from model database…

We can create an user database with the following simple create command

Create Database TestDB

TestDB will inherit all the properties of model database.

With out model database , SQL Server will start or not ?

No it will start the sql server, reason is when you restart sql server.. it has to
create a new tempdb for that model database is required to generate a
template. If the model database is not available, then it will not create tempdb..
so sql server will not start..
MSDB:

SQL Server agent service will be used to implement automations in sql server like
creating jobs and scheduling. MSDB maintains all the information about sql
server agent related like jobs, steps, schedule, operators, alerts, backup
information, restore history ..etc

Backupset – backup information

backupmedia family – backup files information

With out msdb database , SQL Server will start or not ?

Yes, sql server will start with out any issues, but agent service will not work.

TempDB: It’s a temporary database in SQL server.. and it maintains all the
intermittent results of a transaction..

A=5

B=10

Intermittent result (C=a+b=5+10=15)

Intermittent result (D=c-a=15-5=10)

Final E= a+b+c+D= 40

Create table #tlist (name varchar(50))

Insert into #tlist

Select name from sys.databases : This will return all the database names

Wite a loop to iterate all the names from #tlist

Backup database databaneme to disk=path


}

Drop table #tlist

Whenever you restart SQL server, a new tempdb will be created.

With out Tempdb database , SQL Server will start or not ?

Yes it will start sql server as when we restart sql server, it will create new
tempdb..

Q.How to find when SQL server restarted last time ?

By checking, tempdb creation date and time

Right click on tempdbpropertiesgeneralcreate date time

Moving System Databases:

Model:
--Step1: Identify the current location of model database
--right click on model-->properties--> files
use model
go
sp_helpfile

select * from sys.master_files


select * from sys.sysaltfiles

--Step2:update system catalog with new path for the


model database

alter database model modify file


(name='modeldev',filename='C:\sql\model.mdf')
alter database model modify file
(name='modellog',filename='C:\sql\modellog.ldf')

--step3: Stop the sql server


--Step4: copy the files from old to new location
--Step5: Ensure your account have full permissions the
files in new location
--Step6: start the sql server
--step7: verify the new location

use model
go
sp_helpfile

MSDB:
--Step1: Identify the current location of model database
--right click on model-->properties--> files
use msdb
go
sp_helpfile

select name,physical_name
from sys.master_files
where database_id=4

select * from sys.sysaltfiles

--Step2:update system catalog with new path for the model database
alter database msdb modify file
(name='MSDBData',filename='C:\sql\MSDBData.mdf')
alter database msdb modify file
(name='MSDBLog',filename='C:\sql\MSDBLog.ldf')
--step3: Stop the sql server
--Step4: copy the files from old to new location
--Step5: Ensure your account have full permissions the files in new location
--Step6: start the sql server
--step7: verify the new location
use msdb
go
sp_helpfile

Tempdb:

--Step1: Identify the current location of model database


--right click on model-->properties--> files
use tempdb
go
sp_helpfile
select name,physical_name
from sys.master_files
where database_id=2

select * from sys.sysaltfiles

--Step2:update system catalog with new path for the model database
alter database tempdb modify file
(name='tempdev',filename='C:\sql\tempdb.mdf')
alter database tempdb modify file
(name='temp2',filename='C:\sql\tempdb_mssql_2.ndf')
alter database tempdb modify file
(name='temp3',filename='C:\sql\tempdb_mssql_3.ndf')
alter database tempdb modify file
(name='temp4',filename='C:\sql\tempdb_mssql_4.ndf')
alter database tempdb modify file
(name='templog',filename='C:\sql\templog.ldf')

--Step3: re-start the sql server


--step7: verify the new location
use tempdb
go
sp_helpfile

Master:
--Step1: Identify the current location of model database
--right click on model-->properties--> files
use master
go
sp_helpfile

select name,physical_name
from sys.master_files
where database_id=1

select * from sys.sysaltfiles

--Step2:update startup parameters with new path for the master database
--step3: Stop the sql server
--Step4: copy the files from old to new location
--Step5: Ensure your account have full permissions the files in new location
--Step6: start the sql server
--step7: verify the new location
use master
go
sp_helpfile

SQL Server “Error” Log or SQL Server log file:


This is the log file in sql server, it basically logs following event/activities.. this is
very thing that you need to verify whenever an issues reported for sql server like
connectivity issues, login access issues, backup failures, job failures, sql server
startup issues and performance related issues/…etc

Informational message

Error messages

Warnings

How many types of error logs:

1. SQL server log


2. Agent log

How to check error log: When your sql server is up and running

1. SSMS connect to instance  ManagementSQL server logs

2. SSMSConnect to instance Expand SQL server agenterror logs


Verify the error log from the physical location: (when your sql server is not
running)

First identify the error location:


Go to start up parameters  -e parameter gives you error log physical
location
Verify error log using Stored procedure:

EXEC master.dbo.sp_readerrorlog 0, 1, '2019', 'exec'

1 st param - is file number


2 nd parma - is file type (SQL server log or agent log)
1. SQL server
2. SQL Server agent
3rd param - serach string 1
4th param - search string 2

Error log recycling:


1. Recycling happened when ever sql server is restarts
2. We can perfrom error log recycling using following sroed procures

sp_cycle_errorlog

Default error log count : 6 (1 current and 6 archives)

Can I change default error count ?

Yes,

You might also like