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:
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:
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:
In the above command, “sakila-schema.sql” is utilized to create the database structure.
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:
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:
This command is utilized to show the databases.
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:
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:
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:
In the above example, the “actor” is a table.
The output showed the table’s data.
Or it can be used with the built-in functions as provided below:
In the above command, the “COUNT()” function is utilized to count the number of rows in a particular table.
That’s all about using a sample database in MySQL.
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.