In a simple sales sample I have 3 primary tables: Order, Product, Orders_Products:
Product table
+----+-----------+
| id | name |
+----+-----------+
| 1 | A |
| 2 | B |
+----+-----------+
order table
+----+---------+------------+------------+---------------------+
| id | user_id | total_cost | order_date | status_id |
+----+---------+------------+------------+---------------------+
| 1 | 5 | 25.00 | 2012-02-03 23:30:24 | 1 |
| 2 | 7 | 30.00 | 2012-02-13 18:06:12 | 1 |
+----+---------+------------+------------+---------------------+
orders_products table
+----+----------+------------+--------+
| id | order_id | product_id | cost |
+----+----------+------------+--------+
| 1 | 1 | 34 | 10.00 |
| 2 | 1 | 25 | 10.00 |
| 3 | 1 | 27 | 2.50 |
| 4 | 1 | 36 | 2.50 |
| 5 | 2 | 75 | 25.00 |
| 6 | 2 | 74 | 5.00 |
+----+----------+------------+--------+
But in my system:
A user adds money into his account then he can spend it
Products are services like Product A: user can post 5 Ads so when user post a Ads that 5 become 4 and so on.
How should I design DB for it??
You currently have no table to store customer data. I would strongly advise creating one using an Auto-Increment ID field, a name field and a field to store the money on their account which can be modified when the add money to it or purchase something.
In the Order table, the user_id field should be a foreign key from the Customer table I just told you how to create.
If you want to better understand what you should be doing, I would recommend looking at the Entity-Relationship Model
I don't entirely understand what your second question is, can you explain it in more detail? Are all the products just a set number of ads that the customer can post?
Related
I have a tracking table, the content of the tracking table contains products. I want to list the most followed products with procedure How can I do this?
Follow-up chart
| ID |KullaniciID| StokKodu | Tarih |
| 1 | 1 | AXERA | 2023-02-01 15:10:53.193 |
| 2 | 1 | ERKMF | 2023-02-02 10:27:17.373 |
| 3 | 2 | AXERA | 2023-02-02 10:27:16.643 |
| 4 | 3 | AXERA | 2023-02-06 16:27:28.873 |
In this table given as an example, I want to list 5 of the most followed products according to the stock code, how do I do this?
For some reason I am having difficulty wording this question but I will try my best. I've been searching for 2 days on and off now and haven't found a good solution to the issue.
I have a Table called InventoryNode;
_________________________________________________
| InvID | ID | Slot | ItemID1 | ItemID2 | ItemID3 |
|-------|----|------|---------|---------|---------|
| 1 | 1 | Neck | 10 | 22 | 66 |
| 1 | 2 | Head | 26 | 23 | 56 |
| 1 | 3 | Leg | 19 | 21 | 76 |
And another table called Inventory which stores the Node ID in each column
_____________________________
| ID| Neck | Head | Leg | ... |
|---|------|------|-----|-----|
| 1 | 1 | 2 | 3 | 66 |
If there a way I can insert the node ID's into the Inventory table based off the InvID and populate all the columns with the correct name with the Node's ID?
Something like this?
INSERT INTO Inventory INNER JOIN InventoryNode ON
(Inventory.ID = InventoryNode.InvID) WHERE Inventory.column_name =
InventoryNode.Slot SET InventoryNode.InvID
So I am building a webpage that shows a bunch of video games located in a SQL database and one suggestion I had was to have the different prices from each region display based on a drop down. My question is trying to figure out whats the best way to store int in the database. Would it be like:
GAME
CountryID
price1
CountryID
price2
CountryID
price3 ...
Or is there a better way to do this?
Just a heads up I've only been developing web applications for a year or so and I'm still pretty new to SQL.
Thanks for your input!
I would use multiple tables, one for games and one for region pricing.
Games
+--------+----------+
| GameID | GameName |
+--------+----------+
| 1 | Game1 |
| 2 | Game2 |
| 3 | Game3 |
| 4 | Game4 |
+--------+----------+
RegionPricing
+----------+--------+-------+
| RegionID | GameID | Price |
+----------+--------+-------+
| 1 | 1 | 60 |
| 1 | 2 | 55 |
| 1 | 3 | 45 |
| 1 | 4 | 80 |
| 2 | 1 | 50 |
| 3 | 2 | 30 |
| 3 | 3 | 25 |
| 3 | 4 | 45 |
| 4 | 1 | 60 |
| 4 | 2 | 55 |
| 4 | 3 | 45 |
| 4 | 4 | 80 |
+----------+--------+-------+
By using separate tables you minimize duplicate data and allow for easy granular changes. You may also consider adding a column to RegionPricing for currency. This would also need a Region table, with RegionID and RegionName.
i have 2 tables.
1.home_shop
+---+------------+
|id | product |
+---+------------+
| 1 | soap |
| 2 | cake |
| 3 | biscuit |
+---+------------+
2.office_shop
+---+------------+
|id | product |
+---+------------+
| 1 | key |
| 2 | lock |
| 3 | pen |
+---+------------+
what i want is union this two tables into a new table "complete_shop" with a flag indicating "home" and "office"
for example:
+---+------------+-------------+
|id | product | flag |
+---+------------+-------------+
| 1 | soap | home |
| 1 | key | office |
| 2 | cake | home |
| 2 | lock | office |
| 3 | biscuit | home |
| 3 | pen | office |
+---+------------+-------------+
how do i do this union in mysql please help me. i am a beginner
Do a UNION query and introduce the flag column using the appropriate values.
SELECT id, product, 'home' AS flag
FROM home_shop
UNION ALL
SELECT id, product, 'office' AS flag
FROM office_shop
ORDER BY id, flag
Note that you don't need to use a subquery to order here, you can just specify the columns you want to use.
I wonder what's the best practice is when you have a table like this below with orders. I need to calculate the total price of each order. Should I use triggers to calculate the price or should I hardcode the calculation before insert into database?
ORDERS:
id | Article | Price | Quantity | Total price
---------------------------------------------
1 | TV | 5 | 1 | 5
2 | CD | 3 | 2 | 6
3 | Book | 2 | 3 | 6
4 | XBOX | 1 | 1 | 1