Site icon Wicked Yoda's

Comprehensive Tutorial: Completing the DAD-220 Module 2 Lab (Step-by-Step for Beginners)

Welcome to this detailed, beginner-friendly tutorial. We’ll cover each step in-depth, ensuring you understand exactly what each command does and how to use it effectively.


Step 1: Prepare Your Database Environment

  1. Open MySQL Workbench (or your preferred MySQL tool).
  2. Connect to your MySQL server:

Step 2: Create Your Database

  1. Open a new SQL tab and type the following command:
CREATE DATABASE sales_db;
  1. Run the command (press the lightning bolt icon or Ctrl+Enter).
  2. Verify the database creation by typing:
SHOW DATABASES;
  1. Select your new database to work with:
USE sales_db;

Step 3: Creating the Tables

Creating the customers Table:

  1. Type and execute the following:
CREATE TABLE customers (
  customer_id INT PRIMARY KEY AUTO_INCREMENT,
  first_name VARCHAR(50),
  last_name VARCHAR(50),
  email VARCHAR(100)
);

Creating the orders Table:

  1. Similarly, execute:
CREATE TABLE orders (
  order_id INT PRIMARY KEY AUTO_INCREMENT,
  customer_id INT,
  order_date DATE,
  amount DECIMAL(10,2),
  FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);
  1. Confirm tables exist:
SHOW TABLES;

Step 4: Adding Data to Tables

Insert data into customers:

  1. Enter the following SQL:
INSERT INTO customers (first_name, last_name, email) VALUES
('John', 'Doe', 'john.doe@example.com'),
('Jane', 'Smith', 'jane.smith@example.com');

Insert data into orders:

  1. Next, enter:
INSERT INTO orders (customer_id, order_date, amount) VALUES
(1, '2023-01-15', 250.50),
(2, '2023-01-20', 125.75);
  1. Verify data entry:
SELECT * FROM customers;
SELECT * FROM orders;

Step 5: Running and Understanding Queries

Basic Selection:

  1. Find all orders greater than $150:
SELECT * FROM orders WHERE amount > 150;

Combining Tables (Join):

  1. Display each customer’s name alongside their orders:
SELECT customers.first_name, customers.last_name, orders.order_date, orders.amount
FROM customers
JOIN orders ON customers.customer_id = orders.customer_id;

Step 6: Documentation

-- This query selects all customers
SELECT * FROM customers;

Exit mobile version