Skip to content

Instantly share code, notes, and snippets.

@ephrimlawrence
Created November 21, 2022 23:23
Show Gist options
  • Save ephrimlawrence/b73f137356247db92b06d1dfb549386e to your computer and use it in GitHub Desktop.
Save ephrimlawrence/b73f137356247db92b06d1dfb549386e to your computer and use it in GitHub Desktop.
The script demonstrates how to create a MySQL database with tables and some records in it
-- Create new a database
-- Replace 'hello_db' with any name of your choice
CREATE DATABASE hello_db;
--
-- Use the created database
-- Replace 'hello_db' with the same name your used to create the database
USE hello_db;
-- categories table
CREATE TABLE categories (
category_id INT,
category_name varchar(50) NOT NULL,
PRIMARY KEY (category_id)
);
-- customers table
CREATE TABLE customers (
customer_id INT,
last_name varchar(50) NOT NULL,
first_name varchar(50) NOT NULL,
favorite_website varchar(50),
PRIMARY KEY (customer_id)
);
-- departments table
CREATE TABLE departments (
dept_id INT,
dept_name varchar(50) NOT NULL,
PRIMARY KEY (dept_id)
);
-- employees table
CREATE TABLE employees (
employee_number INT,
last_name varchar(50) NOT NULL,
first_name varchar(50) NOT NULL,
salary INT,
dept_id INT,
PRIMARY KEY (employee_number),
FOREIGN KEY (dept_id) REFERENCES departments(dept_id)
);
-- orders table
CREATE TABLE orders (
order_id INT,
customer_id INT,
order_date date,
PRIMARY KEY (order_id),
FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);
-- products table
CREATE TABLE products (
product_id int NOT NULL,
product_name varchar(50) NOT NULL,
price INT NOT NULL,
category_id INT,
CONSTRAINT products_pk PRIMARY KEY (product_id),
FOREIGN KEY (category_id) REFERENCES categories(category_id)
);
-- Insert records into the categories table
INSERT INTO
categories (category_id, category_name)
VALUES
(25, 'Deli');
INSERT INTO
categories (category_id, category_name)
VALUES
(50, 'Produce');
INSERT INTO
categories (category_id, category_name)
VALUES
(75, 'Bakery');
INSERT INTO
categories (category_id, category_name)
VALUES
(100, 'General Merchandise');
INSERT INTO
categories (category_id, category_name)
VALUES
(125, 'Technology');
-- Insert records into the customers table
INSERT INTO
customers (
customer_id,
last_name,
first_name,
favorite_website
)
VALUES
(4000, 'Jackson', 'Joe', 'techonthenet.com');
INSERT INTO
customers (
customer_id,
last_name,
first_name,
favorite_website
)
VALUES
(5000, 'Smith', 'Jane', 'digminecraft.com');
INSERT INTO
customers (
customer_id,
last_name,
first_name,
favorite_website
)
VALUES
(
6000,
'Ferguson',
'Samantha',
'bigactivities.com'
);
INSERT INTO
customers (
customer_id,
last_name,
first_name,
favorite_website
)
VALUES
(7000, 'Reynolds', 'Allen', 'checkyourmath.com');
INSERT INTO
customers (
customer_id,
last_name,
first_name,
favorite_website
)
VALUES
(8000, 'Anderson', 'Paige', NULL);
INSERT INTO
customers (
customer_id,
last_name,
first_name,
favorite_website
)
VALUES
(9000, 'Johnson', 'Derek', 'techonthenet.com');
-- Insert records into the departments table
INSERT INTO
departments (dept_id, dept_name)
VALUES
(500, 'Accounting');
INSERT INTO
departments (dept_id, dept_name)
VALUES
(501, 'Sales');
-- Insert records into the employees table
INSERT INTO
employees (
employee_number,
last_name,
first_name,
salary,
dept_id
)
VALUES
(1001, 'Smith', 'John', 62000, 500);
INSERT INTO
employees (
employee_number,
last_name,
first_name,
salary,
dept_id
)
VALUES
(1002, 'Anderson', 'Jane', 57500, 500);
INSERT INTO
employees (
employee_number,
last_name,
first_name,
salary,
dept_id
)
VALUES
(1003, 'Everest', 'Brad', 71000, 501);
INSERT INTO
employees (
employee_number,
last_name,
first_name,
salary,
dept_id
)
VALUES
(1004, 'Horvath', 'Jack', 42000, 501);
-- Insert records into the orders table
INSERT INTO
orders (order_id, customer_id, order_date)
VALUES
(1, 7000, '2016/04/18');
INSERT INTO
orders (order_id, customer_id, order_date)
VALUES
(2, 5000, '2016/04/18');
INSERT INTO
orders (order_id, customer_id, order_date)
VALUES
(3, 8000, '2016/04/19');
INSERT INTO
orders (order_id, customer_id, order_date)
VALUES
(4, 4000, '2016/04/20');
INSERT INTO
orders (order_id, customer_id, order_date)
VALUES
(5, null, '2016/05/01');
-- Insert records into the products table
INSERT INTO
products (product_id, product_name, category_id, price)
VALUES
(1, 'Pear', 50, 1000);
INSERT INTO
products (product_id, product_name, category_id, price)
VALUES
(2, 'Banana', 50, 2000);
INSERT INTO
products (product_id, product_name, category_id, price)
VALUES
(3, 'Orange', 50, 500);
INSERT INTO
products (product_id, product_name, category_id, price)
VALUES
(4, 'Apple', 50, 7000);
INSERT INTO
products (product_id, product_name, category_id, price)
VALUES
(5, 'Bread', 75, 200);
INSERT INTO
products (product_id, product_name, category_id, price)
VALUES
(6, 'Sliced Ham', 25, 600);
INSERT INTO
products (product_id, product_name, category_id, price)
VALUES
(7, 'Kleenex', null, 10000);
@MarkValentineAikins
Copy link

You did not create any relationships among the tables, so I could not use for practice SQL Join

@ephrimlawrence
Copy link
Author

ephrimlawrence commented Sep 17, 2023

@MarkValentineAikins Actually, there are relationships among the tables, it was not explicitly created using the foreign key statement. For example, dept_id (foreign key) in the employees table, category_id in products table and customer_id in the orders table.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment