Organic Gardens SQL Database Schema By Christopher Kaczor
1. SQL Database Schema and stored procedures created by
Christopher Kaczor
This code is only for use with Microsoft SQL Server
/* When you open Microsoft SQL Server right click Databases in the object explorer then
click “New Database”, name the database Organic_Gardens. Create a new query and make sure
you choose Organic Gardens from the available databases list or type USE Organic_Gardens
and under that GO, run the query. The schema and later stored procedures are divided into
parts which need to be installed in the proper numerical order to work. Copy and paste
the code into Microsoft SQL server following the numbers from lowest to greatest. Each
part is divided by the -------- divider. */
-------------------------------------------------------------------------------------PART
1----------------------------------------------------------------------------------------
----------
---Copy and past this in first up to the bottom line.
CREATE TABLE Vendors (
item_UPC INT NOT NULL
PRIMARY KEY,
vend_inv_stock INT NOT NULL,
vendor_name VARCHAR(50) NOT NULL,
vend_country VARCHAR(15) NOT NULL,
vend_state CHAR(2) NOT NULL,
vend_city VARCHAR(20) NOT NULL,
vend_zip VARCHAR(15) NOT NULL,
vend_address VARCHAR(20) NOT NULL,
vend_phone VARCHAR(20) NOT NULL,
date_ordered DATE NULL,
on_order INT NULL,
);
CREATE TABLE Items (
item_UPC INT NOT NULL REFERENCES
Vendors (item_UPC),
item_name VARCHAR(50) NULL,
item_price MONEY NOT NULL,
aisle INT NOT NULL,
store_on_hand INT NOT NULL,
);
CREATE TABLE OnlineOrders (
item_UPC INT NOT NULL REFERENCES
Vendors (item_UPC),
online_price MONEY NOT NULL,
online_custid VARCHAR(50) NOT NULL PRIMARY KEY,
);
CREATE TABLE OnlineCustomers (
online_custid VARCHAR(50) NOT NULL REFERENCES
OnlineOrders(online_custid),
cust_name VARCHAR(50) NOT NULL,
cust_address VARCHAR(50) NOT NULL,
cust_city VARCHAR(20) NOT NULL,
2. cust_country VARCHAR(20) NOT NULL,
cust_state CHAR(2) NOT NULL,
cust_zip VARCHAR(15) NOT NULL,
cust_email VARCHAR(50) NULL,
cust_phone VARCHAR(20) NULL,
order_date DATE NOT NULL,
);
-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
----------
--------------------------------------------------------------------------------------
PART 2-----------------------------------------------------------------------------------
-----------------------------------------------------------------------
--Copy and past this in second up to the bottom line.
CREATE PROC AddVendor (@itemUPC INT, @vendstock INT, @vendname VARCHAR(50), @vendcountry
VARCHAR(15), @vendstate CHAR(2), @vendcity VARCHAR(20), @vendzip VARCHAR(15), @vendphone
VARCHAR(20), @vend_address VARCHAR(20), @dateord DATE, @onorder INT)
AS
BEGIN
INSERT INTO Vendors (item_UPC, vend_inv_stock, vendor_name, vend_country, vend_state,
vend_city, vend_zip, vend_phone, vend_address, date_ordered, on_order)
VALUES (@itemUPC, @vendstock, @vendname, @vendcountry, @vendstate, @vendcity, @vendzip,
@vendphone, @vend_address, @dateord, @onorder);
END
-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------
----------------------------------------------------------------------------------PART 3-
-----------------------------------------------------------------------------------------
------------------------------------------------------------------------
------Copy and past this in third up to the bottom line.
EXEC AddVendor 1000000001, 6597, 'Organic_Produce', 'USA', 'PA', 'West Chester', '19057',
'256-8754-2594', '456 Apple St', '05-05-2015', 10528
EXEC AddVendor 1000000002, 5811, 'Organic_Produce', 'USA', 'PA', 'West Chester', '19057',
'256-8754-2594', '456 Apple St', '05-05-2015', 9562
EXEC AddVendor 1000000003, 8745, 'Organic_Produce', 'USA', 'PA', 'West Chester', '19057',
'256-8754-2594', '456 Apple St', '05-05-2015', 108
EXEC AddVendor 1000000004, 8521, 'Organic_Produce', 'USA', 'PA', 'West Chester', '19057',
'256-8754-2594', '456 Apple St', '05-05-2015', 1851
EXEC AddVendor 1000000005, 9657, 'Organic_Produce', 'USA', 'PA', 'West Chester', '19057',
'256-8754-2594', '456 Apple St', '05-05-2015', 854
EXEC AddVendor 1000000006, 2562, 'Organic_Produce', 'USA', 'PA', 'West Chester', '19057',
'256-8754-2594', '456 Apple St', '05-05-2015', 10528
EXEC AddVendor 1000000007, 4548, 'Organic_Produce', 'USA', 'PA', 'West Chester', '19057',
'256-8754-2594', '456 Apple St', '05-05-2015', 5684
EXEC AddVendor 1000000008, 2857, 'Organic_Produce', 'USA', 'PA', 'West Chester', '19057',
'256-8754-2594', '456 Apple St', '05-05-2015', 195
EXEC AddVendor 1000000009, 8752, 'Organic_Produce', 'USA', 'PA', 'West Chester', '19057',
'256-8754-2594', '456 Apple St', '05-05-2015', 528
EXEC AddVendor 1000000010, 10587, 'Organic_Produce', 'USA', 'PA', 'West Chester',
'19057', '256-8754-2594', '456 Apple St', '05-05-2015', 8754
EXEC AddVendor 1000000011, 96525, 'Viandes Bios', 'Canada', 'QC', 'St Anicet', 'H1A 5M2',
'785-8452-9672', '687 rue agricole', '06-05-2015', 100
4. EXEC AddItems 1000000016, 'Cereal', '3.00', 4, 30;
EXEC AddItems 1000000017, 'Dried Oats', '2.50', 4, 70;
EXEC AddItems 1000000018, 'Nuts| 2 lb', '1.36', 4, 1568;
EXEC AddItems 1000000019, 'Cashews| 2 lb', '6.99', 4, 8524;
-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
---------------
-----------------------------------------------------------------------------------------
--Part 6---------------------------------------------------------------------------------
--------------
---------Copy and past this in sixth up to the bottom line.------------------------------
-----------------------------------------------------------------------------------------
----------------
CREATE PROC AddOnlineOrders (@itemUPC INT, @onlprice MONEY, @onlcustid VARCHAR(50))
AS
BEGIN
INSERT INTO OnlineOrders (item_UPC, online_price, online_custid)
VALUES (@itemUPC, @onlprice, @onlcustid)
END
-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
----------------------------
--------------------------------------------------------------------------------Part 7---
-----------------------------------------------------------------------------------------
---------------------------
----Copy and past this in seventh up to the bottom line.---------------------------------
-----------------------------------------------------------------------------------------
-------------
EXEC AddOnlineOrders 1000000011, 29.99, 'steak_lover';
EXEC AddOnlineOrders 1000000002, 0.75, 'j_aime_des_oranges';
EXEC AddOnlineOrders 1000000009, 2.00, 'picky_potato';
-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
-------------------
---------------------------------------------------------------------------------------
Part 8-----------------------------------------------------------------------------------
-------------------------------
----Copy and past this in eighth up to the bottom line.----------------------------------
-----------------------------------------------------------------------------------------
------------
CREATE PROC AddOnlineCust (@onlcustid VARCHAR(50), @custname VARCHAR(50), @custaddress
VARCHAR(50), @custcity VARCHAR(20), @custcountry VARCHAR(20), @custstate CHAR(2),
@custzip VARCHAR(15), @custemail VARCHAR(50), @custphone VARCHAR(20), @orderdate DATE)
AS
BEGIN
INSERT INTO OnlineCustomers (online_custid, cust_name, cust_address, cust_city,
cust_country, cust_state, cust_zip, cust_email, cust_phone, order_date)
VALUES (@onlcustid, @custname, @custaddress, @custcity, @custcountry, @custstate,
@custzip, @custemail, @custphone, @orderdate)
END
-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
-------------------------------------
5. --------------------------------------------------------------------------------------
Part 9-----------------------------------------------------------------------------------
-----------------------------------
----Copy and past this in ninth up to the bottom line.-----------------------------------
-----------------------------------------------------------------------------------------
---------------
EXEC AddOnlineCust 'steak_lover', 'Christopher Steakous', '156 Steak Rd', 'Philadelphia',
'USA', 'PA', '19065', 'loverofsteak@steak.com', '215-584-9638', '01-01-2016'
EXEC AddOnlineCust 'j_aime_des_oranges', 'Philipe Orange', '352 rue Beauvoir', 'Quebec',
'Canada', 'QC', 'H2A 6D3 ', 'philipe@rodgers.com', '658-521-9632', '01-01-2016'
EXEC AddOnlineCust 'picky_potato', 'Bob Prank', '169 Prank Rd', 'New York City', 'USA',
'NY', '56821', 'bob@bob.com', '785-565-9274', '01-01-2016'
-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
--------------------
-------------------------------------------------------------------------------MISC
Stored Procedures for analysis-----------------------------------------------------------
-----------
--This stored procedures allows you to count how many orders a customer has placed in the
system
CREATE PROC NumOrderPerCust (@custname VARCHAR(50))
AS
BEGIN
SELECT OC.cust_name, COUNT(OO.online_custid) AS num_of_orders
FROM OnlineCustomers AS OC FULL OUTER JOIN OnlineOrders AS OO
ON OC.online_custid = OO.online_custid
WHERE OC.cust_name LIKE '%' + @custname + '%'
GROUP BY OC.cust_name
END
--This stored procedure allows you to see how many overall orders are in the system
CREATE PROC OrdersInSystem
AS
SELECT OC.cust_name, COUNT(OO.online_custid) AS num_of_orders
FROM OnlineCustomers AS OC FULL OUTER JOIN OnlineOrders AS OO
ON OC.online_custid = OO.online_custid
GROUP BY OC.cust_name