Home >Database >Mysql Tutorial >How to design a maintainable MySQL table structure to implement online shopping cart functionality?
How to design a maintainable MySQL table structure to implement the online shopping cart function?
When designing a maintainable MySQL table structure to implement the online shopping cart function, we need to consider the following aspects: shopping cart information, product information, user information and order information. This article details how to design these tables and provides specific code examples.
CREATE TABLE cart ( cart_id INT PRIMARY KEY AUTO_INCREMENT, user_id INT, product_id INT, quantity INT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (user_id) REFERENCES user(user_id), FOREIGN KEY (product_id) REFERENCES product(product_id) );
CREATE TABLE product ( product_id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255), price DECIMAL(10, 2), description TEXT );
CREATE TABLE user ( user_id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255), email VARCHAR(255), password VARCHAR(255) );
CREATE TABLE order ( order_id INT PRIMARY KEY AUTO_INCREMENT, user_id INT, product_id INT, quantity INT, total_price DECIMAL(10, 2), created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (user_id) REFERENCES user(user_id), FOREIGN KEY (product_id) REFERENCES product(product_id) );
Through the above table structure design, we can implement a basic online shopping cart function. In actual use, you may need to adjust and expand table fields according to specific needs. Hope this article is helpful to you!
The above is the detailed content of How to design a maintainable MySQL table structure to implement online shopping cart functionality?. For more information, please follow other related articles on the PHP Chinese website!