0% found this document useful (0 votes)
2 views9 pages

Scripts

The document outlines the SQL schema for the Northwind database, including tables for categories, suppliers, products, warehouses, orders, and more. It also includes sample data insertion for various tables and a stored procedure to generate large volumes of records for testing purposes. The schema is designed to manage inventory, sales, and customer interactions efficiently.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

Scripts

The document outlines the SQL schema for the Northwind database, including tables for categories, suppliers, products, warehouses, orders, and more. It also includes sample data insertion for various tables and a stored procedure to generate large volumes of records for testing purposes. The schema is designed to manage inventory, sales, and customer interactions efficiently.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

-- Northwind DB --

-- Create the Northwind database


CREATE DATABASE IF NOT EXISTS northwind;
USE northwind;

-- Categories Table: Stores product categories


CREATE TABLE Categories (
CategoryID INT AUTO_INCREMENT PRIMARY KEY,
CategoryName VARCHAR(50) NOT NULL,
Description TEXT
);

-- Suppliers Table: Stores supplier information


CREATE TABLE Suppliers (
SupplierID INT AUTO_INCREMENT PRIMARY KEY,
CompanyName VARCHAR(100) NOT NULL,
ContactName VARCHAR(50),
ContactTitle VARCHAR(50),
Address VARCHAR(100),
City VARCHAR(50),
Region VARCHAR(50),
PostalCode VARCHAR(20),
Country VARCHAR(50),
Phone VARCHAR(20),
Fax VARCHAR(20)
);

-- Products Table: Stores product information


CREATE TABLE Products (
ProductID INT AUTO_INCREMENT PRIMARY KEY,
ProductName VARCHAR(100) NOT NULL,
SupplierID INT,
CategoryID INT,
QuantityPerUnit VARCHAR(50),
UnitPrice DECIMAL(10, 2) DEFAULT 0.00,
UnitsInStock INT DEFAULT 0,
UnitsOnOrder INT DEFAULT 0,
ReorderLevel INT DEFAULT 0,
Discontinued TINYINT(1) DEFAULT 0,
FOREIGN KEY (SupplierID) REFERENCES Suppliers(SupplierID),
FOREIGN KEY (CategoryID) REFERENCES Categories(CategoryID)
);

-- Warehouses Table: Stores warehouse information


CREATE TABLE Warehouses (
WarehouseID INT AUTO_INCREMENT PRIMARY KEY,
WarehouseName VARCHAR(100) NOT NULL,
Address VARCHAR(100),
City VARCHAR(50),
Region VARCHAR(50),
Country VARCHAR(50)
);

-- WarehouseStock Table: Tracks stock levels in each warehouse for each product
CREATE TABLE WarehouseStock (
WarehouseStockID INT AUTO_INCREMENT PRIMARY KEY,
WarehouseID INT,
ProductID INT,
StockLevel INT DEFAULT 0,
LastUpdated DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (WarehouseID) REFERENCES Warehouses(WarehouseID),
FOREIGN KEY (ProductID) REFERENCES Products(ProductID)
);

-- InventoryTransactions Table: Tracks inventory changes (stock additions,


deductions)
CREATE TABLE InventoryTransactions (
TransactionID INT AUTO_INCREMENT PRIMARY KEY,
ProductID INT,
WarehouseID INT,
TransactionType ENUM('IN', 'OUT') NOT NULL, -- IN for stock addition, OUT for
deduction
Quantity INT NOT NULL,
TransactionDate DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (ProductID) REFERENCES Products(ProductID),
FOREIGN KEY (WarehouseID) REFERENCES Warehouses(WarehouseID)
);

-- Promotions Table: Stores promotional campaigns for products


CREATE TABLE Promotions (
PromotionID INT AUTO_INCREMENT PRIMARY KEY,
ProductID INT,
PromotionName VARCHAR(100) NOT NULL,
Discount DECIMAL(5, 2), -- e.g., 10.00 for 10% discount
StartDate DATE NOT NULL,
EndDate DATE NOT NULL,
FOREIGN KEY (ProductID) REFERENCES Products(ProductID)
);

-- Customers Table: Stores customer information


CREATE TABLE Customers (
CustomerID INT AUTO_INCREMENT PRIMARY KEY,
CompanyName VARCHAR(100),
ContactName VARCHAR(50),
ContactTitle VARCHAR(50),
Address VARCHAR(100),
City VARCHAR(50),
Region VARCHAR(50),
PostalCode VARCHAR(20),
Country VARCHAR(50),
Phone VARCHAR(20),
Fax VARCHAR(20),
Email VARCHAR(100)
);

-- Employees Table: Stores employee information


CREATE TABLE Employees (
EmployeeID INT AUTO_INCREMENT PRIMARY KEY,
LastName VARCHAR(50) NOT NULL,
FirstName VARCHAR(50) NOT NULL,
Title VARCHAR(50),
TitleOfCourtesy VARCHAR(10),
BirthDate DATE,
HireDate DATE,
Address VARCHAR(100),
City VARCHAR(50),
Region VARCHAR(50),
PostalCode VARCHAR(20),
Country VARCHAR(50),
HomePhone VARCHAR(20),
Extension VARCHAR(10),
ReportsTo INT,
FOREIGN KEY (ReportsTo) REFERENCES Employees(EmployeeID)
);

-- Regions Table: Stores region information


CREATE TABLE Regions (
RegionID INT AUTO_INCREMENT PRIMARY KEY,
RegionDescription VARCHAR(50) NOT NULL
);

-- Territories Table: Stores territory information for employees


CREATE TABLE Territories (
TerritoryID INT AUTO_INCREMENT PRIMARY KEY,
TerritoryDescription VARCHAR(50) NOT NULL,
RegionID INT,
FOREIGN KEY (RegionID) REFERENCES Regions(RegionID)
);

-- Shippers Table: Stores shipper information


CREATE TABLE Shippers (
ShipperID INT AUTO_INCREMENT PRIMARY KEY,
CompanyName VARCHAR(100) NOT NULL,
Phone VARCHAR(20)
);

-- Orders Table: Stores customer orders


CREATE TABLE Orders (
OrderID INT AUTO_INCREMENT PRIMARY KEY,
CustomerID INT,
EmployeeID INT,
OrderDate DATETIME DEFAULT CURRENT_TIMESTAMP,
RequiredDate DATE,
ShippedDate DATE,
ShipperID INT,
Freight DECIMAL(10, 2) DEFAULT 0.00,
ShipName VARCHAR(100),
ShipAddress VARCHAR(100),
ShipCity VARCHAR(50),
ShipRegion VARCHAR(50),
ShipPostalCode VARCHAR(20),
ShipCountry VARCHAR(50),
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID),
FOREIGN KEY (EmployeeID) REFERENCES Employees(EmployeeID),
FOREIGN KEY (ShipperID) REFERENCES Shippers(ShipperID)
);

-- OrderDetails Table: Stores items in each order


CREATE TABLE OrderDetails (
OrderDetailID INT AUTO_INCREMENT PRIMARY KEY,
OrderID INT,
ProductID INT,
UnitPrice DECIMAL(10, 2) NOT NULL,
Quantity INT NOT NULL,
Discount DECIMAL(5, 2) DEFAULT 0.00,
FOREIGN KEY (OrderID) REFERENCES Orders(OrderID),
FOREIGN KEY (ProductID) REFERENCES Products(ProductID)
);

-- CustomerFeedback Table: Stores feedback from customers about orders


CREATE TABLE CustomerFeedback (
FeedbackID INT AUTO_INCREMENT PRIMARY KEY,
CustomerID INT,
OrderID INT,
Rating INT CHECK (Rating BETWEEN 1 AND 5),
Comment TEXT,
FeedbackDate DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID),
FOREIGN KEY (OrderID) REFERENCES Orders(OrderID)
);

-- Returns Table: Tracks returned orders or items


CREATE TABLE Returns (
ReturnID INT AUTO_INCREMENT PRIMARY KEY,
OrderID INT,
ProductID INT,
Quantity INT NOT NULL,
ReturnDate DATETIME DEFAULT CURRENT_TIMESTAMP,
Reason TEXT,
FOREIGN KEY (OrderID) REFERENCES Orders(OrderID),
FOREIGN KEY (ProductID) REFERENCES Products(ProductID)
);

-- Payments Table: Tracks payments for orders


CREATE TABLE Payments (
PaymentID INT AUTO_INCREMENT PRIMARY KEY,
OrderID INT,
PaymentDate DATETIME DEFAULT CURRENT_TIMESTAMP,
Amount DECIMAL(10, 2) NOT NULL,
PaymentMethod ENUM('Credit Card', 'Debit Card', 'Cash', 'Bank Transfer') NOT
NULL,
FOREIGN KEY (OrderID) REFERENCES Orders(OrderID)
);

-- Add indexes for better performance


CREATE INDEX idx_customer_email ON Customers(Email);
CREATE INDEX idx_order_customer ON Orders(CustomerID);
CREATE INDEX idx_order_employee ON Orders(EmployeeID);
CREATE INDEX idx_order_detail_order ON OrderDetails(OrderID);
CREATE INDEX idx_inventory_product ON InventoryTransactions(ProductID);
CREATE INDEX idx_feedback_order ON CustomerFeedback(OrderID);
CREATE INDEX idx_payment_order ON Payments(OrderID);

USE northwind;

-- Insert sample data into Categories


INSERT INTO Categories (CategoryName, Description) VALUES
('Beverages', 'Soft drinks, coffees, teas'),
('Confections', 'Desserts, candies, sweet breads'),
('Dairy Products', 'Cheeses'),
('Grains/Cereals', 'Breads, crackers, pasta'),
('Meat/Poultry', 'Prepared meats'),
('Produce', 'Dried fruit and bean curd'),
('Seafood', 'Seaweed and fish');
-- Insert sample data into Suppliers
INSERT INTO Suppliers (CompanyName, ContactName, ContactTitle, Address, City,
Region, PostalCode, Country, Phone) VALUES
('Exotic Liquids', 'Charlotte Cooper', 'Purchasing Manager', '49 Gilbert St.',
'London', NULL, 'EC1 4SD', 'UK', '(171) 555-2222'),
('New Orleans Cajun Delights', 'Shelley Burke', 'Order Administrator', 'P.O. Box
78934', 'New Orleans', 'LA', '70117', 'USA', '(100) 555-4822'),
('Tokyo Traders', 'Yoshi Nagase', 'Marketing Manager', '9-8 Sekimai Musashino-shi',
'Tokyo', NULL, '100', 'Japan', '(03) 3555-5011');

-- Insert sample data into Products


INSERT INTO Products (ProductName, SupplierID, CategoryID, QuantityPerUnit,
UnitPrice, UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued) VALUES
('Chai', 1, 1, '10 boxes x 20 bags', 18.00, 39, 0, 10, 0),
('Chang', 1, 1, '24 - 12 oz bottles', 19.00, 17, 40, 25, 0),
('Aniseed Syrup', 1, 2, '12 - 550 ml bottles', 10.00, 13, 70, 25, 0),
('Chef Anton''s Cajun Seasoning', 2, 2, '48 - 6 oz jars', 22.00, 53, 0, 0, 0),
('Grandma''s Boysenberry Spread', 3, 2, '12 - 8 oz jars', 25.00, 120, 0, 25, 0),
('Northwoods Cranberry Sauce', 3, 2, '12 - 12 oz jars', 40.00, 6, 0, 0, 0),
('Mishi Kobe Niku', 3, 5, '18 - 500 g pkgs.', 97.00, 29, 0, 0, 1);

-- Insert sample data into Warehouses


INSERT INTO Warehouses (WarehouseName, Address, City, Region, Country) VALUES
('Main Warehouse', '123 Main St', 'Seattle', 'WA', 'USA'),
('East Warehouse', '456 East St', 'Boston', 'MA', 'USA'),
('West Warehouse', '789 West St', 'Los Angeles', 'CA', 'USA');

-- Insert sample data into WarehouseStock


INSERT INTO WarehouseStock (WarehouseID, ProductID, StockLevel) VALUES
(1, 1, 1000),
(1, 2, 800),
(2, 3, 500),
(3, 4, 600);

-- Insert sample data into Promotions


INSERT INTO Promotions (ProductID, PromotionName, Discount, StartDate, EndDate)
VALUES
(1, 'Summer Sale', 10.00, '2025-06-01', '2025-06-30'),
(2, 'Winter Discount', 15.00, '2025-12-01', '2025-12-31');

-- Insert sample data into Regions


INSERT INTO Regions (RegionDescription) VALUES
('Northern'),
('Southern'),
('Eastern'),
('Western');

-- Insert sample data into Territories


INSERT INTO Territories (TerritoryDescription, RegionID) VALUES
('Seattle', 1),
('Portland', 1),
('Boston', 3),
('Miami', 2);

-- Insert sample data into Employees


INSERT INTO Employees (LastName, FirstName, Title, TitleOfCourtesy, BirthDate,
HireDate, Address, City, Region, PostalCode, Country, HomePhone, ReportsTo) VALUES
('Davolio', 'Nancy', 'Sales Representative', 'Ms.', '1968-12-08', '1992-05-01',
'507 - 20th Ave. E.', 'Seattle', 'WA', '98122', 'USA', '(206) 555-9857', NULL),
('Fuller', 'Andrew', 'Vice President, Sales', 'Dr.', '1952-02-19', '1992-08-14',
'908 W. Capital Way', 'Tacoma', 'WA', '98401', 'USA', '(206) 555-9482', NULL),
('Leverling', 'Janet', 'Sales Representative', 'Ms.', '1963-08-30', '1992-04-01',
'722 Moss Bay Blvd.', 'Kirkland', 'WA', '98033', 'USA', '(206) 555-3412', 2);

-- Insert sample data into Shippers


INSERT INTO Shippers (CompanyName, Phone) VALUES
('Speedy Express', '(503) 555-9831'),
('United Package', '(503) 555-3199'),
('Federal Shipping', '(503) 555-9931');

-- Stored procedure to generate millions of records


DELIMITER //

CREATE PROCEDURE GenerateNorthwindData()


BEGIN
DECLARE i INT DEFAULT 1;
DECLARE customerCount INT DEFAULT 1000000; -- 1 million customers
DECLARE orderCount INT DEFAULT 5000000; -- 5 million orders
DECLARE orderDetailCount INT DEFAULT 15000000; -- 15 million order details
DECLARE feedbackCount INT DEFAULT 2000000; -- 2 million feedback records
DECLARE paymentCount INT DEFAULT 5000000; -- 5 million payments
DECLARE returnCount INT DEFAULT 500000; -- 500,000 returns
DECLARE inventoryCount INT DEFAULT 3000000; -- 3 million inventory transactions

-- Generate Customers (1 million)


WHILE i <= customerCount DO
INSERT INTO Customers (CompanyName, ContactName, ContactTitle, Address,
City, Region, PostalCode, Country, Phone, Email)
VALUES (
CONCAT('Company ', i),
CONCAT('Contact ', i),
'Manager',
CONCAT(i, ' Main St'),
'City',
'Region',
CONCAT('ZIP', i),
'Country',
CONCAT('(123) 555-', LPAD(i % 10000, 4, '0')),
CONCAT('contact', i, '@company.com')
);
SET i = i + 1;
END WHILE;

-- Reset counter
SET i = 1;

-- Generate Orders (5 million)


WHILE i <= orderCount DO
INSERT INTO Orders (CustomerID, EmployeeID, OrderDate, RequiredDate,
ShippedDate, ShipperID, Freight, ShipName, ShipAddress, ShipCity, ShipRegion,
ShipPostalCode, ShipCountry)
VALUES (
(i % customerCount) + 1, -- Random customer
(i % 3) + 1, -- Random employee (1-3)
DATE_SUB('2025-06-06', INTERVAL (i % 365) DAY), -- Random date within
the last year
DATE_SUB('2025-06-06', INTERVAL (i % 365 - 7) DAY),
DATE_SUB('2025-06-06', INTERVAL (i % 365 - 3) DAY),
(i % 3) + 1, -- Random shipper (1-3)
(i % 100) + 10.00, -- Freight between 10 and 110
CONCAT('Ship Name ', i),
CONCAT(i, ' Ship St'),
'Ship City',
'Ship Region',
CONCAT('SHIPZIP', i),
'Ship Country'
);
SET i = i + 1;
END WHILE;

-- Reset counter
SET i = 1;

-- Generate Order Details (15 million)


WHILE i <= orderDetailCount DO
INSERT INTO OrderDetails (OrderID, ProductID, UnitPrice, Quantity,
Discount)
VALUES (
(i % orderCount) + 1, -- Random order
(i % 7) + 1, -- Random product (1-7)
10.00 + (i % 90), -- Unit price between 10 and 100
(i % 10) + 1, -- Quantity between 1 and 10
(i % 20) / 100.00 -- Discount between 0% and 20%
);
SET i = i + 1;
END WHILE;

-- Reset counter
SET i = 1;

-- Generate Customer Feedback (2 million)


WHILE i <= feedbackCount DO
INSERT INTO CustomerFeedback (CustomerID, OrderID, Rating, Comment,
FeedbackDate)
VALUES (
(i % customerCount) + 1, -- Random customer
(i % orderCount) + 1, -- Random order
(i % 5) + 1, -- Rating between 1 and 5
CONCAT('Feedback comment ', i),
DATE_SUB('2025-06-06', INTERVAL (i % 365) DAY)
);
SET i = i + 1;
END WHILE;

-- Reset counter
SET i = 1;

-- Generate Payments (5 million)


WHILE i <= paymentCount DO
INSERT INTO Payments (OrderID, PaymentDate, Amount, PaymentMethod)
VALUES (
(i % orderCount) + 1, -- Random order
DATE_SUB('2025-06-06', INTERVAL (i % 365) DAY),
50.00 + (i % 950), -- Amount between 50 and 1000
CASE (i % 4)
WHEN 0 THEN 'Credit Card'
WHEN 1 THEN 'Debit Card'
WHEN 2 THEN 'Cash'
WHEN 3 THEN 'Bank Transfer'
END
);
SET i = i + 1;
END WHILE;

-- Reset counter
SET i = 1;

-- Generate Returns (500,000)


WHILE i <= returnCount DO
INSERT INTO Returns (OrderID, ProductID, Quantity, ReturnDate, Reason)
VALUES (
(i % orderCount) + 1, -- Random order
(i % 7) + 1, -- Random product (1-7)
(i % 5) + 1, -- Quantity between 1 and 5
DATE_SUB('2025-06-06', INTERVAL (i % 365) DAY),
CONCAT('Return reason ', i)
);
SET i = i + 1;
END WHILE;

-- Reset counter
SET i = 1;

-- Generate Inventory Transactions (3 million)


WHILE i <= inventoryCount DO
INSERT INTO InventoryTransactions (ProductID, WarehouseID, TransactionType,
Quantity, TransactionDate)
VALUES (
(i % 7) + 1, -- Random product (1-7)
(i % 3) + 1, -- Random warehouse (1-3)
CASE (i % 2)
WHEN 0 THEN 'IN'
WHEN 1 THEN 'OUT'
END,
(i % 100) + 1, -- Quantity between 1 and 100
DATE_SUB('2025-06-06', INTERVAL (i % 365) DAY)
);
SET i = i + 1;
END WHILE;
END //

DELIMITER ;

-- Call the procedure to generate data


CALL GenerateNorthwindData();

SELECT 'Categories' AS TableName, COUNT(*) AS ExactRowCount FROM Categories


UNION
SELECT 'Suppliers', COUNT(*) FROM Suppliers
UNION
SELECT 'Products', COUNT(*) FROM Products
UNION
SELECT 'Warehouses', COUNT(*) FROM Warehouses
UNION
SELECT 'WarehouseStock', COUNT(*) FROM WarehouseStock
UNION
SELECT 'InventoryTransactions', COUNT(*) FROM InventoryTransactions
UNION
SELECT 'Promotions', COUNT(*) FROM Promotions
UNION
SELECT 'Customers', COUNT(*) FROM Customers
UNION
SELECT 'Employees', COUNT(*) FROM Employees
UNION
SELECT 'Regions', COUNT(*) FROM Regions
UNION
SELECT 'Territories', COUNT(*) FROM Territories
UNION
SELECT 'Shippers', COUNT(*) FROM Shippers
UNION
SELECT 'Orders', COUNT(*) FROM Orders
UNION
SELECT 'OrderDetails', COUNT(*) FROM OrderDetails
UNION
SELECT 'CustomerFeedback', COUNT(*) FROM CustomerFeedback
UNION
SELECT 'Returns', COUNT(*) FROM Returns
UNION
SELECT 'Payments', COUNT(*) FROM Payments;

[
{ "tableName": "Categories", "count": 7 },
{ "tableName": "Suppliers", "count": 3 },
{ "tableName": "Products", "count": 7 },
{ "tableName": "Warehouses", "count": 3 },
{ "tableName": "WarehouseStock", "count": 4 },
{ "tableName": "InventoryTransactions", "count": 3000000 },
{ "tableName": "Promotions", "count": 2 },
{ "tableName": "Customers", "count": 1000000 },
{ "tableName": "Employees", "count": 3 },
{ "tableName": "Regions", "count": 4 },
{ "tableName": "Territories", "count": 4 },
{ "tableName": "Shippers", "count": 3 },
{ "tableName": "Orders", "count": 5000000 },
{ "tableName": "OrderDetails", "count": 15000000 },
{ "tableName": "CustomerFeedback", "count": 2000000 },
{ "tableName": "Returns", "count": 500000 },
{ "tableName": "Payments", "count": 5000000 }
],
"totalRecords": 31500030

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy