Ms SQL Query
Ms SQL Query
• Developer
o Programming
Stored procedures
Functions
Triggers
Cursors
RDBMSs allow Create, Read, Update, and Delete (CRUD) operations. Structured Query
Language (SQL) is the language that is used to query, update, and delete data in relational
database management systems (RDBMS). SQL is a standard query language. SQL language’s
queries are also known as SQL commands or SQL statements.
RDBMS Products:
1. SQL Server – Mircosoft, licensed
2. Oracle – Oracle Corporation , licensed
3. DB2 – IBM, Licensed
4. Sybase – SAP, Licensed
5. MySQL – Oracle (open source)
6. MariaDB – Open source
7. PostgreSQL – Open source
8. Green plum – Greenplum, License
9. Informix
10. Azure SQL
11. AWS RDS
12. Amazon Aurora
13. CloudSQL – GCP
NoSQL Products: (Not Only SQL)
• Mongo DB
• Cassandra
• BigData Hadoop
• CouchDB
SQL Server History:
SQL Server was originally developed by Sybase corporation.. Then Sybase approached
Microsoft to take it to the market
Starting from SQL Server 2017 Service Packs will no longer be released
SP1 SP2 SP3 SP4
SQL Server 2016
SQL Server 13
Support end date: 2021-07-13
Ext. end date: 2026-07-14 13.0.1601.5
+ CU9
13.0.4001.0
or 13.1.4001.0
+ CU15
13.0.5026.0
or 13.2.5026.0
+ CU17
13.0.6300.2
or 13.3.6300.2
SQL Server 2014
SQL Server 12
Support end date: 2019-07-09
Ext. end date: 2024-07-09 12.0.2000.8
+ CU14
12.0.4100.1
or 12.1.4100.1
+ CU13
12.0.5000.0
or 12.2.5000.0
+ CU18
12.0.6024.0
or 12.3.6024.0
+ CU4
Patch:
A Patch nothing but a solution for bugs reported (bug fix)
CTP Community Technology Preview (beta release)
RC Release Candidate
RTM Released To Manufacturing; It is the original, released build version of the product, i.e.
what you get on the DVD or when you download the ISO file from MSDN.
CU Cumulative Update; Cumulative updates contain the bug fixes and enhancements–up to
that point in time–that have been added since the previous Service Pack release and will be
contained in the next service pack release. Installation of the Cumulative Update is similar to the
installation of a Service Pack. Cumulative Updates are not fully regression tested.
* Since January 27, 2016: Microsoft recommends ongoing, proactive installation of SQL Server
CUs as they become available. SQL Server CUs are certified to the same levels as Service
Packs, and should be installed with the same level of confidence.
SP Service Pack; much larger collection of hotfixes that have been fully regression tested. In
some cases delivers product enhancements. All SQLServer service packs are cumulative,
meaning that each new service pack contains all the fixes that are included with previous
service packs and any new fixes.
* Service Packs are discontinued starting from SQL Server 2017. Only Cumulative Updates
(CUs) will be provided.
GDR General Distribution Release; GDR fixes should not contain any of the CU updates.
QFE Quick Fix Engineering; QFE updates include CU fixes.
CVE Common Vulnerabilities and Exposures; publicly known information-security
vulnerabilities.
OD On-Demand hotfix. An OD is released per customer request for specific problem
scenarios to that customer. OD hotfixes may or may not be released to the general public.
COD Critical On-Demand hotfix. A COD hotfix is defined as critical because security is
impacted a severe stability or functionality is detected.
Free editions
Express Express edition is a free lightweight/lite edition of SQL Server with some
limitations, that can be used in production environment.
Main limitations:
• Limited to lesser of 1 physical CPU or 4 cores.
• No single database (.MDF file) can be over 10 GB.Download SQL2019 Express
EditionDownload SQL2017 Express Edition
Evaluation Evaluation edition is a fully-functional trial Enterprise Edition of SQL Server but is
limited to 180 days, after which the tools will continue to run, but the server services will
stop.Download SQL2019 Evaluation EditionDownload SQL2017 Evaluation Edition
Paid editions
Enterprise Enterprise edition is the top-end edition with a full feature set.
Enterprise Core Enterprise edition with the Core-based Licensing. The plain Enterprise
one is limited to just 20 CPU cores.
Standard Standard edition has less features than Enterprise, when there is no requirement
of advanced features.
Azure SQL Database Azure SQL Database is the cloud-based version of SQL Server,
presented as a platform as a service (PaaS) offering on Microsoft Azure.
Specialized editions
Web Web edition is a low total-cost-of-ownership option for Web hosters.
Business Intelligence Business Intelligence edition includes the Standard Edition capabilities
and Business Intelligence tools.
Analytics Platform System (APS) Formerly Parallel Data Warehouse (PDW). A massively
parallel processing (MPP) SQL Server appliance optimized for large-scale data warehousing
such as hundreds of terabytes.
Big Data Clusters (BDC) Starting with SQL Server 2019, SQL Server Big Data Clusters
allow you to deploy scalable clusters of SQL Server, Spark, and HDFS (Hadoop Distributed File
System) containers running on Kubernetes.
LocalDB SQL Server Express LocalDB is a lightweight version of Express that has all of its
programmability features, runs in user mode and has a fast, zero-configuration installation and a
short list of prerequisites.
Edition comparison:
Feature Enterprise Standard Express Express w/ Advanced Services
Max. Compute Capacity (per instance) OS Max. Lesser of 4 sockets / 24 cores
Lesser of 1 socket / 4 cores Lesser of 1 socket / 4 cores
Max. Buffer Pool Memory (per instance) OS Max. 128GB 1410MB 1410MB
Max. Columnstore Cache Memory (per instance) Unlimited 32GB 352MB 352MB
Max. Memory-Optimized Data Size (per instance) Unlimited 32GB 352MB 352MB
Max. database size 524PB 524PB 10GB 10GB
Log Shipping Yes Yes No No
Mirroring Yes Yes As Witness As Witness
Backup Compression Yes Yes No No
Database Snapshots Yes Yes Yes Yes
AlwaysOn AG Yes No No No
Basic Availability Groups(opens new window)
Yes Yes (2 nodes) No No
Encrypted Backups Yes Yes No No
Stretch Database Yes Yes Yes Yes
Table/index Partitioning Yes Yes Yes Yes
Buffer Pool Extension Yes Yes No No
Compression Yes Yes Yes Yes
Resource Governor Yes No No No
In-Memory OLTP Yes Yes Yes Yes
Auditing Yes Yes Yes Yes
Fine Grained Auditing Yes Yes Yes Yes
Dynamic Data Masking Yes Yes Yes Yes
Always Encrypted w/ Secure Enclaves Yes Yes Yes Yes
Transparent Database Encryption Yes Yes No No
Contained Databases Yes Yes Yes Yes
Change Tracking Yes Yes Yes Yes
Merge Replication Yes Yes As Subscriber As Subscriber
Transactional Replication Yes Yes As Subscriber As Subscriber
Transactional Replication to Azure Yes Yes No No
Snapshot Replication Yes Yes As Subscriber As Subscriber
P2P Transactional Replication Yes No No No
SQL Server Agent Yes Yes No No
Dedicated Admin Connection Yes Yes Yes Yes
PowerShell SMO Support Yes Yes Yes Yes
Full Text Search Yes Yes No Yes
CLR Integration Yes Yes Yes Yes
Import/Export Wizard Yes Yes Yes Yes
Database Mail Yes Yes No No
UTF-8 Yes Yes Yes Yes
Java Runtime Integration Yes Yes Yes Yes
Polybase Compute Node Yes Yes Yes Yes
Master Instance for Big Data Cluster Yes Yes Yes Yes
Acceleterated Database Recovery Yes Yes No No
Intelligent Database: automatic tuning Yes No No No
Intelligent Database: batch mode for row store Yes No No No
Intelligent Database: row mode memory grant feedback Yes No No No
Intelligent Database: approximate count distinct Yes Yes Yes Yes
Intelligent Database: table variable deferred compilation Yes Yes Yes Yes
Intelligent Database: scalar UDF inlining Yes Yes Yes Yes
Parallel Query Processing(opens new window)
Yes Yes No No
Developer Edition:
CMD prompt/Powershell:
IP address:
RunMsinfo32
Rundiskmgmt.msc
Computer Management:
Instance:
Instance is nothing but a SQL Server; In one machine we can install multiple instances. Each
instance should unique name.
Types of Instances:
1. Default - If we choose instance type as default, it will automatically assigns windows
machine name as a default instance name. so in one machine, we can setup only one default
instance
2. Named: If we choose instance type as named, it will allows us assign a name as per our
business requirements. We can setup 49 named instances in one machine
Total in one server, we can setup 50 SQL servers…
2. Service console:
Runservices.mscright click on SQL servicesstart/stop/restart
3. Command Prompt
Open CMD as administrator
Default:
NET START MSSQLSERVER
NET STOP MSSQLSERVER
Named Instance:
NET START MSSQL$<INSTANCE NAME>
NET STOP MSSQL$<INSTANCE NAME>
4. Powershell
Default:
Start-service –name mssqlserver
Stop-service –name mssqlserver
Named:
Start-service –name ‘mssql$instancename’
Stop-service –name ‘mssql$instancename’
5. SSMS
Named Instance:
Server Name - <serveranme/Instance>
Option#2:
RunREGEDIT
CU12:
Uninstall a patch:
Control panelprogrammsview installed updates
CONNECTIVITY:
How to Enable TCP/IP for a SQL server?
Configuration managerSQL Server network configurationsprotocols for <SQL server>right click
on TCP/IP enableRestart SQL Servvice
Telnet success:
Telnet Failure:
2. Constraints:
Constrains allows us to create a condition on the table
a. Primary Key: it won’t allow null values and duplicates. In one table we can create only
one primary key
b. Not null: It won’t allow any null values
c. Unique: it won’t allow duplicates, but it will allow one null value
d. Check: it accepts values in a particular range.
Age >18 and age <35
e. Default: while creating a table.. we can assign default value to particular column.
EX: Location varchar(50) default ‘Hyd’
f. Identity: if we have any column with fixed auto incremental values..we can use Identity
Ex: SNO int Identity (1,1)
g. Foreign Key: it must be a primary key in some other table and it accept the values only
from reference table.
SQL constraints are used to specify rules for the data in a table.
Constraints are used to limit the type of data that can go into a table. This ensures the accuracy
and reliability of the data in the table. If there is any violation between the constraint and the
data action, the action is aborted.
Constraints can be column level or table level. Column level constraints apply to a column, and
table level constraints apply to the whole table.
The following constraints are commonly used in SQL:
• NOT NULL - Ensures that a column cannot have a NULL value
• UNIQUE - Ensures that all values in a column are different
• PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each
row in a table
• FOREIGN KEY - Prevents actions that would destroy links between tables
• CHECK - Ensures that the values in a column satisfies a specific condition
• DEFAULT - Sets a default value for a column if no value is specified
• CREATE INDEX - Used to create and retrieve data from the database very quickly
3. SQL languages
a. DDL: Data definition language
• Create
Create database <database Name>
eX:
create database Devbasics
• Alter
• DROP
drop database [Devbasics]
USE [master]
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [Devbasics2].[dbo].[sp_fulltext_database] @action = 'enable'
end
GO
--creating table
create table employee
(id int,
name varchar(50),
sal int,
location varchar(50),
DOJ datetime)
--adding column to the table
Alter table [dbo].[employee] add dept varchar(50)
--drop a column from the table
alter table [dbo].[employee] drop column dept
--DML Commands
insert into [dbo].[employee] values
(1,'Mike',10000,'Hyd',(getdate()-365))
--column filter
SELECT SalesOrderID,UnitPrice FROM [Sales].[SalesOrderDetail]
--row filters
SELECT * FROM [Sales].[SalesOrderDetail]
where SalesOrderID=43659
Architectures:
1. Database Architecture
1. Data file Architecture
`Page Architecture
2. Log file architecture
2. SQL server Architecture
3. Memory Architecture
Databases:
1. System Databases: The system databases are gets created by default as part SQL
installation
a. Master
b. Model
c. Msdb
d. Tempdb
e. Resource
2. User Databases: User Databases are something which we create to store
users/Business data
Master:
Master is a master of all the databases and it maintains all the systems level information such
as logins, linked servers, end points and it also maintains information about all other databases
and database files information also, open sessions, connections..etc
Logins:
select name from [sys].[syslogins]
Linked Server:
If we want access data of one SQL server from another SQL Server
sp_who
sp_who2
Ex:
Daily_sales
Sales_history
Requirement –
all the daily sales information will be inserted into Daily_sales table
Every Day mid night, we need insert all the data from Daily_Sales to sale_history and then
delete from daily_Sales
USE [DevBasics]
GO
Create table daily_sales
(salesID int, orderqty int, orderDate datetime)
USE [DevBasics]
GO
Create table daily_sales
(salesID int, orderqty int, orderDate datetime)
Create table sales_history
(salesID int, orderqty int, orderDate datetime)
Backup history:
TempdB: it is temporary database in SQL Server, it maintains all the intermittent results of a
transaction temporarily. It is a central and critical database and this will be used by transaction
from every databases.
**Whenever we restart SQL server, a new TempDB will be created
Temporary Tables:
#Temp (local Temp tables): the span is limited to a session where it is created.
create table #Temp1 (id int)
create table ##Temp2 (id int)
Option#2:
use [DevBasics]
go
sp_helpfile
Option3#:
select name,physical_name from sys.master_files
Error Log:
It is log file in SQL server and maintains all the error messages, warnings and informational
messages. In SQL server, we need to start trouble any issues with verifying error log.
How to find error log location:
Option #1:
-e in startup parameters provides the location of error log
Option#2:
SSMSunder Instance NamemanagementSQL server logs
Right click on the SQL Server logs configure Restart SQL server/Recycle error log
exec sp_cycle_errorlog
Customer have a requirement to generate a new error log file everyday with limit of 30 file ?
Create a sql server agent job add sp_cycle_errorlog to step add schedule
Option #3:
We have two types logs
1. SQL Server log
2. Agent log
As we know C – Drive OS drive, it is not a best practice to keep any of the database files in C
drive.. as part of any assessment, if we see any system/user databases in C- drive, we need to
move the system databases to other drive..
How to move:
MSDB:
--Moving MSDB Database
--step #1: identify the current location of MSDB database
use MSDB
go
sp_helpfile
--or
--right click on the model--> propeties-->file
--or
select * from sys.master_files where database_id=4
--Step#2: modify the current location msdb database in system catalog
ALTER DATABASE [msdb] MODIFY FILE
(NAME='MSDBData',FILENAME='F:\SQLLab\SYSTEMDATABASES\MSDBData.mdf')
ALTER DATABASE [msdb] MODIFY FILE
(NAME='MSDBLog',FILENAME='F:\SQLLab\SYSTEMDATABASES\MSDBLog.ldf')
--STEP#3: STOP THE SQL SERVER
--STEP#4: COPY THE FILES FROM OLD TO NEW DIRECTORY MANULLAY
--STEP#5: START THE SQL SERVER
--sTEP#6: VERIFY THE CURRENT LOCATIN OF msdb DATABASE
USE msdb
GO
sp_helpfile
TEMPDB:
--Moving Tempdb Database
--step #1: identify the current location of temdbdb database
use tempdb
go
sp_helpfile
--or
--right click on the model--> propeties-->file
--or
select * from sys.master_files where database_id=2
--Step#2: modify the current location tempdb database in system catalog
ALTER DATABASE [tempdb] MODIFY FILE
(NAME='tempdev',FILENAME='F:\SQLLab\SYSTEMDATABASES\tempdb.mdf')
ALTER DATABASE [tempdb] MODIFY FILE
(NAME='temp2',FILENAME='F:\SQLLab\SYSTEMDATABASES\tempdb_mssql_2.ndf')
ALTER DATABASE [tempdb] MODIFY FILE
(NAME='temp3',FILENAME='F:\SQLLab\SYSTEMDATABASES\tempdb_mssql_3.ndf')
ALTER DATABASE [tempdb] MODIFY FILE
(NAME='temp4',FILENAME='F:\SQLLab\SYSTEMDATABASES\tempdb_mssql_4.ndf')
Master:
--Moving master Database
--step #1: identify the current location of master database
use master
go
sp_helpfile
--or
--right click on the master--> propeties-->file
--or
select * from sys.master_files where database_id=1
--Step#2: modify the startup paramter with the new path
Joins:
If we want to retrieve the data from two or more tables. Then we can write joins. If we want join
tow tables, there should be a common column between these tables.
Case study – pull all the databases along with file names
select
sd.name,
sd.create_date,
sd.recovery_model_desc,
smf.type_desc,
smf.physical_name from
sys.databases sd join sys.master_files smf
on sd.database_id=smf.database_id
Example:
create table orders (ORderID int, customerID int, orderdate datetime)
--left join
select O.ORderID,C.customername,O.orderdate
from orders O
LEFT join customers c
on o.customerID=c.custid
--RIGHT join
select O.ORderID,C.customername,O.orderdate
from orders O
right join customers c
on o.customerID=c.custid
--FULL join
select O.ORderID,C.customername,O.orderdate
from orders O
full join customers c
on o.customerID=c.custid
Union:
The UNION operator is used to combine the result-set of two or more SELECT statements.
• Every SELECT statement within UNION must have the same number of columns
• The columns must also have similar data types
• The columns in every SELECT statement must also be in the same order
SELECT City FROM Customers
UNION
SELECT City FROM Suppliers
ORDER BY City;
Union selects only distinct values.. but union all selects duplicates also..
Maintaining a copy of original data in a different location. In the event of original data loss, we
can recover it from the backup available.
It is a DBA prime responsibility to configure the regular backups from all our production
databases..
Backup/Recovery Architecture:
Types of backups:
1. Full backup
2. Differential backup
3. Log or Transactional log backups
4. File and file group backups
5. Split backup
6. Mirror backups
7. Tail log backups
8. Copy only backup
Full backup:
It is copy of entire database.. if I have database of 500 Gb.. then if I take full backup of that
database and the backup size will be ‘500 Gb’..
The extension of full back up is ‘bak’
Differential backups:
This takes backup of all the changes happened from last successful full backup to till now.
I have full backup happened at 10 AM and from there upto 12 PM, about 10 GB data added..
now when we take differential backup at 12:00 PM, it takes the backup of 10 GB added after last
full backup
Log backup:
It takes the backup of all the transactions available in the log file and once the backup is done, it
will truncate those transactions from the log file..
Backup Strategy:
Failure Point:
The exact time at which the database is crashed is called ‘Failure point’
In our case failure point is ‘Thursday 4.30 PM’
Recovery Models:
It is a database level option and it decides to which point of time we can recover a database in
the event of failure.
1. Simple
2. Bulk logged
3. Full Recovery
Simple: In simple recovery model or if we put our database in simple recovery model.. The
transaction log file (ldf) gets truncated automatically in regular intervals. So we can’t take log
backup in simple recovery model, as we don’t have log backup, we can’t achieve point in time
recovery. This is not recommended for production OLTP (online transactional processing)
databases. This can be set for Non production databases and OLAP (Online analytical
processing)
Case 1: Database is in bulk logged recovery model and there are bulk operations performed..
then Point time recover is not possible
Case 2: Database is in bulk logged recovery model and there are no bulk operations performed,
so it is as similar to full recovery model,. Then point time recovery is possible.
** Bulk logged recovery model is recommended for any database when we performing bulk
operations like BCP, Bulk insert, select into, index rebuild ..etc
Full Recovery model:
In full recovery model, all the transactions will be fully logged into the transaction log file and it
wont truncate any of these transaction automatically.. we can take transaction backups..as we
have transaction log backup, we can achieve Point time recovery.
It is recommended to keep all the production databases in full recovery model.
Option #1:
Option #2:
select name,recovery_model_desc from sys.databases
Option#3:
use [DevBasics]
go
sp_helpdb
Option#1:
Right click on the database namepropertiesoptionschange the recovery model from the
dropdown
Option#2:
use master
go
alter database [DevBasics] set recovery simple
alter database [DevBasics] set recovery Full
alter database [DevBasics] set recovery bulk_logged
Recovery Options:
With no recovery:
If there are any further backups to be restored, then we need to choose, with no recovery. It
restores the backup, but it will not bring the database online. The database will be in restoring
state and we cant access it.
With recovery:
When there are no further backups to be restore and this is our last log back as part of the
recovery.. the new can with recovery and it restores the backup and brings the database online
for use.
With Standby:
It restores the backup and brings database online with read-only enabled and can’t make any
changes to data.
Ways to backup:
1. SSMS – GUI:
Restore Process:
2. SSMS – T-SQL command
Case study:
F – Full backup
D – Differential backup
T – Transactional log backups
P – Failure points/PIT
P4 – Amulya
• Restore F1 with no recovery
• Restore T1 with no recovery
• Restore T2 with no recovery
• Restore D1 with recovery
P4 – Anil
• Restore F1 with no recovery
• Restore T1 with no recovery
• Restore T2 with no recovery
• Restore D1 with no recovery
• Restore D3 with recovery
P4 – Sreekar
• Restore F1 with no recovery
• Restore D1 with no recovery
• Restore T1 with no recovery
• Restore T2 with recovery
P4 – Ramu
• Restore F1 with no recovery
• Restore D3 with no recovery
• Restore T8 with recovery
P9- Durga
Restore F1 with no recovery
Restore D1 with no recovery
Restore D2 with no recovery
Restore D5 with recovery
Restore F2 with recovery
P9- Manohar
Restore F2 with recovery
P8 – Sreekar
• Restore F1 with no recovery
• Restore D2 with no recovery
• Restore t5 w/r
P6- Santosh
• Restore F1 with no recovery
• Restore D1 with no recovery
• Restore t3 with no recovery
• Restore t4 w/r
P11 – Sreenu
• Restore F2 with no recovery
• Restore T6 with no recovery
• Restore t7 w/r
Case study:
I am trying to take a log backup and log backup option is not showing?
May be the database is in simple recovery model, log backups are allowed only in full or bulk
logged recovery model
I have changed the recovery model to Full, but still my log backup is not working?.. it is failing
with below error..
Ans: when ever we change the recovery model, we need to take a fresh full back.. then only the
log backups will work..
Split Backup:
If my target backup location doesn’t have enough space to accommodate the backup.. Then we
can use split backup, this will divide the database backup equally in to multiple destinations.
This split backup improves the backup performance as it will initiate multiple threads to write the
backup into backup location.
Demo:
Restore:
T-SQL:
backup database [BackupDemo]
to
disk='F:\SQLLab\backups\backupdemo_s1.bak',
disk='F:\SQLLab\backups\backupdemo_s2.bak'
Mirror Backup:
Taking multiple copies of full backup at a time for extra safety.
Recovery process:
• Restore latest Sunday full backup with no recovery
• Restore Thursday 6 am differential backup with no recovery
• Restore log backups from 7am to 3 PM with no recovery
• Restore 4 pm log backup with recovery
So, we are able to recover the database up to 4 PM, but we are losing about 30 mins
transactions.
To recover remaining 30 mins of transaction, we can perform tail log backup.. this takes the
backup of all the transaction happened from last successful log backup (Thursday 4 PM) and to
failure point (Thursday 4.30 PM).
If we have database crashed. But the log file healthy.. in that case we can take tail log backup..
Recovery process:
• Perform the manual tail log backup
• Restore latest Sunday full backup with no recovery
• Restore Thursday 6 am differential backup with no recovery
• Restore log backups from 7am to 4 PM with no recovery
• Restore tail log backup with recovery
Demo:
--Tail Log back up and Restore Demonstration:
--Create a Database
CREATE DATABASE [TailLogTest]
--Create a Table
USE [TailLogTest]
GO
use master
Go
--Set database to offline
ALTER DATABASE [TailLogTest] SET OFFLINE
GO
On Thursday 4.30 PM, got a request to refresh dev server with a production backup.
--step 1
USE master
GO
create database CopyOnlyDemo
/**If we try to restore our original full backup, our latest full backup and any transaction log
backup after the differential we get this error. **/
USE master
GO
--F:\SQLLab\backups\copyonly\Copy_ONLY (sunday full bakup)
BACKUP DATABASE CopyOnlyDemo
TO DISK='F:\SQLLab\backups\copyonly\CopyOnlyDemo_full1.BAK'
--step 2
RESTORE DATABASE CopyOnlyDemo FROM
DISK='F:\SQLLab\backups\copyonly\CopyOnlyDemo_full1.BAK' WITH NORECOVERY,
REPLACE
RESTORE DATABASE CopyOnlyDemo FROM
DISK='F:\SQLLab\backups\copyonly\CopyOnlyDemo_diff3.BAK' WITH NORECOVERY
RESTORE LOG CopyOnlyDemo FROM
DISK='F:\SQLLab\backups\copyonly\CopyOnlyDemo_log2.TRN' WITH RECOVERY
What is the database and tables maintain the backup and restore history:
Database: MSDB
Tables:
backupset – database name
and backupmediafamily – file location
select
bs.database_name,
bs.type,
bs.is_copy_only,
bs.differential_base_lsn,
bs.backup_finish_date,
bmf.physical_device_name
from backupset bs join backupmediafamily bmf
on bs.media_set_id=bmf.media_set_id
order by bs.backup_finish_date desc
Backup Options:
12 percent processed.
22 percent processed.
32 percent processed.
42 percent processed.
52 percent processed.
62 percent processed.
72 percent processed.
81 percent processed.
91 percent processed.
100 percent processed.
Processed 336 pages for database 'CopyOnlyDemo', file 'CopyOnlyDemo' on file 1.
Processed 2 pages for database 'CopyOnlyDemo', file 'CopyOnlyDemo_log' on file 1.
BACKUP DATABASE successfully processed 338 pages in 0.278 seconds (9.484 MB/sec).
Restore commands:
• Restore Verify only – checks wether backup is in valid restorable format or not
restore verifyonly from DISK='F:\SQLLab\backups\backupdemo.BAK'
• Restore header only: This will provide all the metadata of backup – database name,
backup name, backup type, backup date and it provides the backup sets information.
Backup set:
• Restore filelistonly – it will list out all the file information from the backup
restore filelistonly
from DISK='F:\SQLLab\backups\backupdemo.bak'
• Restore Lableonly –This will basically give us the backup media information.
Backup Strategy:
Weekly Sunday 12:00 – full backups
Daily differential backups – 6 am
Log backups for every 1 hour
On Thursday 4.30 PM, a developer accidentally deleted a table from the production.
IMPORTANT: The ability to grant access to the Database Engine and to configure user
permissions allows the security admin to assign most server permissions. The securityadmin
role should be treated as equivalent to the sysadmin role.
processadmin Members of the processadmin fixed server role can end processes that are
running in an instance of SQL Server.
setupadmin Members of the setupadmin fixed server role can add and remove linked servers
by using Transact-SQL statements. (sysadmin membership is needed when using Management
Studio.)
bulkadmin Members of the bulkadmin fixed server role can run the BULK INSERT
statement.
The bulkadmin role or ADMINISTER BULK OPERATIONS permissions is not supported for SQL
Server on Linux. Only the sysadmin can perform bulk inserts for SQL Server on Linux.
diskadmin The diskadmin fixed server role is used for managing disk files.
dbcreator Members of the dbcreator fixed server role can create, alter, drop, and restore
any database.
Public Every SQL Server login belongs to the public server role. When a server principal has
not been granted or denied specific permissions on a securable object, the user inherits the
permissions granted to public on that object. Only assign public permissions on any object when
you want the object to be available to all users. You cannot change membership in public.
Note: public is implemented differently than other roles, and permissions can be granted,
denied, or revoked from the public fixed server roles.
sp_configure
go
sp_configure
sp_configure 'max server memory (MB)', 1024
go
reconfigure
go
sp_configure 'xp_cmdshell', 1
go
reconfigure
go
Fixed-Database Roles
The following table shows the fixed-database roles and their capabilities. These roles exist in all
databases. Except for the public database role, the permissions assigned to the fixed-database
roles cannot be changed.
FIXED-DATABASE ROLES
Fixed-Database role name Description
db_owner Members of the db_owner fixed database role can perform all configuration and
maintenance activities on the database, and can also drop the database in SQL Server. (In SQL
Database and Azure Synapse, some maintenance activities require server-level permissions
and cannot be performed by db_owners.)
db_securityadmin Members of the db_securityadmin fixed database role can modify role
membership for custom roles only and manage permissions. Members of this role can
potentially elevate their privileges and their actions should be monitored.
db_accessadmin Members of the db_accessadmin fixed database role can add or remove
access to the database for Windows logins, Windows groups, and SQL Server logins.
db_backupoperator Members of the db_backupoperator fixed database role can back up the
database.
db_ddladmin Members of the db_ddladmin fixed database role can run any Data Definition
Language (DDL) command in a database.
db_datawriter Members of the db_datawriter fixed database role can add, delete, or change
data in all user tables.
db_datareaderMembers of the db_datareader fixed database role can read all data from all user
tables and views. User objects can exist in any schema except sys and
INFORMATION_SCHEMA.
db_denydatawriter Members of the db_denydatawriter fixed database role cannot add,
modify, or delete any data in the user tables within a database.
db_denydatareader Members of the db_denydatareader fixed database role cannot read any
data from the user tables and views within a database.
MSDB Roles:
msdb role name Description
db_ssisadmin
db_ssisoperator
db_ssisltduser Members of these database roles can administer and use SSIS. Instances of
SQL Server that are upgraded from an earlier version might contain an older version of the role
that was named using Data Transformation Services (DTS) instead of SSIS. For more
information, see Integration Services Roles (SSIS Service).
dc_admin
dc_operator
dc_proxy Members of these database roles can administer and use the data collector. For
more information, see Data Collection.
ServerGroupAdministratorRole
High availability is to ensure the database services are available up and running for applications
all the time.
HA solutions:
• Log shipping - Database
• Database Mirroring – Database
• Replication – Database Level/Object level
• Failover clusters – Instance/Server level
• Always On –SQL server 2012 – Database (group of database)
Log Shipping:
Log shipping is basic database level high availability solution which transfers the transactional
log backups from one server (prod) to another server (standby) and it restores in the stand by
server automatically.
2. Database must be in Full or bulk logged Recovery Model, but recommended is full
recovery model
3. Two network share folders – one in primary and another one is in secondary
4. SQL server agent must be up and running
5. Take a full backup of primary database and restore it in secondary w/n
use LsDemo
go
sp_helpfile
USE [master]
GO
ALTER DATABASE [LsDemo] ADD FILE
( NAME = N'LsDemo1', FILENAME = N'F:\SQLLab\LsDemo1.ndf' , SIZE = 8192KB ,
FILEGROWTH = 65536KB )
TO FILEGROUP [PRIMARY]
GO
The restore job will fail, when it restore the log backup generated after we added the file to
primary database. It will try to create same file to the secondary database in the same location.
Because of the restore job will fail with below error.
How to Fix it :
We need to identify the log backup which is generate after adding the file to primary database
How to fix ?
We need to reconfigure log shipping, take a full backup of primary and restore it into secondary
with norecovery
--Primary
backup database [LsDemo]
to disk='F:\SQLLab\logshipping\lsdemo_full.bak'
--secondary
restore database [LsDemo]
from disk='F:\SQLLab\logshipping\lsdemo_full.bak'
with norecovery
Log shipping is working fine, some log backups are deleted before they restore into the
secondary:
How to Fix:
We need to take one differential from primary and restore it in secondary w/n
backup database [LsDemo]
to disk='F:\SQLLab\logshipping\lsdemo_diff.bak'
with differential
restore database [LsDemo]
from disk='F:\SQLLab\logshipping\lsdemo_diff.bak'
with norecovery
How to change the secondary database from restoring to stand by Vice versa:
1. Take a tail log backup (this will take backup of all the transaction happened from last log
backup to failure point)
2. Identify all the pending log backups from primary and sencondary shares and restore
w/n
3. Restore tail log backup w/r
Database Mirroring:
Mirroring also database level solution which transfers transaction logs from one server to
another server and apply those transactions.
Pre requisites:
1. Minimum two server
a. Principal – Production, source in Mirroring
b. Mirror- Stand by , target in Mirroring
c. Witness server (Optional)
2. Database Must be in ‘Full’ Recovery model
3. Take full backup of Principal database and restore it in mirror w/n
4. Take a log backup of principal database and restore in mirror w/n
5. Service account should have GRANT connect permissions on endpoints
Types/Modes of Mirroring:
1. Synchronous:
a. High protection/High Safety – The transaction will be first applied on the mirror server,
then based on commit acknowledgement.. Transaction will be committed on the principal. So
there won’t any transaction loss, at any point of time the principal and mirror will be in sync.
b. High Availability mode – if we add third witness serve to High safety mode, that’s
becomes a high availability mode and we can achieve database level automatic failover. The
witness server continuously monitoring your principal and mirror, when ever principal goes
down, it will bring mirror online automatically.
2. Asynchronous
a. High Performance Mode – The transaction will be immediately committed on the
principal and then send it to mirror and it does not wait for any acknowledgement. There can be
a case, where the transaction is committed successfully on principal but failed on mirror. There
can some data mismatch... if the principal is down, so there can some data loss. The advantage
is performance will be good.
Demo:
backup database [MirrorDemo]
to disk='F:\SQLLab\mirroring\mirrordemo.bak'
restore filelistonly
from disk='F:\SQLLab\mirroring\mirrordemo.bak'
Changing modes:
Adding Witness:
Mirroring States:
• Synchronized
• Synchronizing
• Disconnected
• Suspended
• Failover Pending
Mirroring state Description
SYNCHRONIZING The contents of the mirror database are lagging behind the contents of
the principal database. The principal server is sending log records to the mirror server, which is
applying the changes to the mirror database to roll it forward.
At the start of a database mirroring session, the database is in the SYNCHRONIZING state. The
principal server is serving the database, and the mirror is trying to catch up.
SYNCHRONIZED When the mirror server becomes sufficiently caught up to the principal
server, the mirroring state changes to SYNCHRONIZED. The database remains in this state as
long as the principal server continues to send changes to the mirror server and the mirror server
continues to apply changes to the mirror database.
If transaction safety is set to FULLautomatic failover and manual failover are both supported in
the SYNCHRONIZED state, there is no data loss after a failover.
If transaction safety is off, some data loss is always possible, even in the SYNCHRONIZED
state.
SUSPENDED The mirror copy of the database is not available. The principal database is
running without sending any logs to the mirror server, a condition known as running exposed.
This is the state after a failover.
A session can also become SUSPENDED as a result of redo errors or if the administrator
pauses the session.
SUSPENDED is a persistent state that survives partner shutdowns and startups.
PENDING_FAILOVER This state is found only on the principal server after a failover has
begun, but the server has not transitioned into the mirror role.
When the failover is initiated, the principal database goes into the PENDING_FAILOVER state,
quickly terminates any user connections, and takes over the mirror role soon thereafter.
DISCONNECTED The partner has lost communication with the other partner.
Replication
It is basically object level solution which replicates whatever the objects we want replicates.
Tracer Tokens:
Gives the latency information in replication
Appwiz.cpl Add/Remove
Programs