How to enter multiple foreign keys to one column - mysql

Hi I am making a restaurant management system for an assignment and I have a customer table, item table and order table. I want to enter the customer ID and the item ID to the order table when a customer places an order. My question is how to handle this when one customer orders several items together, how can I insert this to the order table? Is there a way to enter the foreign key of multiple items to the item ID column of the order table?
I am using MySQL server 5.7, Java 1.8 and NetBeans 8.2
Thanks!

Entry in Order table should per user not per Item. And if you want to insert items their are two ways one is in Order table as meta with order entry and second is create another item table with order id as ref key.

You need two table. Order and Order Details.
Order Table will contain Order ID, Customer ID, Date, Total Price, Tax, Offer, Coupon and other details regarding order.
And Order Details will contain Order ID, Item IT, Price, Quantity, Discount, and other details regarding each order item.

Related

If statement in access queries in choosing 2 prices from 1 product based on Quantity Ordered

So, lets say I have 3 tables.
First table is the product table where it consists of
Prod_code, Prod_name, Prod_qty, Prod_Price1, Prod_Price2.
The second table is the Order table where it consists of
Order_ID, Order_Date, Total_price.
And the last table is the order detail table which consists of
Order_Detail_ID, Order_ID (foreign key from order table), Prod_code(foreign key from Product table), Qty_Ordered, and Price
If you can see I have 2 prices for each product in the product table for the purpose of:
If the Qty_ordered from the order detail table is greater than 10 unit, then the Price column in the order detail given will be Prod_price2
If the Qty_ordered from the order detail table is equal to or less than 10 unit, then Price column in the order detail given will be Prod_price1
How can I make this happen so when I create an order detail form, it can automatically give me the Product price based on the Qty_ordered that I input?
I believe this could require some if statements in the query but I'm a novice in Ms. Access and I need you guys' help. Thanks a lot
In a query that joins Orders and Products to OrderDetails, calculate a field with:
IIf(Qty_Ordered <= 10, Prod_Price1, Prod_Price2)
If you need to display this on data entry form bound to OrderDetails, build a combobox that lists products and has both prices as columns (can be hidden if you want). An expression in textbox can reference columns of combobox by their index. So if Price1 is in third column its index is 2.
=IIf(Qty_Ordered <= 10, Me.cbxProduct.Column(2), Me.cbxProduct.Column(3))
Saving this calculated result will require code (macro or VBA) in some event, probably form BeforeUpdate.
Me!Price = Me.tbxPrice

Group by the same ID with diferent diseases

I have a table
each different disease create a new row with the same ID, i want to group this rows to transform the ID into a primary key. I already tried a bunch of queryes without success.
With group by you can group by data with a column. Here you want to group by your ID and keep the maximum of every ID. So you can use GROUP BY and display the maximum value with MAX().
SELECT ID, MAX(DISEASE1), MAX(DISEASE2), MAX(DISEASE3)
FROM table
GROUP BY ID

Alternatives to junction table?

I'm designing a relational database tables for storing data about eCommerce scenario where I need to store
List of Products purchased by a user
List of users who purchased a particular product.
Its a many to many relationship.
So far I could only thinking of doing this.
create a table for storing orders
table recordorders(
userID // foreign key from users table
productID, // foreign key from products table
dateofpurchase,
quantity,
price_per_unit,
total_amount
)
It will act like a junction table.
Is this a good approach and are there any other methods than junction table that are more effective and efficient for querying ?
Your bullets describe two tables, not one. Your junction table is not properly described as two lists. It is a set of order info rows. The junction table you gave holds rows where "user [userID] purchased product [productID] on ...". Ie it records order info. (Combinations of user, product, date, etc of orders.) Given a user or product, you can get the corresponding bullet table by querying the order info table.
However your table probably needs another column that is a unique order id. Otherwise it cannot record that there are two orders that are alike in all those columns. (Eg if the same person buys the same product on the same date in the same quantity, price and total.) Ie its rows probably aren't 1:1 with orders. That's why above I called it an order info table rather than an order table. It records that some order had those properties; but it doesn't record distinct orders if there can be orders with the same info. It's really a many-to-many-to-etc (for every column) association. That is why an order id gets picked as a unique name for an order as further info. This new table would be called an entity table, not a junction or association table. It holds rows where "in order [id] user [user] purchased ...".
PS An order is usually something that can be characterized as an association on/among/between an order id, user, set of order-lines (product, quantity, price & total), and other stuff (date, grand total, etc). The orders are usually relationally characterized by an order entity table on order id with its user, date etc plus an order-line association table on order ids and their order-line info.
PPS It's time for you to read a book about information modeling and database design.
You don't "store" those two things in a table (Junction, or otherwise), you discover them from the raw ("Fact") data:
Using your proposed table:
List of Products purchased by a user:
SELECT productID
FROM recordorders
WHERE userID = 123;
List of users who purchased a particular product:
SELECT userID
FROM recordorders
WHERE productID = 987;

How to store sales in groups of orders

For a purchase-scenario:
I have a table (PRODUCTS) that contain columns like NAME, TYPE, DEFAULTPRICE, ID and a table with sales (SALES) that contain columns like DATE, ID, PRICE, AMOUNT where ID in SALES is a foreign key to primary key ID in PRODUCTS. Very simple, not much to mess up there.
This works fine as long as you insert one row of whichever product (and how many) at what total price.
BUT - let's say I want to design this system to support "basket"-kind of shopping. I.e. a user selects several items that are not the same product ID, but gets a single ORDER ID that contains all the products that were included in the order, the amount of each product, the total price, date etc.
How would one go about creating this? I was thinking maybe creating a table called "ORDERS" and link every row from SALES to an ID from ORDERS - but I'm not sure.
You would create a table called Orders for each order. It would have whatever information you want about the order.
You would then have a table for the products in each order. A common name is OrderLines. This would have an OrderId, linking it to the Orders table and a ProductId linking to the Products table.

Magento DB customer orders mapping

How are Magento orders mapped to the customer? Does the order contain the customer ID or does the customer contain all their order ids? Trying to figure out if changing order numbers will mess up customer mapping.
Let me answer your questions at first
How are Magento orders mapped to the customer?
Magento Orders are mapped to the customers using "customer_id" Foreign Key field in the order tables "sales_flat_order","sales_flat_order_grid","sales_flat_order_address". And Customer entity has no reference to the orders in turn.
Does the order contain the customer ID or does the customer contain all their order ids?
Order only contains the customer ID not the other way around. Basically Customer entity follows the EAV structure like products and categories.
Now coming to your task of changing Order Numbers (technical field name increment_id).
Yes you can change the increment numbers of the order. You need to do that in two tables
1. sales_flat_order
2. sales_flat_order_grid
Please make sure to reset the order increment last id of entity type id "5" which represents Order after completing your task of changing the order numbers. You should do this in the table "eav_entity_store"
Let me know if you need more information regarding this.