- SQL Basics
- SQL Introduction
- SQL Syntax
- SQL Sample Database
- SQL SELECT
- SQL WHERE
- SQL ORDER BY
- SQL DISTINCT
- SQL LIMIT
- SQL FETCH
- SQL AND
- SQL OR
- SQL BETWEEN
- SQL IN
- SQL LIKE
- SQL IS NULL
- SQL Comparison Operators
- SQL Logical Operators
- SQL Alias
- SQL CASE
- Joins and Subqueries
- SQL INNER JOIN
- SQL LEFT JOIN
- SQL RIGHT JOIN
- SQL FULL OUTER JOIN
- SQL SELF JOIN
- SQL CROSS JOIN
- SQL Subquery
- SQL Correlated Subquery
- SQL UNION
- SQL INTERSECT
- SQL EXCEPT
- Aggregate Functions
- SQL AVG
- SQL COUNT
- SQL MAX
- SQL MIN
- SQL SUM
- SQL GROUP BY
- SQL HAVING
- SQL ROLLUP
- SQL CUBE
- SQL GROUPING SETS
- Database Management
- SQL CREATE DATABASE
- SQL ALTER DATABASE
- SQL DROP DATABASE
- SQL BACKUP DATABASE
- SQL SHOW DATABASES
- SQL SELECT DATABASE
- Table Management
- SQL CREATE TABLE
- SQL ALTER TABLE
- SQL ADD COLUMN
- SQL DROP COLUMN
- SQL DROP TABLE
- SQL TRUNCATE TABLE
- SQL SHOW TABLES
- SQL RENAME TABLE
- SQL Constraints
- SQL Primary Key
- SQL Foreign Key
- SQL UNIQUE Constraint
- SQL CHECK Constraint
- SQL NOT NULL Constraint
SQL BACKUP DATABASE
The BACKUP DATABASE
statement is used to create a backup copy of a database to prevent data loss. It ensures that data can be restored in case of failure or corruption.
Key Features of SQL BACKUP DATABASE
- Creates a copy of the database for recovery.
- Requires administrative privileges.
- Supports full, differential, and transaction log backups.
SQL BACKUP DATABASE Syntax (For SQL Server)
BACKUP DATABASE database_name
TO DISK = 'file_path\backup_file.bak';
- database_name: The name of the database to back up.
- file_path: The location where the backup file will be stored.
- backup_file.bak: The name of the backup file.
Example of SQL BACKUP DATABASE (Full Backup)
BACKUP DATABASE SchoolDB
TO DISK = 'C:\Backup\SchoolDB.bak';
Output:
The SchoolDB
database is backed up to the specified location.
Creating a Differential Backup
A differential backup contains only the changes made since the last full backup.
BACKUP DATABASE SchoolDB
TO DISK = 'C:\Backup\SchoolDB_Diff.bak'
WITH DIFFERENTIAL;
Output:
Only the modified data after the last full backup is saved.
Creating a Transaction Log Backup
A transaction log backup saves all changes made since the last transaction log backup.
BACKUP LOG SchoolDB
TO DISK = 'C:\Backup\SchoolDB_Log.trn';
Output:
The transaction logs are saved to the specified file.
Summary
BACKUP DATABASE
is used to create a full backup of a database.WITH DIFFERENTIAL
backs up only modified data since the last full backup.BACKUP LOG
saves transaction logs to restore point-in-time changes.- Always store backups securely to prevent data loss.