Creating a Database and Verifying Its Data and Log Files
go
use sqldbanow
go
you will see the message “Changed database context to ‘DVSQLRocks'”.
5. Create a table, major_conference, and insert two rows of data:
create table major_conference (id int, name nvarchar(50), start_date datetime)
go
insert into major_conference values (1, ‘SQL Saturday LA’, ‘2019-06-15’)
insert into major_conference values (2, ‘SQL PASS Summit’, ‘2019-11-05’)
insert into major_conference values (3, ‘AWS Re:Invent’, ‘2019-12-02’)
go
Figure 1: Create a database and table and insert data into a table |
6. Find the location of the data and log files for the newly created databases:
select DB_NAME(database_id), physical_name from sys.master_files
where DB_NAME(database_id) = ‘sqldbanow’
go
Figure 2: Query the sys.master_files to find the location of the data and log files
To backup a database, perform the following steps:
1. Connect to your SQL Server if not already connected:
2. Backup our sample database, sqldbanow:
Figure 3: Backup a database If executed successfully, you will see the confirmation message as in Figure 3. Restoring up a Database To restore a database, perform the following steps: 1. Find the location of the backup file. In our example, it is /var/opt/mssql/data/sqldabanow_20210127.bak. 2. Connect to your SQL Server if not already connected: 3. Change the database to master:
Just as in SQL on Windows, you can’t restore a database if it is in use. 4. Restore the database. In this example, we will restore and replace the existing sqldbanow databases: Figure 4: Restore a database |