Skip to content

Jenspi/databases

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

43 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ›๏ธ E-Commerce Database Management System 1.0

Oracle PL/SQL e-commerce DB project: design schemas, manage data, run queries, and modularize code for easy reuse & testing. . เผ˜หšโ‹†๐Ÿ›๏ธ ๏ฝกโ‹† ๐Ÿ›’ โ‹† ๐–ฆน.โœงหš


๐Ÿ“‹ Table of Contents

๐Ÿ“˜ Project Summary

This 16-week long project for Data Models and Database Management Systemsโ€“ CSCI4125 demonstrates practical use of Oracle PL/SQL to design, implement, and manage a relational database system with business logic and data consistency/accuracy. It showcases schema design, advanced SQL querying, and reusable/modular PL/SQL development through a realistic e-commerce data model.

๐ŸŽฏ Project Features

๐Ÿ“‘ Data Processing & Automation

  • Scalable to millions of records
  • Build data import/export workflows
  • Generate SQL INSERT statements from raw text files
  • Automatic data type detection for any dataset with the use of Java
  • Solidfy understanding of query logic by building flat file models in Java

๐Ÿ—„๏ธ Database Design & Architecture

  • Entity-Relationship (ER) modeling + mapping
  • Design and implement an e-commerce database (customers, orders, products, reviews)

๐Ÿ–‡๏ธ Business Logic & Operations

  • Write complex SQL queries and aggregations
  • Develop modular PL/SQL procedures, functions, and triggers to enforce business logic

๐Ÿ‘ฉ๐Ÿผโ€๐Ÿ’ป Developer Experience

  • No installation required โ€“ runs on free Oracle LiveSQL
    • Runtime execution output via DBMS_OUTPUT

(back to table of contents)

๐Ÿ”– Project Requirements

๐Ÿ“ฆ Oracle account
๐Ÿ“ฆ SQL Developer (if going the SQL Developer route)
๐Ÿ“ฆ Free Tier Oracle Database (if going the SQL Developer route)

๐Ÿš€ Run This Project

๐Ÿ”ญ Run With FreeSQL (No Installation Required)

๐Ÿ‘๏ธโ€๐Ÿ—จ๏ธ Oracle Cloud Setup

๐Ÿ“Œ Create an Oracle Cloud account and sign up for the Oracle Cloud Free Tier
https://www.oracle.com/cloud/free/
โญ๏ธ You will never be charged unless you explicitly upgrade to a different tier. You database will deactivate after being dormant for a certain number of days, and even then, they'll warn you via email to restart your database to keep it.

๐Ÿ“Œ Create a database in Oracle Cloud

โ†ณ Leave all default options (unless you are experiencedโ€“ I have no clue what any of it does).

โ†ณ Press Cloud page icon to get back to the database instance.

โ†ณ Click database

โ†ณ Database connection โ†’ Wallet Type = Instance Type โ†’ Download Wallet. ๐ŸšจDo not share this wallet with others.๐Ÿšจ

โŒจ๏ธ SQL Developer Setup & Execution

๐Ÿ“Œ Download SQL Developer
https://www.oracle.com/database/sqldeveloper/

Set a new database connection

โ†ณ add connection by clicking the green plus sign in the top left corner of your SQL Developer window

โ†ณ You'll see something like this. Fill it out the following way.
o Name: Name your connection. I used my project title.
o Username: ADMIN
o Password: Password. I used my Oracle Cloud password.
CONNECTION TYPE: Cloud wallet Browse > add wallet from Oracle Cloud that you got in [Oracle Cloud Setup](#oracle-cloud-setup) o Click the โ€œTestโ€ box at the bottom. If everything is correct, the โ€œStatusโ€ in the lower left-hand corner should say โ€œSuccessโ€.
o Click the โ€œSaveโ€ box at the bottom to save your connection.
o Click the โ€œConnectโ€ box.

Now, do the same steps but for a user account.


๐Ÿ“Œ Use SQL Developer to run SQL scripts

โ†ณ Run the entire> script

โ†ณ run only the selected statement in the script

Running files โ€ผ๏ธ Files located in in ECommerceDB/

โ†ณ Drag `setup.sql` into the SQL Developer window and press run. If it was successful, you will see tables appear in the Connections pane โ†’ Your database โ†’ Tables (expand) and results in the Script Output window! โ˜บ๏ธ๐ŸŽ‰

Drag each .sql script (in the order below) into the SQL Developer window and press the Run button. Check the Script Output tab for output.

  1. setup.sql โ€” Creates tables + populates all data except LineItem + adds Inventory column
  2. triggers.sql โ€” Creates inventory management system trigger + inserts LineItem data (trigger fires here)
    • will intentionally throw custom user errors when a product has no stock/not enough stock left
  3. procedures.sql โ€” Defines Invoice procedure + runs test calls
  4. queries.sql โ€” Optional test queries

โญ๏ธ If you mess up at any point, run setup.sql again and start over.


๐Ÿ”ฌ Run With FreeSQL (No Installation Required)

This project uses PL/SQL.
You can run all scripts for free on Oracle LiveSQL.

๐Ÿ“Œ Open LiveSQL/FreeSQL
https://freesql.com/

๐Ÿ“Œ Create a workspace (free)


Click โ€œLogin / Sign Upโ€ โ†’ create an Oracle account if you donโ€™t have one.


๐Ÿ“Œ How to run the files:


โ€ผ๏ธ Files located in in ECommerceDB/
Drag setup.sql into the SQL Worksheet window and press run. If it was successful, you will see the tables appear in the Navigator pane and results in the Query Results! โ˜บ๏ธ๐ŸŽ‰

Drag each .sql file (in the order below) into the SQL Worksheet window and press the Run button. Check the Query Results tab for output.

  1. setup.sql โ€” Creates tables + populates all data except LineItem + adds Inventory column
  2. triggers.sql โ€” Creates inventory management system trigger + inserts LineItem data (trigger fires here)
    • will intentionally throw custom user errors when a product has no stock/not enough stock left
  3. procedures.sql โ€” Defines Invoice procedure + runs test calls
  4. queries.sql โ€” Optional test queries

๐Ÿงช Expected Output

When running schema.sql, you should see output similar to the following in the DBMS Output panel: // TODO: picture here

PRODUCT COUNT: SHOULD BE 20
1 20
REVIEW COUNT: SHOULD BE 12
2 12
...

(back to table of contents)


โฃ๏ธ A Detailed Breakdown of the 10 Phases

๐Ÿ” Phase 1: Automatic Data Type Detection (Java Program)

Phase 1 Input: .txt or .csv file of data

Phase 1 Output: .csv of data types (built upon in Phase 3)

Java program that does a simple evaluation for the data types of values in a file. Scalable, volume-independent, and data-agnostic.

(back to table of contents)

๐Ÿ—‚๏ธ Phase 2: Entity-Relationship (ER) Modeling

My Entity-Relationship Model for the request quoted below.
Entities (in squares), attributes (in ovals) and keys (underlined), and the relationships (in triangles) (including cardinality and participation) among the online retail store.

Our database keeps track of customers and the orders they make. For each customer, we store a unique customer ID, name, and date of birth. We also allow customers to store multiple shipping addresses. We only allow New Orleanians to be customers so an address is just the street address; we donโ€™t store city, state or zip.

Once a customer registers their account, they can place as many orders as they want. However, an order is only made by one customer, no split orders. For each order, we store a unique confirmation number, the order date, the cost of products in the order, the shipping cost, and the total order cost. The cost of the products is computed from the sum of all the items and their quantities. The shipping cost is determined based on the cost of the products. If the order is at least $35, we offer free shipping. Otherwise, we charge a standard $10 for shipping. The total cost is the sum of the product costs and the shipping cost.

Orders may include many products and a product may be part of many orders. For each product, we store a unique ID, name, price, and the inventory. When a product is included in an order, we also record the quantity of the product included in the order; this quantity also needs to be subtracted from the inventory.

We also store anonymous reviews for products. For each review, we store a number, which is only unique for each product, a rating (1 to 5), and the review text, which is limited to 255 characters.

We also have a rewards program where customers can receive credit for referring other customers. A current customer can refer as many people they want (for more credits), but only one person can receive credit for referencing another customer.

(back to table of contents)

๐Ÿงฌ Phase 3: Entity-Relationship (ER) Mapping & Generating SQL Insert Statements

๐Ÿงฑ Phase 3 Part 1โ€“ ER Mapping for online retail store

Developing a complete logical schema (ER Map) based on Phase 2's ER Model.

๐Ÿ–จ๏ธ Phase 3 Part 2โ€“ Generating SQL Insert Statements

Phase 3 Input: .txt file of data

Phase 3 Output: .csv of SQL insert statements (built on Phase 1)

Phase 3 builds on the previous Phase 1, which was a Java program that determines data types of values in a file. Phase 3 takes our data from a text file and automatically generates SQL insert statements for the data.

โ“ Why is automating the generation of SQL insert statements important?

  • Less Copy/Pasting SQLโ€“ automates SQL insert statements for you to copy/paste into a .sql file.
  • Scalabilityโ€“ works with thousands or millions of records to populate.
  • Even Your Grandma Could Do Itโ€“ a non-CS end-user can pass in data without needing to write any SQL.

(back to table of contents)

๐Ÿ—๏ธ Phase 4: SQL DDL & Populating the Database

๐Ÿ› ๏ธ Phase 4 Part 1โ€“ SQL DDL

SQL DDL script to define my tables Customer, Address, Product, Review, Orders, and LineItem.

Phase 4 builds on our previous phase. Here, we are defining a DDL to run in SQL Developer. Then, we can move on to Part 2 to populate our database, also in SQL Developer.

๐Ÿ“ฅ Phase 4 Part 2โ€“ Populating the Database

Phase 4 Input: six .txt files of data.


Phase 4 Output: six SQL scripts containing INSERT statements, using Phase 3's Java program.

Here, we populate our 6 brand new tables by putting our input into our Phase 3 Java code that automates SQL insert statements, taking the output from that program, and putting it into SQL Developer.

If you want to verify that all records were populated, you can run SELECT COUNT(*) FROM [table name] in SQL Developer. This should return 20 for Customer, 15 for Address, 20 for Product, 12 for Review, 50 for Orders, and 100 for Lineitem.

Run SQL scripts in SQL Developer in the order of the following to guarantee referential integrity:
Product,
Review,
Customer,
Address,
Orders,
LineItem

(back to table of contents)

๐ŸŒฑ Phase 5: โœจIntroductoryโœจ SQL Retrieval Queries & Query Processing

๐Ÿ“ค Phase 5 Part 1โ€“ SQL Retrieval Queries


Introductory SQL Retrieval Query Input


Introductory SQL Retrieval Query Code


Introductory SQL Retrieval Query Results

Phase 5 is based on Introductory SQL: SQL Retrieval Queries. A bunch of practice problems!

๐Ÿ”„ Phase 5 Part 2โ€“ Query Processing


Introductory SQL Retrieval Query Input


Flat File Model (Java Program) of SQL Retrieval Query


SQL Retrieval Query Result For Java Program to Replicate


Flat File Model Result

Query #8: Find the name of the product with the highest price. Do not hardcode any prices or other values โ€“ you must use SQL without assuming you know the current database snapshot.

One of benefits of SQL is the program-data independence supported by the relational model (or the DBMS). To fully understand the power of SQL, it is helpful to know how the equivalent could be implemented in the flat file model. In this task, we wrote a Java method that is the flat file model solution for Query #8 in Part 1.

(back to table of contents)

๐Ÿ“ˆ Phase 6: โœจIntermediateโœจ Retrieval Queries & Understanding Query Processing

๐Ÿงฎ Phase 6 Part 1โ€“ Intermediate Retrieval Queries


Intermediate SQL Retrieval Query Code


Intermediate SQL Retrieval Query Code Results

Phase 6 is based on Inter SQL: Intermediate Retrieval Queries. Wrote queries (without hardcoding) to find the following:

  1. Find the names of all customers who have at least one address
  2. For each product id, list the total quantity sold
  3. Find the names of all products that do not have any reviews
  4. Find the names of all customers who were referred by 'Margot Robbie'. Note: You must use the string โ€˜Margot Robbieโ€™ and not hardcode the ID.

๐Ÿงต Phase 6 Part 2โ€“ Understanding Query Processing


Flat File Model (Java Program) of SQL Retrieval Queries #1 & #2


SQL Retrieval Query Result For Java Program to Replicate


Flat File Model Result

Query #1: Find the names of all customers who have at least one address
Query #2: For each product id, list the total quantity sold

One of benefits of SQL is the program-data independence supported by the relational model (or the DBMS). To fully understand the power of SQL, it is helpful to know how the equivalent could be implemented in the flat file model. In this task, we wrote a Java method that is the flat file model solution for Queries #1 and #2 in Part 1.

(back to table of contents)

๐Ÿ” Phase 7: Intermediate Retrieval Queries + Transactions & Serialization

๐Ÿ“Š Phase 7 Part 1โ€“ Intermediate Retrieval Queries


More Intermediate SQL Retrieval Query Code


Intermediate SQL Retrieval Query Code Results

More Inter SQL: Intermediate Retrieval Queries. Wrote queries (without hardcoding) to find the following:

  1. Retrieve the names of all products which have sold a total quantity greater than 30.
  2. Retrieve the names of all customers who were referred by someone who was referred by the customer with ID C15. Ex. Return CustomerA who was referred by CustomerB where CustomerB was referred by customer C15
  3. Find the customer name's that have ordered the least expensive product
  4. Find the avg review rating for each product that has received a review (do no include products that have not received a review).
  5. List the total cost for each order (quantity * price for all products for each order ID) where the order cost is greater than 1000.
  6. List customers that have spent more than $1000 on orders
  7. Find the total number of orders placed by customers who were referred by Margot Robbie; use the regular expression function REGEXP_LIKE.
  8. Find all customer names where both the first name and last name begin with a vowel

๐Ÿ” Phase 7 Part 2โ€“ Transactions & Serialization


Precedence graphs for S1 and S2

Consider the three transactions T1, T2, and T3, and the schedules S1 and S2 given below. Draw the serializability (precedence) graphs for S1 and S2, and state whether each schedule is serializable or not. If a schedule is serializable, write down the equivalent serial schedule(s).

T1: R1(X), R1(Z), W1(X) T2: R2(Z), R2(Y), W2(Z), W2(Y) T3: R3(X), R3(Y), W3(Y)

S1: R1(X), R2(Z), R1(Z), R3(X), R3(Y), W1(X), W3(Y), R2(Y), W2(Z), W2(Y) S2: R1(X), R2(Z), R3(X), R1(Z), R2(Y), R3(Y), W1(X), W2(Z), W3(Y), W2(Y)

(back to table of contents)

โš™๏ธ Phase 8: PL/SQL Triggers & PL/SQL Procedures

๐Ÿšจ Phase 8 Part 1โ€“ PL/SQL Triggers


SQL trigger that will maintain the inventory for products.

Wrote a trigger that will fire when we INSERT a row into the Lineitem table. This trigger will check the value of Inventory for the corresponding ProductID.

๐Ÿงช Phase 8 Part 2โ€“ PL/SQL Procedures


Example invoice return


PL/SQL procedure that generates an invoice for a given customer ID and order date

Wrote a PL/SQL procedure that generates an invoice for a given customer ID and order date. The invoice will compute some of the derived columns we saw in Phase 2, specifically product cost, shipping cost, and total cost.

(back to table of contents)

๐Ÿ”Œ Phase 9: Java Database Connectivity (JDBC) with Prepared Statements



Java application that generates an invoice for a given customer ID and order date.


Generated invoices when passing in Customer ID and Order Date; handles no invoice as well.

Wrote a Java application that generates an invoice for a given customer ID and order date. The invoice will compute some of the derived columns we saw in Phase 2, specifically product cost, shipping cost, and total cost.

(back to table of contents)

๐Ÿงผ Phase 10: Normalization

๐Ÿ“ Phase 10 Part 1โ€“ Product Relation


Part 1 Info: New PRODUCT Relation


Removed any partial key dependencies to create PRODUCT Relation in Second Normal Form


Removed any transitive dependencies to create PRODUCT Relation in Third Normal Form

Since we've originally designed the web store database, fields have been added to the original PRODUCT relation. We start to notice some data inconsistencies (due to update anomalies caused by data redundancy). Our job is to evaluate and fix these problems.

๐Ÿ‘ฅ Phase 10 Part 2โ€“ Customer Relation


Part 2 Info: New CUSTOMER relation to store (multiple) payment methods for customers.


Proper 1NF to allow multiple payment methods for each customer.

Someone in our organization created the new CUSTOMER relation above to store (multiple) payment methods for customers. To allow multiple payment methods for each customer, we had to alter the relation so that it is in proper 1NF.


Redundancies and decomposition on functional dependencies.

With 1NF introducing redundancies (payment methods), we have to decompose on functional dependencies. Decompose on this functional dependency:
CustomerID โ†’ Name, DateOfBirth, ReferrerID

Resulting decomposition:
CUSTOMER(CustomerID, Name, DateOfBirth, ReferrerID)
CUSTOMER_PAYMENT(CustomerID, PaymentMethod)


New relation(s) in proper 3NF.

Using the functional dependencies identified, we then show the new relation(s) in proper 3NF.

๐Ÿ—บ๏ธ Phase 10 Partโ€“ Updated Project Schema


Updated schema for the entire web store in proper 3NF.

Using information gathered from parts 1 and 2, we drew the updated schema for the entire web store in proper 3NF.
Note: Parts of Phase 10 may be incorrect due to not saving professor feedback :(

(back to table of contents)

About

Oracle PL/SQL e-commerce DB project: design schemas, manage data, run queries, and modularize code for easy reuse & testing. . เผ˜หšโ‹†๐Ÿ›๏ธ ๏ฝกโ‹† ๐Ÿ›’ โ‹† ๐–ฆน.โœงหš

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Contributors

Languages