


How can all users share one table in the database, but each user's order number has an independent namespace?
1. Actual scenario:
The order table in the database is public, and all users are on one order table.
How to make each user’s order number an independent namespace? For example, if user A places two orders, his order numbers should be C001 and C002, and if user B places three orders, the starting position of his numbers should still be C001, not C003, and all his order numbers should be C001. C002,C003.
How to do this?
Is it necessary to save a field under each user for how many orders he has currently opened, and then read it out first, add one, and then save it back? This is not good... Is there a better way to do it on the database side? I seem to have heard someone talk about using database functions or triggers?
2. If the database can do the above, does this C001 generally exist in the primary key field? Or is the primary key still an incremental primary key of numeric type, just adding a field to the table to store this C001?
Note: It is in the Mysql database.
Reply content:
1. Actual scenario:
The order table in the database is public, and all users are on one order table.
How to make each user’s order number an independent namespace? For example, if user A places two orders, his order numbers should be C001 and C002, and if user B places three orders, the starting position of his numbers should still be C001, not C003, and all his order numbers should be C001. C002,C003.
How to do this?
Is it necessary to save a field under each user for how many orders he has currently opened, and then read it out first, add one, and then save it back? This is not good... Is there a better way to do it on the database side? I seem to have heard someone talk about using database functions or triggers?
2. If the database can do the above, does this C001 generally exist in the primary key field? Or is the primary key still an incremental primary key of numeric type, just adding a field to the table to store this C001?
Note: It is in the Mysql database.
1: There is no need to store them in the database like this. You only need to traverse all the orders and display them in order.
2: Add static member variables
to each user attribute, and add corresponding fields to the database. The data added each time will be what you said.
What the poster means is to establish a unified order management library for different customer accounts. Different accounts can only see their own orders, which is somewhat similar to cloud ERP.
The order table can have a redundant primary key id. In terms of data storage uniqueness, it can be viewed as order code + user id to uniquely confirm a piece of data.
If you need to optimize the design of the table structure, you can automatically create an order table according to each user, and separate the order data of each user. The table name is similar to order_userid. Each time the corresponding table name is found according to the user, the data is fetched, but at the end The comprehensive background statistics of all user order data may be a little troublesome.
How can you ensure the uniqueness of your order
If the database is designed according to the meaning of the title, only the operation of finding orders will cause an exception. Because we cannot determine the unique order based on the order number.
The value of the primary key is used to uniquely identify a record in the table.
(About your project
I suggest:
The primary key of the Order table can be OrderId
And the primary key of the User table can be UserId
I think your problem should lie in the basic understanding of database tables. It is recommended to read Database Normalization related content )
Use an orderid table to store the IDs of all users' next orders. If you find it troublesome, it is recommended that you use time as part of the order number.
Cannot be used as a primary key. There is a principle in primary key design - it must be irrelevant to the business. Therefore, the primary key will always be a meaningless auto-incrementing ID or the like.
I don’t see what actual need your question is trying to solve
The design of table structure must comply with the requirements of the three most basic paradigms. The purpose of the three paradigms is to ensure that there is no redundancy or dependency between fields. I won’t repeat the issues mentioned above. Your design like this violates the principles of the three major paradigms of database table structure design. I suggest you clarify your requirements carefully. You should not do such a design, or we have misunderstood it. You rephrase the question. Or directly post your table creation statement.
You can use a joint primary key to combine uid and cid, which ensures uniqueness. But please pay attention to the following two points:
1. To use mysql joint primary key auto-increment, you need to use MyISAM as the storage engine.
2. When using a union to auto-increment the primary key, the auto-increment key cannot be the leftmost key of the primary key.
Just add a user_owner_order_key to the order table, and then write your own code to handle the exchange logic between C001 and the real order_id.
<code>//伪码 $user_owner_order_id = (select count(*) from order where user = $uid and order_id </code>
To talk about another issue, generally third-party payment
cannot make multiple payments for the same order number
, so it is best to consider carefully whether to do so.
Solution
The user table can have a field to store the order quantity. Every time there is a new order, add one to this value and then update it back;
The new order gets the number of existing orders, plus one. However, when deleting the order, it must be soft deleted

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

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

Atom editor mac version download
The most popular open source editor

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

Zend Studio 13.0.1
Powerful PHP integrated development environment