How to design the inventory table structure of the mall in MySQL?
When designing the inventory table structure of the mall, you need to consider the attributes, classification, inventory quantity, price and other information of the product. The following will introduce how to design the mall's inventory table structure in MySQL with specific code examples.
First, we can create a table named products
to store the basic information of the product, including product ID, name, category, price, etc.:
CREATE TABLE products ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL, category VARCHAR(50) NOT NULL, price DECIMAL(10, 2) NOT NULL );
Next , you can create a table named inventory
to store product inventory information, including product ID, inventory quantity, etc.:
CREATE TABLE inventory ( id INT AUTO_INCREMENT PRIMARY KEY, product_id INT NOT NULL, quantity INT NOT NULL, FOREIGN KEY (product_id) REFERENCES products(id) );
In this table, the product_id
column As a foreign key, it is associated with the id
column of the products
table, ensuring that the product ID in the inventory table corresponds to the ID in the product table.
In addition, when designing the inventory table structure, you can also consider some other attributes, such as product specifications, colors, etc. Corresponding columns can be added to the products
table to store these attributes. For example, add a specification
column to store product specification information:
ALTER TABLE products ADD COLUMN specification VARCHAR(100);
In this way, we can The specification information of the product is obtained by querying the specification column in the products
table.
If the product has multiple specifications and multiple attributes, you can consider using other related tables to store this information, and then associate it to the products
table through foreign keys. Taking the color information of products as an example, you can create an association table named product_colors
to store the relationship between products and colors:
CREATE TABLE product_colors ( id INT AUTO_INCREMENT PRIMARY KEY, product_id INT NOT NULL, color VARCHAR(50) NOT NULL, FOREIGN KEY (product_id) REFERENCES products(id) );
In this table, product_id# The ## column is associated with the
id column of the
products table as a foreign key, and the
color column stores the color information of the product.
The above is the detailed content of How to design the mall's inventory table structure in MySQL?. For more information, please follow other related articles on the PHP Chinese website!