Home > Database > Mysql Tutorial > Establishing a product brand table for the grocery shopping system in MySQL

Establishing a product brand table for the grocery shopping system in MySQL

WBOY
Release: 2023-11-01 15:01:56
Original
602 people have browsed it

Establishing a product brand table for the grocery shopping system in MySQL

Title: Establishment of commodity brand table in MySQL grocery shopping system

Introduction:
With the development of e-commerce, more and more people choose to buy online Grocery shopping system is one of the popular ways to buy food and groceries needed for daily life. When building a database for a grocery shopping system, establishing a product brand table is an essential step. This article will introduce how to use MySQL to build the product brand table of the grocery shopping system, and provide specific code examples.

1. Create database and product brand table

First, open the MySQL database terminal or other MySQL client and create a new database for the vegetable shopping system, such as "buy_vegetables".

CREATE DATABASE buy_vegetables;
Copy after login

Then, set the database as the currently used database.

USE buy_vegetables;
Copy after login

Next, create a product brand table and define the data types and attributes of each field.

CREATE TABLE `brand` (
  `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '品牌ID',
  `name` VARCHAR(100) NOT NULL COMMENT '品牌名称',
  `logo` VARCHAR(200) DEFAULT NULL COMMENT '品牌Logo图片链接',
  `description` TEXT COMMENT '品牌描述',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='商品品牌表';
Copy after login

In the above code, we define a table named "brand", which contains the following fields:

  • id: Brand ID, using an auto-increasing integer type, as Primary key.
  • name: Brand name, in string type, with a length of 100, and is not allowed to be empty.
  • logo: Brand logo image link, using string type, length is 200, allowed to be empty.
  • description: Brand description, using text type, used to describe brand-related information.

2. Insert sample data

After creating the product brand table, we can insert some sample data into the table for subsequent operations and queries.

INSERT INTO `brand` (`name`, `logo`, `description`)
VALUES
  ('品牌A', 'http://example.com/logoA.png', '品牌A的描述信息'),
  ('品牌B', 'http://example.com/logoB.png', '品牌B的描述信息'),
  ('品牌C', 'http://example.com/logoC.png', '品牌C的描述信息');
Copy after login

The above code inserts three sample data into the brand table, namely brand A, brand B and brand C, including brand name, logo image link and brand description.

3. Practical Application

In practical applications, more fields can be added to the product brand table according to specific needs to meet different business needs. For example, you can add fields such as creation time and modification time, and can also be associated with other tables to implement more complex functions.

ALTER TABLE `brand`
ADD COLUMN `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
ADD COLUMN `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间';
Copy after login

The above code adds "created_at" (creation time) and "updated_at" (modification time) fields to the product brand table, which are used to record the creation time and latest modification time of the brand respectively.

Summary:
Through the introduction of this article, we have learned how to use MySQL to build the product brand table of the grocery shopping system, and provided relevant code examples. In practical applications, the table structure can be expanded and optimized according to business needs to meet system requirements. Establishing a product brand table is an important part of the database design of the grocery shopping system. It provides basic support for subsequent product management and query.

The above is the detailed content of Establishing a product brand table for the 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