ecommerce-website/ddl/SQLServer_orderdb.ddl

251 lines
26 KiB
Plaintext
Raw Normal View History

2024-03-10 20:04:53 +00:00
USE master;
IF NOT EXISTS (SELECT * FROM sys.databases WHERE name = 'orders')
BEGIN
CREATE DATABASE orders
END;
USE orders;
DROP TABLE IF EXISTS review;
DROP TABLE IF EXISTS shipment;
DROP TABLE IF EXISTS productinventory;
DROP TABLE IF EXISTS warehouse;
DROP TABLE IF EXISTS orderproduct;
DROP TABLE IF EXISTS incart;
DROP TABLE IF EXISTS product;
DROP TABLE IF EXISTS category;
DROP TABLE IF EXISTS ordersummary;
DROP TABLE IF EXISTS paymentmethod;
DROP TABLE IF EXISTS customer;
CREATE TABLE customer (
customerId INT IDENTITY,
firstName VARCHAR(40) not null,
lastName VARCHAR(40) not null,
email VARCHAR(50) not null unique,
phonenum VARCHAR(20) not null unique,
address VARCHAR(50) not null,
city VARCHAR(40) not null,
state VARCHAR(20) not null,
postalCode VARCHAR(20) not null,
country VARCHAR(40) not null,
userid VARCHAR(20) not null unique,
password VARCHAR(30) not null,
PRIMARY KEY (customerId)
);
CREATE TABLE paymentmethod (
paymentMethodId INT IDENTITY,
paymentType VARCHAR(20),
paymentNumber VARCHAR(30),
paymentExpiryDate DATE,
customerId INT,
PRIMARY KEY (paymentMethodId),
FOREIGN KEY (customerId) REFERENCES customer(customerid)
ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE TABLE ordersummary (
orderId INT IDENTITY,
orderDate DATETIME,
totalAmount DECIMAL(10,2),
shiptoAddress VARCHAR(50),
shiptoCity VARCHAR(40),
shiptoState VARCHAR(20),
shiptoPostalCode VARCHAR(20),
shiptoCountry VARCHAR(40),
customerId INT,
PRIMARY KEY (orderId),
FOREIGN KEY (customerId) REFERENCES customer(customerid)
ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE TABLE category (
categoryId INT IDENTITY,
categoryName VARCHAR(50),
PRIMARY KEY (categoryId)
);
CREATE TABLE product (
productId INT IDENTITY,
productName VARCHAR(40),
productPrice DECIMAL(10,2),
productImageURL VARCHAR(100),
productImage VARBINARY(MAX),
productDesc VARCHAR(1000),
categoryId INT,
PRIMARY KEY (productId),
FOREIGN KEY (categoryId) REFERENCES category(categoryId)
);
CREATE TABLE orderproduct (
orderId INT,
productId INT,
quantity INT,
price DECIMAL(10,2),
PRIMARY KEY (orderId, productId),
FOREIGN KEY (orderId) REFERENCES ordersummary(orderId)
ON UPDATE CASCADE ON DELETE NO ACTION,
FOREIGN KEY (productId) REFERENCES product(productId)
ON UPDATE CASCADE ON DELETE NO ACTION
);
CREATE TABLE incart (
orderId INT,
productId INT,
quantity INT,
price DECIMAL(10,2),
PRIMARY KEY (orderId, productId),
FOREIGN KEY (orderId) REFERENCES ordersummary(orderId)
ON UPDATE CASCADE ON DELETE NO ACTION,
FOREIGN KEY (productId) REFERENCES product(productId)
ON UPDATE CASCADE ON DELETE NO ACTION
);
CREATE TABLE warehouse (
warehouseId INT IDENTITY,
warehouseName VARCHAR(30),
PRIMARY KEY (warehouseId)
);
CREATE TABLE shipment (
shipmentId INT IDENTITY,
shipmentDate DATETIME,
shipmentDesc VARCHAR(100),
warehouseId INT,
PRIMARY KEY (shipmentId),
FOREIGN KEY (warehouseId) REFERENCES warehouse(warehouseId)
ON UPDATE CASCADE ON DELETE NO ACTION
);
CREATE TABLE productinventory (
productId INT,
warehouseId INT,
quantity INT,
price DECIMAL(10,2),
PRIMARY KEY (productId, warehouseId),
FOREIGN KEY (productId) REFERENCES product(productId)
ON UPDATE CASCADE ON DELETE NO ACTION,
FOREIGN KEY (warehouseId) REFERENCES warehouse(warehouseId)
ON UPDATE CASCADE ON DELETE NO ACTION
);
CREATE TABLE review (
reviewId INT IDENTITY,
reviewRating INT,
reviewDate DATETIME,
customerId INT,
productId INT,
reviewComment VARCHAR(1000),
PRIMARY KEY (reviewId),
FOREIGN KEY (customerId) REFERENCES customer(customerId)
ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY (productId) REFERENCES product(productId)
ON UPDATE CASCADE ON DELETE CASCADE
);
INSERT INTO category(categoryName) VALUES ('Beverages');
INSERT INTO category(categoryName) VALUES ('Condiments');
INSERT INTO category(categoryName) VALUES ('Dairy Products');
INSERT INTO category(categoryName) VALUES ('Produce');
INSERT INTO category(categoryName) VALUES ('Meat/Poultry');
INSERT INTO category(categoryName) VALUES ('Seafood');
INSERT INTO category(categoryName) VALUES ('Confections');
INSERT INTO category(categoryName) VALUES ('Grains/Cereals');
INSERT product(productName, categoryId, productDesc, productPrice) VALUES ('Chai', 1, '10 boxes x 20 bags',18.00);
INSERT product(productName, categoryId, productDesc, productPrice) VALUES ('Chang',1,'24 - 12 oz bottles',19.00);
INSERT product(productName, categoryId, productDesc, productPrice) VALUES ('Aniseed Syrup',2,'12 - 550 ml bottles',10.00);
INSERT product(productName, categoryId, productDesc, productPrice) VALUES ('Chef Anton''s Cajun Seasoning',2,'48 - 6 oz jars',22.00);
INSERT product(productName, categoryId, productDesc, productPrice) VALUES ('Chef Anton''s Gumbo Mix',2,'36 boxes',21.35);
INSERT product(productName, categoryId, productDesc, productPrice) VALUES ('Grandma''s Boysenberry Spread',2,'12 - 8 oz jars',25.00);
INSERT product(productName, categoryId, productDesc, productPrice) VALUES ('Uncle Bob''s Organic Dried Pears',4,'12 - 1 lb pkgs.',30.00);
INSERT product(productName, categoryId, productDesc, productPrice) VALUES ('Northwoods Cranberry Sauce',2,'12 - 12 oz jars',40.00);
INSERT product(productName, categoryId, productDesc, productPrice) VALUES ('Mishi Kobe Niku',5,'18 - 500 g pkgs.',97.00);
INSERT product(productName, categoryId, productDesc, productPrice) VALUES ('Ikura',6,'12 - 200 ml jars',31.00);
INSERT product(productName, categoryId, productDesc, productPrice) VALUES ('Queso Cabrales',3,'1 kg pkg.',21.00);
INSERT product(productName, categoryId, productDesc, productPrice) VALUES ('Queso Manchego La Pastora',3,'10 - 500 g pkgs.',38.00);
INSERT product(productName, categoryId, productDesc, productPrice) VALUES ('Tofu',4,'40 - 100 g pkgs.',23.25);
INSERT product(productName, categoryId, productDesc, productPrice) VALUES ('Genen Shouyu',2,'24 - 250 ml bottles',15.50);
INSERT product(productName, categoryId, productDesc, productPrice) VALUES ('Pavlova',7,'32 - 500 g boxes',17.45);
INSERT product(productName, categoryId, productDesc, productPrice) VALUES ('Alice Mutton',5,'20 - 1 kg tins',39.00);
INSERT product(productName, categoryId, productDesc, productPrice) VALUES ('Carnarvon Tigers',6,'16 kg pkg.',62.50);
INSERT product(productName, categoryId, productDesc, productPrice) VALUES ('Teatime Chocolate Biscuits',7,'10 boxes x 12 pieces',9.20);
INSERT product(productName, categoryId, productDesc, productPrice) VALUES ('Sir Rodney''s Marmalade',7,'30 gift boxes',81.00);
INSERT product(productName, categoryId, productDesc, productPrice) VALUES ('Sir Rodney''s Scones',7,'24 pkgs. x 4 pieces',10.00);
INSERT product(productName, categoryId, productDesc, productPrice) VALUES ('Gustaf''s Knackebread',8,'24 - 500 g pkgs.',21.00);
INSERT product(productName, categoryId, productDesc, productPrice) VALUES ('Sasquatch Ale',1,'24 - 12 oz bottles',14.00);
INSERT product(productName, categoryId, productDesc, productPrice) VALUES ('Steeleye Stout',1,'24 - 12 oz bottles',18.00);
INSERT product(productName, categoryId, productDesc, productPrice) VALUES ('Inlagd Sill',6,'24 - 250 g jars',19.00);
INSERT product(productName, categoryId, productDesc, productPrice) VALUES ('Boston Crab Meat',6,'24 - 4 oz tins',18.40);
INSERT product(productName, categoryId, productDesc, productPrice) VALUES ('Jack''s New England Clam Chowder',6,'12 - 12 oz cans',9.65);
INSERT product(productName, categoryId, productDesc, productPrice) VALUES ('Singaporean Hokkien Fried Mee',8,'32 - 1 kg pkgs.',14.00);
INSERT product(productName, categoryId, productDesc, productPrice) VALUES ('Louisiana Fiery Hot Pepper Sauce',2,'32 - 8 oz bottles',21.05);
INSERT product(productName, categoryId, productDesc, productPrice) VALUES ('Laughing Lumberjack Lager',1,'24 - 12 oz bottles',14.00);
INSERT INTO warehouse(warehouseName) VALUES ('Main warehouse');
INSERT INTO productInventory(productId, warehouseId, quantity, price) VALUES (1, 1, 5, 18);
INSERT INTO productInventory(productId, warehouseId, quantity, price) VALUES (2, 1, 10, 19);
INSERT INTO productInventory(productId, warehouseId, quantity, price) VALUES (3, 1, 3, 10);
INSERT INTO productInventory(productId, warehouseId, quantity, price) VALUES (4, 1, 2, 22);
INSERT INTO productInventory(productId, warehouseId, quantity, price) VALUES (5, 1, 6, 21.35);
INSERT INTO productInventory(productId, warehouseId, quantity, price) VALUES (6, 1, 3, 25);
INSERT INTO productInventory(productId, warehouseId, quantity, price) VALUES (7, 1, 1, 30);
INSERT INTO productInventory(productId, warehouseId, quantity, price) VALUES (8, 1, 0, 40);
INSERT INTO productInventory(productId, warehouseId, quantity, price) VALUES (9, 1, 2, 97);
INSERT INTO productInventory(productId, warehouseId, quantity, price) VALUES (10, 1, 3, 31);
INSERT INTO customer (firstName, lastName, email, phonenum, address, city, state, postalCode, country, userid, password) VALUES ('Arnold', 'Anderson', 'a.anderson@gmail.com', '204-111-2222', '103 AnyWhere Street', 'Winnipeg', 'MB', 'R3X 45T', 'Canada', 'arnold' , 'test');
INSERT INTO customer (firstName, lastName, email, phonenum, address, city, state, postalCode, country, userid, password) VALUES ('Bobby', 'Brown', 'bobby.brown@hotmail.ca', '572-342-8911', '222 Bush Avenue', 'Boston', 'MA', '22222', 'United States', 'bobby' , 'bobby');
INSERT INTO customer (firstName, lastName, email, phonenum, address, city, state, postalCode, country, userid, password) VALUES ('Candace', 'Cole', 'cole@charity.org', '333-444-5555', '333 Central Crescent', 'Chicago', 'IL', '33333', 'United States', 'candace' , 'password');
INSERT INTO customer (firstName, lastName, email, phonenum, address, city, state, postalCode, country, userid, password) VALUES ('Darren', 'Doe', 'oe@doe.com', '250-807-2222', '444 Dover Lane', 'Kelowna', 'BC', 'V1V 2X9', 'Canada', 'darren' , 'pw');
INSERT INTO customer (firstName, lastName, email, phonenum, address, city, state, postalCode, country, userid, password) VALUES ('Elizabeth', 'Elliott', 'engel@uiowa.edu', '555-666-7777', '555 Everwood Street', 'Iowa City', 'IA', '52241', 'United States', 'beth' , 'test');
-- Order 1 can be shipped as have enough inventory
DECLARE @orderId int
INSERT INTO ordersummary (customerId, orderDate, totalAmount) VALUES (1, '2019-10-15 10:25:55', 91.70)
SELECT @orderId = @@IDENTITY
INSERT INTO orderproduct (orderId, productId, quantity, price) VALUES (@orderId, 1, 1, 18)
INSERT INTO orderproduct (orderId, productId, quantity, price) VALUES (@orderId, 5, 2, 21.35)
INSERT INTO orderproduct (orderId, productId, quantity, price) VALUES (@orderId, 10, 1, 31);
DECLARE @orderId int
INSERT INTO ordersummary (customerId, orderDate, totalAmount) VALUES (2, '2019-10-16 18:00:00', 106.75)
SELECT @orderId = @@IDENTITY
INSERT INTO orderproduct (orderId, productId, quantity, price) VALUES (@orderId, 5, 5, 21.35);
-- Order 3 cannot be shipped as do not have enough inventory for item 7
DECLARE @orderId int
INSERT INTO ordersummary (customerId, orderDate, totalAmount) VALUES (3, '2019-10-15 3:30:22', 140)
SELECT @orderId = @@IDENTITY
INSERT INTO orderproduct (orderId, productId, quantity, price) VALUES (@orderId, 6, 2, 25)
INSERT INTO orderproduct (orderId, productId, quantity, price) VALUES (@orderId, 7, 3, 30);
DECLARE @orderId int
INSERT INTO ordersummary (customerId, orderDate, totalAmount) VALUES (2, '2019-10-17 05:45:11', 327.85)
SELECT @orderId = @@IDENTITY
INSERT INTO orderproduct (orderId, productId, quantity, price) VALUES (@orderId, 3, 4, 10)
INSERT INTO orderproduct (orderId, productId, quantity, price) VALUES (@orderId, 8, 3, 40)
INSERT INTO orderproduct (orderId, productId, quantity, price) VALUES (@orderId, 13, 3, 23.25)
INSERT INTO orderproduct (orderId, productId, quantity, price) VALUES (@orderId, 28, 2, 21.05)
INSERT INTO orderproduct (orderId, productId, quantity, price) VALUES (@orderId, 29, 4, 14);
DECLARE @orderId int
INSERT INTO ordersummary (customerId, orderDate, totalAmount) VALUES (5, '2019-10-15 10:25:55', 277.40)
SELECT @orderId = @@IDENTITY
INSERT INTO orderproduct (orderId, productId, quantity, price) VALUES (@orderId, 5, 4, 21.35)
INSERT INTO orderproduct (orderId, productId, quantity, price) VALUES (@orderId, 19, 2, 81)
INSERT INTO orderproduct (orderId, productId, quantity, price) VALUES (@orderId, 20, 3, 10);
-- New SQL DDL for lab 8
UPDATE Product SET productImageURL = 'img/1.jpg' WHERE ProductId = 1;
UPDATE Product SET productImageURL = 'img/2.jpg' WHERE ProductId = 2;
UPDATE Product SET productImageURL = 'img/3.jpg' WHERE ProductId = 3;
UPDATE Product SET productImageURL = 'img/4.jpg' WHERE ProductId = 4;
UPDATE Product SET productImageURL = 'img/5.jpg' WHERE ProductId = 5;
-- Loads image data for product 1
UPDATE Product SET productImage = 0xffd8ffe000104a46494600010100000100010000ffdb00840009060712131215131313151515171817171515171717181718171717161717171517181d2820181a251d151721312125292b2e2e2e171f3338332d37282d2e2b010a0a0a0e0d0e1a10101b2d251f252b2d2d2d2d2f2d2d2b2e2d2d2d2d2d2d2b2b2b2d2d2d2d2d2d2d2d2d2b2d2d2d2d2d2d2d2b2d2d2b2d2d2d2d2b2d2d2d2d2bffc000110800c2010303012200021101031101ffc4001b00000105010100000000000000000000000200010304050607ffc4004010000103020304080403060505010000000100021103210431410512516106132232718191a152b1c1d14272f0236282b2e1f11415163392345373a2d207ffc4001a010002030101000000000000000000000000010203040506ffc4002a1100020201040201020603000000000000000102031104122131134151226105323342819171a1d1ffda000c03010002110311003f00810945099672c1932772125002052053129a54931325053ca865105222c9779284011a004d6a2081a51b5458c508d81353289ed82900aa051c2900285c80037522d44122ef34000022490ca603c249d104c420110285384d08918a7c3e6951a5212a79a9262659c3b2e14cf10e55f0cf833c1155a8499534f822d724f56a12eb9952e02a43c5e1520f44c29e79135c1d1d7da2d00c1d173a209be5aa17bcdd0029ca4d8a3051e8b7fe5d40de5c3900928baf7264657c0625f2623901444262164348084a94040e40029a5102990032209d385210e2522534a62119104d72980909f058275436b3466e390fbad5389650ec531d6b8916b0bc6bc05d5165f1816d74ca7d1530db3ea11bc61adf89e607dd3e25f4186ef351c74a605f8ab389a61c01acf33f0830df08cdc2fe73926c2f533fb32db7080272b939e6b14f5537d1b21a682ef91a9542476686efe63bd7e0414f189c86e82720d601ef0a7a55dc1df860697de20117008cb2cb884e71e2e0068741dd1de3adcb8e56d20acee727db2f508ae910d2a55d99d519010e8e7e49ea75a40100ceb00811c654b5a997192f1efeb68bca82a53b897bbcb7f3fd14b731e106cc2cdcd169033dd76efb44a831583a40cf6d91f10de6f8348893e28e1e1c06fc8cc4878fefe8ac56c73c01bc01191b7cc8b82ad85f647a6572a612f4673f67be039b0f69d5877bd40baac02dba3baf76f37b264f227c084d8ca327f6ad2e11fee37be2fada1d96ab657abf52465b34b8fca63052efa92be17746f021ec360f194f070fc2791f295000b6c5a6b28c6d35c309b54e48c3d46113829a22107a3eb142114a604a5c90aa545296f262c066a14c2a15184903c121ae5251c24802b3820705631748b5c47a1e21562b3968dbc9248094004f429a5384c0269489421130204257f676083e5ce3bac1727531986ad0ff004e5434595ac18e20677bce43c954dab8d635a29333223b3a007e64fdf82cf7dbb385d97d156f7c916d1c639ffb2a32da7066d123f36716f9a88bbab616082e393a67cc9c8468a6ea8b69ef0992274304f982564e198e7104b5c3760913a9e3a0cb52b9f9cf674e292584495a9bded92fb44191322d9a9b054ea1225dba72ee3a47980042d2c1b3b40b298ddce6f1391e4606aa7c63e9b7bed630c82489bce53ce74e1ecb77a0c87b3db6d60483bc1b79e2065171e4ae3767071de1022c4c6995a341926d881952a38b5d2441eaf2de19183c6345d260a8d324be982d70b39a6639b7faf252854e453659b4c3c5d0a74dae710406893264db809850d16b9c3b2dbc4c13bc1be90ba4da5870e02dfba7cf8ac9af83751786b01707093a584ff4f644ea71646366515e9e01ff008a0e440fb82e3af0093b040080d8813970f204e47256aa55d44f0e1e055bc2f6d84c078c8b4663cb8e5c128c53781b935c9ce330fbe09197e133711e5636362ac61aad4162779bc1c3b5c3316239e775b988c0c8902639c387000f0e46cb22ad18710eccf762774deee6ead3c5a4dbc2e9ca0e23535221ad44365d4ee0f79a6208e046a3e5f2c9c5e161bd632773223561e0788e07cb99d9a4371c73beba1ca654551c29b8bb7658eecb9a79e9cc1faab68bdc1fd8aada94d7dcc00e468f69617aa7c032d3da63b8b4fd4411e4ab872eb2e5651cd7c13b427365abd1a661cb9c710e21a0580d4f0b2cec539a5c7727764c4e71a4a622294c9a532603929d327400fe899249033471b87de1cd62d46c2eb37010b3369e07f1058e322f68c17041baa5a8a17156111142e09262800a538728a5269d067f54643075984c6b99856efb8c09735b39366240f195caf586a5473ac0e423d8dec4fd9751d2466e536b1a090d6b596b7e1179d32f75cd60599836bda07b831132335cdb25993674e88e22586517b1901ce2e233d018390c869a6a15de8e619d58c3d90d68398209d01390e197050e2311bb1de25d0019b0b92275d0e7c174fd1d637fc3cb4f7886e64de6f79e0156b92737b515712008008ddc841222384e79045b3594b7e6a0749821f32d90008205c653395d686130fbce27741821ac69c85892e227412a96d3a2ded410d2d7006206f7181e50849c7ea2bca7c17ea5263b72a53a448610eeb058c0cc3466e90b7e83a9bfb6d37e563e63eeb2b67baad36f77ac66620dc7f44558d1799dddd7f983ff00a9f9ad30928acffaff008679a72e0d6ad7b037e06d2a1608733902dcb5b47c8ac815cb4c6e92393c3867ab5c787cd5da18b9ce67313636e13984fca9b23e3690d536782fa8ebc82204d888923ce55aa7866b5c1ec101d9e9122c7d6d1cd1e1de0971d0c7b483f2f643b3c8ea5a45c588e113621