


MySQL table design practice: Create a coupon table and usage record table
MySQL table design practice: Create a coupon table and usage record table
In many business scenarios, coupons are a common promotional tool. In order to effectively manage and track the usage of coupons, we need to design a coupon table and a usage record table. This article walks you through how to create these two tables and provides corresponding code examples.
Coupon table design
First, we need to create a coupon table to store all available coupon information. Here is an example of a basic coupon table design:
CREATE TABLE `coupon` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `code` VARCHAR(50) NOT NULL, `discount` DECIMAL(10,2) NOT NULL, `valid_from` DATETIME NOT NULL, `valid_to` DATETIME NOT NULL, `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`), UNIQUE KEY `code_UNIQUE` (`code`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
In the above design, we have used the following fields:
id
: Coupon’s Unique identifier, using an auto-increasing integer as the primary key.code
: The code of the coupon, usually a string of characters or numbers.discount
: The discount amount or discount ratio of the coupon.valid_from
: The coupon’s validity start date and time.valid_to
: The coupon’s validity end date and time.created_at
: The creation time of the coupon, automatically generated using MySQL's CURRENT_TIMESTAMP function.
Usage record table design
Next, we need to create a usage record table to record the usage of each coupon. The following is a basic usage record table design example:
CREATE TABLE `coupon_usage` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `coupon_id` INT(11) NOT NULL, `user_id` INT(11) NOT NULL, `used_at` DATETIME NOT NULL, PRIMARY KEY (`id`), KEY `coupon_id_idx` (`coupon_id`), KEY `user_id_idx` (`user_id`), CONSTRAINT `coupon_id_fk` FOREIGN KEY (`coupon_id`) REFERENCES `coupon` (`id`), CONSTRAINT `user_id_fk` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
In the above design, we used the following fields:
-
id
: usage record Unique identifier, using an auto-increasing integer as the primary key. -
coupon_id
: The ID of the coupon, corresponding to theid
field in the coupon table. -
user_id
: The ID of the user who used the coupon. -
used_at
: The usage time of the coupon.
In order to ensure the consistency and integrity of the data, we also created two foreign key constraints, connecting the coupon_id
field with the coupon
table The ##id field is associated, and the
user_id field is associated with the
id field in the
user table. Doing this ensures that every usage record is valid.
The above is the detailed content of MySQL table design practice: Create a coupon table and usage record table. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



MySQL Table Design Guide: Creating a Simple Schedule Table In modern society, time management and scheduling are becoming more and more important. In order to better organize and arrange our daily activities, we can use a database to create a simple schedule to record and manage our schedule. This article will provide you with a MySQL table design guide to help you create a simple schedule table. First, we need to create a table named "schedule" to store scheduling information. The following is the creation

Use PHP to develop simple coupon and discount calculation functions. With the rapid development of e-commerce, coupon and discount calculation functions have become an important means to attract users and increase sales. In this article, we will introduce how to develop a simple coupon and discount calculation function using PHP to help you better understand this process. First, we need to create a PHP file named discountCalculator.php. In this file we will write a DiscountCalcula

MySQL table design tutorial: Create a simple user information table In the process of developing web applications, user information is a common requirement. In order to conveniently store and manage user information, we can use the MySQL database. This tutorial shows you how to create a simple user information table and provides corresponding code examples. Let's first define the fields of the user information table. A basic user information table can contain the following fields: id: the user's unique identifier, as the primary key. username: username, used for login

How to design the mall's coupon table structure in MySQL? With the rapid development of e-commerce, coupons have become one of the important marketing methods to attract users. In a shopping mall system, it is very important to properly design the structure of the coupon table. This article will introduce how to design the coupon table structure of the mall in MySQL and provide specific code examples. Basic attributes of mall coupons First, we need to clarify the basic attributes of mall coupons. Generally speaking, a coupon includes the following attributes: Coupon ID: Each coupon should have a

As a leading and popular online travel service app in China, Ctrip travel app can help users achieve packaged services from home to hotels. In addition, the app also has a large number of discount coupons, which can allow users to Save money easily and travel happily. So how do you check the coupons you have on the Ctrip travel app? Users who want to know can come and follow this article to learn more! First, we open the Ctrip travel app and click the "My" option in the lower right corner. You can see the coupon card package function in the My page. When we click to enter, we can see all the coupons owned by Zi.

MySQL Table Design Guide: Creating a Simple Blog Tag Table When designing a database, a good table structure is very important. This article will show you how to create a simple blog tag table. First, we need to determine the definition of blog tags. In most blogging systems, tags are used to categorize and organize posts. Each article can have multiple tags, and each tag can be used by multiple articles. Based on the above definition, we can create a table named "tags" to store blog tag information. The following is to create "tag

Launching coupons on the Douyin platform is an effective strategy to attract more users and increase sales. However, for many merchants, there may be some difficulties in how to set up and manage coupons on Douyin. This article will explore in detail how to publish and manage coupons on the Douyin platform, as well as how to create unique coupons to help you improve your sales. In this highly competitive e-commerce environment, coupons can not only attract more users, but also increase users' willingness to purchase your products. Therefore, learning to use coupons flexibly on the Douyin platform will become one of the keys to your business success. 1. How to put coupons on Douyin products? Log in to Douyin Open Platform: Log in to Douyin Open Platform and enter the merchant backend management page. Select products: on the merchant backend management page

How to use PHP to develop a simple coupon function requires specific code examples. With the development of e-commerce, coupons have become an important means of attracting users. In this article, we will introduce how to develop a simple coupon function using PHP and provide specific code examples. 1. Design the database First, we need to design the database to store coupon information. We can create a table named "coupons" with the following structure: CREATETABLEcoupons(idin
