1. Actual scenario: the order table in the database is public, and all users are on one order table. How can I make the order number of each user an independent namespace? For example, if user A has placed two orders, his order numbers should be C001 and C002, and User B has placed three orders. the starting position of his number should also be... 1. actual scenarios:
The order table in the database is public, and all users are on one order table.
How can I make the order number of each user an independent namespace? For example, if user A has placed two orders, his order numbers should be C001 and C002, and User B has placed three orders. the starting position of his number should be C001 rather than C003, all his order numbers should be C001, C002, C003.
How can we achieve this?
Is it necessary to store a field under each user for how many orders he has opened, and then read it out first, add one, and save it back? This is not good ...... Is there a better way to do it on the database end? It seems that I heard people talk about using database functions or triggers?
2. if the database can do this, does this C001 usually exist in primary key fields? Or is the primary key Still an incremental primary key of the numeric type, but adding a field in the table to store this C001?
Note: it is in the Mysql database.
Reply content:
1. actual scenarios:
The order table in the database is public, and all users are on one order table.
How can I make the order number of each user an independent namespace? For example, if user A has placed two orders, his order numbers should be C001 and C002, and User B has placed three orders. the starting position of his number should be C001 rather than C003, all his order numbers should be C001, C002, C003.
How can we achieve this?
Is it necessary to store a field under each user for how many orders he has opened, and then read it out first, add one, and save it back? This is not good ...... Is there a better way to do it on the database end? It seems that I heard people talk about using database functions or triggers?
2. if the database can do this, does this C001 usually exist in primary key fields? Or is the primary key Still an incremental primary key of the numeric type, but adding a field in the table to store this C001?
Note: it is in the Mysql database.
1: The database does not need to be stored in that way. you only need to traverse all the orders and display them in order.
2: Add each user attributeStatic Member variableAnd add fields to the database. The data added each time is what you say.
The main idea is to create a unified order management database for different customer accounts. different accounts can only see their own orders, a bit 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 the order code + user id to uniquely confirm a piece of data.
To optimize the table structure, you can create an order table automatically based on each user and separate the order data of each user. The table name is similar to order_userid. each time you find the corresponding table name to retrieve data, however, in the final integrated background, it may be a little difficult to calculate the number of orders for all users.
How can you ensure the order is unique?
If the database is designed according to the description, onlySearch for ordersThis operation will cause an exception. Because the order number cannot be determined.Unique order.
The primary key value is usedUniqueIdentifies a record in the table.
(About your project
My suggestion:
The primary key of the order table can be OrderId.
The primary key of the user table can be UserId.
I think your problem lies in the basic understanding of database tables. Read databaseNormalization.)
-
Use an orderid table to store the IDs of all users' next orders. if you are in trouble, we recommend that you use time as part of the order number.
-
It cannot be used as a primary key. the primary key design principle must be business-Independent. Therefore, primary keys are always meaningless auto-increment IDs.
I don't know what your actual needs are to solve this problem.
The design of the 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 will not repeat the questions mentioned above. your design violates the principles of the three paradigms in database table structure design. we recommend that you properly understand your needs and should not do this design, or if we get it wrong, repeat the problem. Or paste your table creation statement directly.
You can use the joint primary key to combine uid and cid to ensure uniqueness. Note the following:
1. to use mysql for primary key auto-increment, use MyISAM as the storage engine.
2. when the Union primary key auto-increment function is used, the auto-increment key cannot be the leftmost key of the primary key.
Add a user_owner_order_key to the order table, and write the code to process the switch logic between the C001 and the real order_id.
// Pseudo code $ user_owner_order_id = (select count (*) from order where user = $ uid and order_id <$ currentOrderId order by order_id asc) + 1; $ user_owner_order_key = $ uid. fillZero ($ user_owern_order_id );
Let's talk about another problem. generallyThird-party paymentForSame order numberIt is impossible to make multiple payments, so it is best to consider whether or not to do so.
Solution
-
The user table can have a field to store the order quantity. each time a new order is generated, the value is added with one and updated;
-
Add one to obtain the number of existing orders for a new order. However, this order must be deleted in a soft manner.