This document provides step-by-step instructions on how to create a database named TufftoysDB and tables for managing customer, item, delivery method, sales rep, order, invoice, and payment data. It includes SQL statements for creating the tables and inserting sample data. The document also includes queries for retrieving information from the tables.
Contribute Materials
Your contribution can guide someone’s learning journey. Share your
documents today.
Script Drop database if exists TufftoysDB; create database TufftoysDB; Use TufftoysDB; /* Create Database */ DROP TABLE IF EXISTS Customer; CREATE TABLE Customer ( customerNumber int not null, customerName varchar(40) NOT NULL, phone varchar(20) NOT NULL, email varchar(30) NOT NULL, address varchar(50) NOT NULL, balance double NOT NULL, creditLimit int NOT NULL, Primary Key (customerNumber) ); DROP TABLE IF EXISTS Item; CREATE TABLE Item (
Secure Best Marks with AI Grader
Need help grading? Try our AI Grader for instant feedback on your assignments.
itemCode int not null, description varchar(40) NOT NULL, category varchar(20) NOT NULL, suggestedRetail varchar(30) NOT NULL, cost varchar(50) NOT NULL, overseas double NOT NULL, QOH int NOT NULL, minStockLevel int NOT NULL, Primary Key (itemCode) ); DROP TABLE IF EXISTS DeliveryMethod; CREATE TABLE DeliveryMethod ( deliveryMethod varchar(20) not null, charge double NOT NULL, Primary Key (deliveryMethod) ); DROP TABLE IF EXISTS SalesRep; CREATE TABLE SalesRep ( salesRepID int not null, salesRepName varchar(40) NOT NULL, address varchar(50) NOT NULL,
mobile varchar(20) NOT NULL, email varchar(30) NOT NULL, hiredDate datetime NOT NULL, agreedSalary double NOT NULL, commissionRate int NOT NULL, Primary Key (salesRepID) ); DROP TABLE IF EXISTS `Order`; CREATE TABLE `Order` ( orderNumber int not null, salesRepID int NOT NULL, orderDate datetime NOT NULL, requiredDate datetime NOT NULL, customerNumber int not null, deliveryMethod varchar(20) not null, Primary Key (orderNumber), Constraint fk_customer foreign key (customerNumber) references customer (customerNumber), Constraint fk_delMethod foreign key (deliveryMethod) references DeliveryMethod (deliveryMethod) ); DROP TABLE IF EXISTS OrderItems; CREATE TABLE OrderItems (
orderNumber int not null, itemCode int NOT NULL, quantity int NOT NULL, Primary Key (orderNumber, itemCode), Constraint fk_order foreign key (orderNumber) references `Order` (orderNumber), Constraint fk_item foreign key (itemCode) references Item (itemCode) ); DROP TABLE IF EXISTS Invoice; CREATE TABLE Invoice ( invoiceNumber int not null, orderNumber int not null, deliveryCharge double NOT NULL, totalAmount double NOT NULL, Primary Key (invoiceNumber), Constraint fk_ord foreign key (orderNumber) references `Order` (orderNumber) ); DROP TABLE IF EXISTS Payment; CREATE TABLE Payment ( paymentID int not null, invoiceNumber int not null, customerNumber int not null,
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
DELIMITER // CREATE FUNCTION total_order_item(qty int, price double, overseas double) RETURNS double DETERMINISTIC BEGIN RETURN ((qty*price)+((price*10)/100)+overseas); END // DELIMITER ; /* Trigger */ DELIMITER $$ CREATE TRIGGER after_order_insert AFTER INSERT ON OrderItems FOR EACH ROW BEGIN DECLARE price double; DECLARE overseasValue double; select Cost into price from Item where itemCode=(New.itemCode); select overseas into overseasValue from Item where itemCode=(New.itemCode); update item set QOH=QOH-New.quantity where orderNumber=New.orderNumber;
update customer set balance=balance-total_order_item(quantity, price, overseasValue); END$$ DELIMITER ; /* Trigger */ DELIMITER $$ CREATE TRIGGER after_payment_insert AFTER INSERT ON payment FOR EACH ROW BEGIN update customer set balance=balance+ New.amount; END$$ DELIMITER ; /* Query 1 */ select itemCode, description, category from Item where itemCode not in (select itemCode from OrderItems);
/* Query 2 */ select `order`.orderNumber, orderItems.itemCode, cost, overseas, quantity, (total_order_item(quantity,cost, overseas) + invoice.deliveryCharge) as TotalAmount from `order` inner join orderItems on `order`.orderNumber=orderItems.orderNumber inner join item on orderItems.itemCode=item.itemCode inner join invoice on invoice.orderNumber=`order`.orderNumber;