The document provides SQL commands for backing up, restoring, and managing databases, specifically for the AdventureWorks2014 database. It includes steps for backing up transaction logs, restoring full and differential backups, and managing access roles and indexes. Additionally, it discusses the importance of index usage statistics and how to create, alter, and drop indexes in SQL Server.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
6 views9 pages
Dbdocs
The document provides SQL commands for backing up, restoring, and managing databases, specifically for the AdventureWorks2014 database. It includes steps for backing up transaction logs, restoring full and differential backups, and managing access roles and indexes. Additionally, it discusses the importance of index usage statistics and how to create, alter, and drop indexes in SQL Server.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9
Recovery,Backup & Restore flashcard
dimanche 20 octobre 2024 11:32
BACKUP DATABASE [….]
TO DISK = "…" WITH NOFORMAT, NOINIT, NAME = "… .bak", SKIP, NOREWIND, NOUNLOAD, STATS = 1;
BACKUP DATABASE […..]
TO DISK ="…." WITH NOFORMAT, NOINIT, NAME = "…. .bak" SKIP, NOREWIND, NOUNLOAD, STATS = 1, DIFFERENTIAL;
BACKUP LOG [….]
TO DISK = "…" WITH NOFORMAT, NOINIT, NAME = "… .bak", SKIP, NOREWIND, NOUNLOAD, STATS = 1;
RESTORE DATABASE [AdventureWorks2014]
FROM DISK = "…" WITH RECOVERY, MOVE 'AdventureWorks2014_Data'
Nouvelle section 1 Page 1
RESTORE DATABASE [AdventureWorks2014] FROM DISK = "…" WITH RECOVERY, MOVE 'AdventureWorks2014_Data' TO 'somepath/AdventureWorks2014_Data.mdf', MOVE N'AdventureWorks2014_Log' TO 'somepath\AdventureWorks2014_Log.ldf', NOUNLOAD;
-- Step 1: Backup the Transaction Log
BACKUP LOG [AdventureWorks2014] TO DISK = '…' WITH NOFORMAT, NOINIT, NAME = 'AdventureWorks2014_LogBackup_2022-02-11_07-49-49', NOSKIP, NOREWIND, NOUNLOAD
-- Step 2: Restore the Full Backup
RESTORE DATABASE [AdventureWorks2014Backup4] FROM DISK = '…. .bak' WITH FILE = 2, MOVE N'AdventureWorks2014_Data' TO '….\AdventureWorks2014Backup4_Data.mdf', MOVE N'AdventureWorks2014_Log' TO '….\AdventureWorks2014Backup4_Log.ldf', NORECOVERY, NOUNLOAD
-- Step 3: Restore the Differential Backup
RESTORE DATABASE [AdventureWorks2014Backup4] FROM DISK =' …\AdventureWorks2014_LogBackup_2022-02-04_08-42-27.bak' WITH FILE = 3, NORECOVERY, NOUNLOAD
-- Step 4: Restore the First Transaction Log Backup
RESTORE LOG [AdventureWorks2014Backup4] FROM DISK = '…\AdventureWorks2014_LogBackup_2022-02-04_08-42-27.bak' WITH FILE = 4, NORECOVERY, NOUNLOAD
-- Step 5: Restore the Second Transaction Log Backup with STOPAT
RESTORE LOG [AdventureWorks2014Backup4] FROM DISK = '…\AdventureWorks2014_LogBackup_2022-02-11_07-49-49.bak' WITH NOUNLOAD STOPAT = N'2022-02-11T07:49:58';
RESTORE DATABASE [AdventureWorks2014backup]
FROM DISK = '…\AdventureWorks2014_LogBackup_2022-01-27_13 18-47.bak' WITH CONTINUE_AFTER_ERROR, NORECOVERY, FILE = 6 ALTER DATABASE [AdventureWorks2014backup] SET SINGLE_USER WITH ROLLBACK IMMEDIATE DBCC CHECKDB ([AdventureWorks2014backup], REPAIR_ALLOW_DATA_LOSS) ALTER DATABASE [AdventureWorks2014backup] SET MULTI_USER
Nouvelle section 1 Page 2
Nouvelle section 1 Page 3 Access Management jeudi 24 octobre 2024 12:04
ALTER SERVER ROLE [sysadmin] ADD MEMBER [DESKTOP-ICEQ7B9\SQLTest]
Nouvelle section 1 Page 1
CREATE SERVER ROLE [myServerRole1] ALTER SERVER ROLE [myServerRole1] ADD MEMBER [DESKTOP-ICEQ7B9\SQLTest] GRANT ALTER ANY LOGIN TO [myServerRole1]
ALTER ROLE [db_datareader] ADD MEMBER [SQLTest]
CREATE ROLE [myNewDBRole]
ALTER ROLE [myNewDBRole] ADD MEMBER [SQLTest] GRANT SELECT ON [HumanResources].[Department] TO [myNewDBRole] DENY SELECT ON [HumanResources].[Employee] TO [myNewDBRole]
Nouvelle section 1 Page 2
DENY SELECT ON [HumanResources].[Employee] TO [myNewDBRole]
bulk insert [dbo].[flatFile] from '….' with (FIELDTERMINATOR=',', ROWTERMINATOR='\n', FIRSTROW=2 )
Nouvelle section 1 Page 1
Indexes jeudi 31 octobre 2024 19:10
CREATE CLUSTERED INDEX [IX_NewTable_ID] CREATE NONCLUSTERED INDEX [Index]
ON [dbo].[NewTable] ( [ID] ASC ) ON [dbo].[NewTable] ( [Column1] ASC ) INCLUDE([Column2]) DROP INDEX [Index] ON [dbo].[NewTable]
ALTER INDEX [Index_name]
ON [dbo].[Table_Name] REORGANIZE
ALTER INDEX [IX_NewTable_ID]
ON [dbo].[NewTable] REBUILD PARTITION = ALL WITH (ONLINE = ON)
ALTER INDEX [Index_name]
ON [dbo].[Table_name] REBUILD PARTITION = ALL WITH (FILLFACTOR = 80)
Nouvelle section 1 Page 1
CREATE NONCLUSTERED INDEX [Index_Adress] ON [Person].[Address] ( [AddressID] ASC ) WHERE city='London'
sys.dm_db_index_usage_stats
Value Good Sign Bad Sign Why
User seeks Seeks indicate that the data has been retrieved directly without having to scan unnecessary rows. A high value of seeks means that the index is used frequently and so its creation was a wise choice. User_scans Scans indicate that the server had to scan multiple rows to find the needed data. A high value of scans possibly indicates that → The index isn't covering the right/sufficient data for the common query it's supposed to speed up (e.g the indexed columns don't match the ones specified in the where clause of the query) → The index is very fragmented so big chunks of the pages need to be scanned to retriever needed data → [if the index is filtered]The filter is not appropriateso the server has to fallback to scanning the table (e.g most queries look for adresses with city ="Benzart" but the index on address has a city="London" filter)
User_updates Indexes are very expensive to update.
select * from sys.dm_db_index_usage_stats as stats
join sys.indexes as si on stats.object_id=si.object_id and stats.index_id=si.index_id sys.dm_db_index_usage_stats
ALTER INDEX [Index_name] ON [dbo].[Table_name] DISABLE