An Inventory Management System (IMS) is an essential part of any business dealing with stock, products, and supply chains. It helps businesses track product quantities, manage reordering, monitor suppliers, and generate insightful reports. MySQL is a perfect backend database choice for implementing a robust and scalable inventory system.
This guide presents a complete walkthrough of designing and implementing an Inventory Management System using MySQL. We will cover the database schema design, table creation, normalization, indexing, stored procedures, views, triggers, and reporting functionalities.
The following are the main entities in our Inventory Management System:
CREATE TABLE categories (
category_id INT AUTO_INCREMENT PRIMARY KEY,
category_name VARCHAR(100) UNIQUE NOT NULL,
description TEXT
);
CREATE TABLE suppliers (
supplier_id INT AUTO_INCREMENT PRIMARY KEY,
supplier_name VARCHAR(100) NOT NULL,
contact_name VARCHAR(100),
phone VARCHAR(20),
email VARCHAR(100),
address TEXT
);
CREATE TABLE products (
product_id INT AUTO_INCREMENT PRIMARY KEY,
product_name VARCHAR(100) NOT NULL,
sku VARCHAR(50) UNIQUE,
category_id INT,
supplier_id INT,
purchase_price DECIMAL(10, 2),
sale_price DECIMAL(10, 2),
min_stock INT DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (category_id) REFERENCES categories(category_id),
FOREIGN KEY (supplier_id) REFERENCES suppliers(supplier_id)
);
CREATE TABLE inventory (
inventory_id INT AUTO_INCREMENT PRIMARY KEY,
product_id INT,
quantity INT DEFAULT 0,
last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
FOREIGN KEY (product_id) REFERENCES products(product_id)
);
CREATE TABLE purchases (
purchase_id INT AUTO_INCREMENT PRIMARY KEY,
product_id INT,
quantity INT,
purchase_price DECIMAL(10,2),
purchase_date DATE,
supplier_id INT,
FOREIGN KEY (product_id) REFERENCES products(product_id),
FOREIGN KEY (supplier_id) REFERENCES suppliers(supplier_id)
);
CREATE TABLE sales (
sale_id INT AUTO_INCREMENT PRIMARY KEY,
product_id INT,
quantity INT,
sale_price DECIMAL(10,2),
sale_date DATE,
FOREIGN KEY (product_id) REFERENCES products(product_id)
);
CREATE TABLE stock_movements (
movement_id INT AUTO_INCREMENT PRIMARY KEY,
product_id INT,
movement_type ENUM('IN', 'OUT'),
quantity INT,
movement_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
description TEXT,
FOREIGN KEY (product_id) REFERENCES products(product_id)
);
INSERT INTO categories (category_name, description)
VALUES ('Electronics', 'Electronic gadgets and appliances'),
('Office Supplies', 'General office materials');
INSERT INTO suppliers (supplier_name, contact_name, phone, email, address)
VALUES ('TechCorp', 'Alice Johnson', '555-1234', 'alice@techcorp.com', '123 Tech Street');
INSERT INTO products (product_name, sku, category_id, supplier_id, purchase_price, sale_price, min_stock)
VALUES ('Laptop', 'LPT123', 1, 1, 500.00, 700.00, 5);
INSERT INTO purchases (product_id, quantity, purchase_price, purchase_date, supplier_id)
VALUES (1, 20, 500.00, '2025-07-01', 1);
UPDATE inventory
SET quantity = quantity + 20
WHERE product_id = 1;
INSERT INTO stock_movements (product_id, movement_type, quantity, description)
VALUES (1, 'IN', 20, 'Purchase Order #1001');
INSERT INTO sales (product_id, quantity, sale_price, sale_date)
VALUES (1, 2, 700.00, '2025-07-05');
UPDATE inventory
SET quantity = quantity - 2
WHERE product_id = 1;
INSERT INTO stock_movements (product_id, movement_type, quantity, description)
VALUES (1, 'OUT', 2, 'Sale Invoice #5001');
CREATE VIEW current_stock AS
SELECT p.product_name, i.quantity, p.min_stock
FROM products p
JOIN inventory i ON p.product_id = i.product_id;
SELECT * FROM current_stock
WHERE quantity < min_stock;
CREATE TRIGGER trg_after_purchase
AFTER INSERT ON purchases
FOR EACH ROW
BEGIN
UPDATE inventory SET quantity = quantity + NEW.quantity
WHERE product_id = NEW.product_id;
INSERT INTO stock_movements (product_id, movement_type, quantity, description)
VALUES (NEW.product_id, 'IN', NEW.quantity, CONCAT('Purchase ID: ', NEW.purchase_id));
END;
CREATE TRIGGER trg_after_sale
AFTER INSERT ON sales
FOR EACH ROW
BEGIN
UPDATE inventory SET quantity = quantity - NEW.quantity
WHERE product_id = NEW.product_id;
INSERT INTO stock_movements (product_id, movement_type, quantity, description)
VALUES (NEW.product_id, 'OUT', NEW.quantity, CONCAT('Sale ID: ', NEW.sale_id));
END;
DELIMITER //
CREATE PROCEDURE AddProduct(
IN pname VARCHAR(100),
IN sku_code VARCHAR(50),
IN cat_id INT,
IN supp_id INT,
IN pp DECIMAL(10,2),
IN sp DECIMAL(10,2),
IN minqty INT
)
BEGIN
INSERT INTO products(product_name, sku, category_id, supplier_id, purchase_price, sale_price, min_stock)
VALUES (pname, sku_code, cat_id, supp_id, pp, sp, minqty);
DECLARE pid INT;
SET pid = LAST_INSERT_ID();
INSERT INTO inventory(product_id, quantity) VALUES (pid, 0);
END //
DELIMITER ;
mysqldump -u root -p inventory_db > inventory_backup.sql
mysql -u root -p inventory_db < inventory_backup.sql
CREATE USER 'inventory_user'@'localhost' IDENTIFIED BY 'strongpass';
GRANT SELECT, INSERT, UPDATE, DELETE ON inventory_db.* TO 'inventory_user'@'localhost';
SELECT SUM(quantity * sale_price) AS total_sales
FROM sales
WHERE sale_date BETWEEN '2025-07-01' AND '2025-07-31';
SELECT SUM(quantity * purchase_price) AS total_purchases
FROM purchases
WHERE purchase_date BETWEEN '2025-07-01' AND '2025-07-31';
SELECT product_id,
SUM(CASE WHEN movement_type = 'IN' THEN quantity ELSE 0 END) AS total_in,
SUM(CASE WHEN movement_type = 'OUT' THEN quantity ELSE 0 END) AS total_out
FROM stock_movements
GROUP BY product_id;The Inventory Management System using MySQL provides a powerful and flexible solution to control stock, monitor sales and purchases, track suppliers, and maintain product integrity. With proper indexing, normalization, and automation through triggers and procedures, the system remains scalable even with large datasets. This solution can be easily extended to interface with web and mobile applications or integrated into ERP solutions.
Use the command: CREATE INDEX index_name ON table_name (column_name); to create an index on a MySQL table.
To install MySQL on Windows, download the installer from the official MySQL website, run the setup, and follow the installation wizard to configure the server and set up user accounts.
MySQL is an open-source relational database management system (RDBMS) that uses SQL (Structured Query Language) for managing and manipulating databases. It is widely used in web applications for its speed and reliability.
Use the command: INSERT INTO table_name (column1, column2) VALUES (value1, value2); to add records to a MySQL table.
Use the command: mysql -u username -p database_name < data.sql; to import data from a SQL file into a MySQL database.
DELETE removes records based on a condition and can be rolled back, while TRUNCATE removes all records from a table and cannot be rolled back.
A trigger is a set of SQL statements that automatically execute in response to certain events on a MySQL table, such as INSERT, UPDATE, or DELETE.
The default MySQL port is 3306, and the root password is set during installation. If not set, you may need to configure it manually.
Replication in MySQL allows data from one MySQL server (master) to be copied to one or more servers (slaves), providing data redundancy and load balancing.
A primary key is a unique identifier for a record in a MySQL table, ensuring that no two records have the same key value.
Use the command: SELECT column1, column2 FROM table_name; to fetch data from a MySQL table.
Use the command: CREATE DATABASE database_name; to create a new MySQL database.
Use the command: CREATE PROCEDURE procedure_name() BEGIN SQL_statements; END; to define a stored procedure in MySQL.
Indexing in MySQL improves query performance by allowing the database to find rows more quickly. Common index types include PRIMARY KEY, UNIQUE, and FULLTEXT.
Use the command: UPDATE table_name SET column1 = value1 WHERE condition; to modify existing records in a MySQL table.
CHAR is a fixed-length string data type, while VARCHAR is variable-length. CHAR is faster for fixed-size data, whereas VARCHAR saves space for variable-length data.
MyISAM is a storage engine that offers fast read operations but lacks support for transactions, while InnoDB supports transactions and foreign keys, providing better data integrity.
A stored procedure is a set of SQL statements that can be stored and executed on the MySQL server, allowing for modular programming and code reuse.
Use the command: mysqldump -u username -p database_name > backup.sql; to create a backup of a MySQL database.
Use the command: DELETE FROM table_name WHERE condition; to remove records from a MySQL table.
A foreign key is a column or set of columns in one MySQL table that references the primary key in another, establishing a relationship between the two tables.
Use the command: CREATE TRIGGER trigger_name BEFORE INSERT ON table_name FOR EACH ROW BEGIN SQL_statements; END; to create a trigger in MySQL.
Normalization in MySQL is the process of organizing data to reduce redundancy and improve data integrity by dividing large tables into smaller ones.
JOIN is used to combine rows from two or more MySQL tables based on a related column, allowing for complex queries and data retrieval.
Use the command: mysqldump -u username -p database_name > backup.sql; to export a MySQL database to a SQL file.
Copyrights © 2024 letsupdateskills All rights reserved