Project 3
Project 3
Output:
Table format:
Creating table for Shopping Carts:
Output:
Output:
Creating table for Orders:
Columns:
Output:
Trigger function to prevent negative quantities in OrderItems:
Output:
Output:
OrderItems:
Output:
promote reusability.
Output:
b. Calculate Average Price of Books by an Author
Output:
Output:
4. Apply normalization techniques to optimize the database design and
minimize data redundancy. Identify functional dependencies and normalize
the tables to achieve higher normal forms (e.g., 1NF, 2NF, 3NF). Use
SQL's ALTER TABLE statements to modify the table structure as needed.
Ensure that the table has atomic values and each record is unique. This is
already achieved in the given schema.
Ensure that all non-key attributes are fully functionally dependent on the
primary key. Our current tables appear to be in 2NF since they have a single
primary key and no partial dependencies.
Ensure that all non-key attributes are not transitively dependent on the
primary key. Let's review each table to ensure it adheres to 3NF.
Based on the current schema, it appears that all tables are already normalized
up to 3NF.
ERD Description
Users Table:
o Attributes: user_id, username, email, password, is_admin
o Primary Key: user_id
Books Table:
o Attributes: book_id, title, author, price
o Primary Key: book_id
ShoppingCarts Table:
o Attributes: cart_id, user_id
o Primary Key: cart_id
o Foreign Key: user_id references Users(user_id)
CartItems Table:
o Attributes: cart_item_id, cart_id, book_id, quantity
o Primary Key: cart_item_id
o Foreign Keys: cart_id references ShoppingCarts(cart_id), book_id
references Books(book_id)
Orders Table:
o Attributes: order_id, user_id, order_date, shipping_address,
billing_address, total_amount
o Primary Key: order_id
o Foreign Key: user_id references Users(user_id)
OrderItems Table:
o Attributes: order_item_id, order_id, book_id, quantity, price
o Primary Key: order_item_id
o Foreign Keys: order_id references Orders(order_id), book_id references
Books(book_id)
Output:
Output:
7. Utilize SQL’s data import and export functionality to migrate data between
different databases or systems. Use SQL statements like COPY or IMPORT to
load data from external files, and employ EXPORT or COPY TO statements to
extract data from the database.
Suppose you have a CSV file named books.csv with the following content:
title,author,price
"1984","George Orwell",8.99
Query:
Task: Find users who have spent more than $20 on their orders
Grouping by Book and Counting Sales
Task: Find out how many times each book has been sold.
Task: Find out the total quantity of books sold and the total revenue
generated by each book.
Combining GROUP BY with JOIN
Task: Find out the total revenue generated by each book, along with the book
title and author.
We'll create a stored procedure named place_order that accepts user ID, book
ID, quantity, shipping address, and billing address as parameters. The
procedure will perform the following steps:
Output: