How to design a MySQL table structure to manage warehouse inventory
With the development of the logistics industry, warehouse inventory management has become more and more important. In the warehouse, accurately recording and managing inventory can help businesses improve operational efficiency and customer satisfaction. As a widely used relational database management system, MySQL can help us manage warehouse inventory effectively. This article will explore how to design a MySQL table structure to manage warehouse inventory and provide specific code examples.
CREATE TABLE Warehouse (
id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255) NOT NULL, address VARCHAR(255) NOT NULL, contact_number VARCHAR(20), created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
CREATE TABLE Product (
id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255) NOT NULL, category VARCHAR(255) NOT NULL, price DECIMAL(10, 2) NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
CREATE TABLE Inventory (
id INT PRIMARY KEY AUTO_INCREMENT, warehouse_id INT NOT NULL, product_id INT NOT NULL, quantity INT NOT NULL, FOREIGN KEY (warehouse_id) REFERENCES Warehouse(id), FOREIGN KEY (product_id) REFERENCES Product(id), created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
CREATE TABLE Inbound (
id INT PRIMARY KEY AUTO_INCREMENT, warehouse_id INT NOT NULL, product_id INT NOT NULL, quantity INT NOT NULL, inbound_at TIMESTAMP NOT NULL, FOREIGN KEY (warehouse_id) REFERENCES Warehouse(id), FOREIGN KEY (product_id) REFERENCES Product(id), created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
CREATE TABLE Outbound (
id INT PRIMARY KEY AUTO_INCREMENT, warehouse_id INT NOT NULL, product_id INT NOT NULL, quantity INT NOT NULL, outbound_at TIMESTAMP NOT NULL, FOREIGN KEY (warehouse_id) REFERENCES Warehouse(id), FOREIGN KEY (product_id) REFERENCES Product(id), created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
Through the design of the above five tables, we can achieve effective management of warehouse inventory. The warehouse table stores the basic information of the warehouse, the product table stores the basic information of the goods, the inventory table records the inventory quantity of different goods in the warehouse, and the warehousing table and the outbound table record the warehousing and outgoing information of the goods respectively.
In actual use, we can increase or decrease inventory by writing corresponding MySQL stored procedures or triggers. For example, when an outbound operation occurs, we can write a trigger to automatically update the inventory quantity of the corresponding item in the inventory table.
The above is a brief introduction and code examples on how to design the MySQL table structure to manage warehouse inventory. Through reasonable table structure design and corresponding business logic implementation, we can achieve precise control and management of warehouse inventory and improve the company's operational efficiency and customer satisfaction.
The above is the detailed content of How to design MySQL table structure to manage warehouse inventory?. For more information, please follow other related articles on the PHP Chinese website!