MySQL MariaDB

How to Use SAMPLE DATABASE in MySQL

If you are new to MySQL and looking to learn how to use a database, you will need to start by creating and populating a database with sample data. A sample database is a pre-built database with a well-defined schema and data that can be utilized for different purposes or to test your queries and applications.

This write-up will explain the usage of the SAMPLE DATABASE in MySQL.

How to Use SAMPLE DATABASE in MySQL?

To use the sample database in MySQL, follow the given below steps:

Step 1: Download Sample Database

Visit the official website of MySQL, navigate to the “Example Databases” section, and download the “ZIP” file of “sakila database”:

Step 2: Extract Sample Database

After the successful download, move to the download directory and extract the “ZIP” file:

Step 3: Copy Directory’s Path

Head into the extracted file directory and copy the path of that directory:

Step 4: Launch CMD

Press the “WINDOWS” button, type “CMD” and click on “OPEN”:

Step 5: Log Into MySQL

Type the given below command to log in to the MySQL server as a “root” user:

mysql -u root -p

Output

In the above output, it can be seen that the MySQL server has been logged in.

Step 6: Create the Sample Database Structure

Let’s execute the contents of a sample database file that was downloaded from the official website, by following the given below syntax:

SOURCE copiedpath/filename.sql

In the above syntax, SOURCE is utilized to execute a file’s code or contents.

Specify the “copiedpath” and “filename” in the above syntax as given below:

SOURCE C:/Users/md/Downloads/sakila-db/sakila-schema.sql;

In the above command, “sakila-schema.sql” is utilized to create the database structure.

Output

The output showed that the file had been executed.

Step 7: Fill the Database Structure

Let’s fill in the database structure with the sample data by executing the “sakila-data.sql” file:

SOURCE C:/Users/md/Downloads/sakila-db/sakila-data.sql;

Output

The output showed that the sample data of the sample database has been inserted.

Step 8: Confirm Database Creation

Let’s confirm if the database was created or not by typing the giving below command:

SHOW DATABASES;

This command is utilized to show the databases.

Output

The output displayed a database name “sakila” which is a sample database.

Step 9: Use Sample Database

To utilize the “sakila” database, the “USE” keyword can be utilized as given below:

USE sakila;

Output

The output depicts that the database has been changed, after that, you can use the database as per your need.

Note: The steps below are not compulsory; they are just a few actions you can perform after using the sample database in MySQL.

Step 10: Show Tables

The table of the sample database can be shown by utilizing the given below command:

SHOW TABLES;

Output

The output showed the table in the “sakila” (sample) database.

Step 11: Fetch Data From a Particular Table

To show the table from the sample database, simply use the “SELECT” statement with the table name as given below:

SELECT * FROM actor;

In the above example, the “actor” is a table.

Output

The output showed the table’s data.

Or it can be used with the built-in functions as provided below:

SELECT COUNT(*) FROM actor;

In the above command, the “COUNT()” function is utilized to count the number of rows in a particular table.

Output

That’s all about using a sample database in MySQL.

Conclusion

To use a sample database in MySQL, you need to download and extract the database files, log in to the MySQL server, and execute the database schema and data files to create and insert data. After inserting the data, you can interact with the database utilizing different commands such as “SELECT” and “SHOW TABLES”. This article has provided a step-by-step guide on how to use a sample database in MySQL.

About the author

Danish Ghafoor

I am a computer science graduate with a certification in cybersecurity. My passion for technology has led me to develop a deep understanding of the challenges and solutions in the field of information security. I am dedicated to staying up-to-date with the latest industry trends and technologies, and constantly seeking out new opportunities to expand my knowledge and skills.