Home > Database > Mysql Tutorial > body text

How to create an order details table for a grocery shopping system in MySQL

WBOY
Release: 2023-11-01 08:17:03
Original
879 people have browsed it

How to create an order details table for a grocery shopping system in MySQL

How to create the order details table of the food shopping system in MySQL

When developing the food shopping system, the order details table is a very important data table. It records the product details in each order, including product ID, quantity, price and other information. This article will introduce how to create an order details table for the grocery shopping system in MySQL, and attach specific code examples.

  1. Create database and data table

First, create a database named buy_vegetables in MySQL. You can use the following command:

CREATE DATABASE buy_vegetables;
Copy after login

Then, select the database:

USE buy_vegetables;
Copy after login

Next, create the order details table order_items. The table contains the following fields:

  • id: order detail ID, which is an auto-incrementing primary key
  • order_id: order ID, and order The order ID in the table is associated with
  • product_id: product ID, which is associated with the product ID in the product table
  • quantity: product quantity
  • price: Product unit price

You can use the following code to create the order_items table:

CREATE TABLE order_items (
  id INT AUTO_INCREMENT PRIMARY KEY,
  order_id INT,
  product_id INT,
  quantity INT,
  price DECIMAL(10, 2)
);
Copy after login
  1. Add foreign key constraints

To ensure the integrity of the association, foreign key constraints can be added to the order_id and product_id fields. First, create the orders table and the products table, and then add foreign key constraints respectively.

  • Create the orders table, containing the following fields:

    • id: order ID, which is an auto-incrementing primary key
    • customer_id: Customer ID, associated with the customer ID in the customer table
    • order_date: Order date

    You can use the following code to create the orders table:

    CREATE TABLE orders (
      id INT AUTO_INCREMENT PRIMARY KEY,
      customer_id INT,
      order_date DATE
    );
    Copy after login
  • Create the products table with the following fields:

    • id: Product ID, an auto-incremented primary key
    • name: Product name
    • price: Product price

    You can use the following code to create the products table:

    CREATE TABLE products (
      id INT AUTO_INCREMENT PRIMARY KEY,
      name VARCHAR(255),
      price DECIMAL(10, 2)
    );
    Copy after login
  • To add foreign key constraints, use the following code:

    ALTER TABLE order_items
    ADD CONSTRAINT fk_order_items_orders
    FOREIGN KEY (order_id) REFERENCES orders(id),
    ADD CONSTRAINT fk_order_items_products
    FOREIGN KEY (product_id) REFERENCES products(id);
    Copy after login
  1. Inserting data example

In order to demonstrate the usage of the order details table, the following is an example SQL statement example to insert data:

INSERT INTO orders (customer_id, order_date)
VALUES (1, '2021-01-01');

INSERT INTO products (name, price)
VALUES ('苹果', 5.99),
       ('橙子', 4.99);

INSERT INTO order_items (order_id, product_id, quantity, price)
VALUES (1, 1, 2, 11.98),
       (1, 2, 3, 14.97);
Copy after login

The above code is to# Some sample data is inserted into the ##orders table and the products table, and order details are inserted into the order_items table.

Summary

This article introduces how to create the order details table of the grocery shopping system in MySQL, including creating a database, data table and adding foreign key constraints. It also provides examples of inserting data to facilitate developers' understanding and practice. The design and use of the order details table is an important link in the development of the grocery shopping system. Through reasonable table structure and foreign key constraints, the integrity of the order data can be ensured. I hope this article will be helpful to developers developing grocery shopping systems.

The above is the detailed content of How to create an order details table for a grocery shopping system in MySQL. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!