> CREATE DATABASE sales;
mysql
1 row affected (0.00 sec) Query OK,
OLTP database - MySQL
Objectives
I’ll be working this project at times on the cloud and at times on the local machine. We’ll start with these steps:
- design the schema for OLTP database.
- load data into OLTP database.
- automate admin tasks.
Start MySQL Server
From the cloud, choose DataBases > MySQL > Create an instance > MySQL CLI >
Create DB
Create table named sales_data based on the image below
# Change to the correct db
> USE sales;
mysql
Database changed
CREATE TABLE sales_data (
product_id INT NOT NULL,
customer_id INT NOT NULL,10,2) NOT NULL,
price DECIMAL(
quantity INT,
timestamp TIMESTAMP;
)0 rows affected (0.03 sec)
Query OK,
> SHOW tables;
mysql+-----------------+
| Tables_in_sales |
+-----------------+
| sales_data |
+-----------------+
1 row in set (0.00 sec
Load Data
- Download the file oltpdata.csv to local drive, from https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DB0321EN-SkillsNetwork/oltp/oltpdata.csv
- Import the data from oltpdata.csv into sales_data table using phpMyAdmin.
Count Rows
> SELECT count(*) FROM sales_data;
mysql+----------+
| count(*) |
+----------+
| 2605 |
+----------+
1 row in set (0.01 sec)
Create an Index
Create an index named ts on the timestamp field.
> CREATE INDEX ts ON sales_data(timestamp);
mysql
0 rows affected (0.09 sec)
Query OK, 0 Duplicates: 0 Warnings: 0 Records:
List Indexes
> SHOW INDEX FROM sales_data;
mysql+------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| sales_data | 1 | ts | 1 | timestamp | A | 2605 | NULL | NULL | YES | BTREE | | | YES | NULL |
+------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
1 row in set (0.01 sec)
Write Script to Export Data
Write a bash script named datadump.sh
that exports all the rows in the sales_data table to a file named sales_data.sql
Take a screenshot of the contents of the datadump.sh
bash file command you used and the output.
Name the screenshot as exportdata.jpg
. (images can be saved with either .jpg or .png extension)
End of assignment.
# In new terminal
touch datadump.sh# this will open up a file enter this into the file, save it
#!/bin/bash
# Backup the MySQL database
> sales_data.sql
mysqldump sales
"Backup completed: sales_data.sql" echo
# Run the bash file to backup the db
~:/home/project$ touch datadump.sh
~:/home/project$ bash datadump.sh
Backup completed: sales_data.sql