create table name from other table value - mysql

i am doing a simple shopping cart for my assignment. currently i am doing the planning for relational databases. here are the part of the requirements:
multiple seller with multiple products
multiple customer
cust can add many items in a cart
a cart (having many items) is saved after a transaction so cust can view them in transaction history
so the cart has cart_id tied to cust_id
if there are 10 transaction, 10 carts will be saved
each cart has many items.
this is the flow (my attempt) -
a cart with cart_id=10001 is create when cust=11 adds 1st item
lets say cust added 5 items in cart
transaction completed. 2 things happen:
cart_id is saved to master_cart table with cust_id
(on my part) a table is created with the name 10001 having prod_id and quantity
my prob is about the list how do i create a table having the name of the cart_id? also, is this the right way?
this is my draft for db.
is this the right way or how do i create table with the name based on other table row values?

You should not create a table for each cart. Dynamic information should be in table contents, not names.
You should have a single cart_items table with cart_id as a column.
CREATE TABLE cart_items (
cart_id INT,
prod_id INT,
quantity INT,
UNIQUE KEY (cart_id, prod_id),
FOREIGN KEY (cart_id) REFERENCES cart (cart_id),
FOREIGN KEY (prod_id) REFERENCES product (prod_id)
);

Related

What is the best way to keep info on 'stock levels' for a product in a database?

My database is called 'shopping' and it contains the following tables:
products (product_id, product_name, price, category_id)
product_category (category_id, cagtegory_name)
customers (customer_id, username, pass, fname, lname, email)
orders (order_id, order_date, product_id, customer_id)
Now I want to consider keeping records of each products stock levels.
i.e
product_id = 1
product_name = MICROSOFT Limited Edition Gears 5 Xbox One X, Tekken 7 & Project Cars 2 Bundle
price = 449.99
category_id = 2
stock _id = 1 ( references stock_id in product_stock table ) :-/ ?
I have considered THREE approaches to storing 'stock info' on products.
Approach a) Add a new Column in products table called 'stock_level' simple, and valid since stock level is entirely dependent on its primary key in this situation.
Approach b) Create a new table called 'product_stock' , with the columns 'stock_id, and stock_level'.
The confusion is arising in the logic here.
For some reason, having gone with approach b, I had made the primary key of product_stock table (stock_id) ALSO a foreign key, referencing back to the products table as:
product_stock.stock_id = products.product_id
i.e
product_stock.stock_id 1 REFERENCES BACK TO products.product_id 1
product_stock.stock_id 2 REFERENCES BACK TO products.product_id 2
etc etc
THE THIRD approach I thought about was keep the stock_id key as a Primary Key only, and sever the link back to Products table. Then in products, create a column called stock_id with is a FK referencing stock_id in product_stock table.
Which of the three approaches seems the best ? I for some reason, regret deleting the columns and relations set up which was giving me behavior as discussed in approach b.
The reason I liked approach b was because, if you hover over each stock_id key in product_stock, you can see instantly what product it relates to in the products table ( phpmyadmin ).
thoughts on this folks ?
Refer to question and read.
no need, its all on phpmyadmin and not required in this case.
refer to question.
As far as you explained, each product has only and only one stock value. The simplest way to proceed is to to store that information directly in a products table: option a) in your question.
Using a separated table would be useful if, for example, you were in a situation where you have several stores and you need to keep track of the stock level of each product in each store. You would then create a separated table, with a column to store the id of the product, another to store the id of the store, and a third column to store the level of this particular product in this particular stock.

Self join- When i'm trying to do a self join, my query is displaying matching and non matching values

Friends-
I have 2 tables, 1st one contains the details about history, 2nd contains details on current product details, i want to fetch the product whose prod.description has changed in the current. I got the answer for this.Now i have another table which contains both current and history details of the product, now i need the products whose description has changed, i'm trying this with self join,but i'm seeing both current and previous description
i have used the below query
select h.ProdName,h.ProdId, h.MfgDate, h.ProdDescription
from historycurrent c join historycurrent h on c.ProdId=h.ProdId where
h.ProdDescription != c.ProdDescription;
but i need only the last 3 records from the output.
What you want to do seems impossible with your current schema. This is because your table is de-normalized into a hybrid object table / transaction table. To produce the output you have, your table undoubtedly has six rows. The first three inserts added three objects ("D-Cold", "Otrivin" and "ZanduBalm") but the last three inserts updated those objects with a new description (and thus change the old records forever).
You need to split the table into two. So this:
create table historycurrent (
ProdName VARCHAR(20),
ProdId INT,
MfgDate VARCHAR(20),
ProdDescription VARCHAR(50)
);
Needs to become (something like):
create table product (
prodId INT PRIMARY KEY,
prodName VARCHAR(?),
mfgDate DATE
);
create table history (
historyId INT,
prodId INT, -- foreign key
changeDate DATE,
prodDescription VARCHAR(?)
);
Then you can query the history and do a simple join for the product info

Begining with Relationships with Foreign Key

I'm starting with MySQL database relashionships and I have a question.
I'm going to put in an exemple:
Table A = customer
Table B = products
Table C = sales
I wanna know how can I make a relationship with these tables where "sales" has only one customer and multiple produts.
The "sales" table can't have a Primary Key with both foreign keys, cuz it will have multiple products.
I could create a ´idsale´, but how I would structure the table to receive multiple products?
Thank you.
Costumers and Sales have a ONE-TO-MANY relation. You could add a customer reference on the Sales table
Table sales
- id PK
- customers_id FK
Sales and Product have a MANY-TO-MANY relation. You will need a new table to map this relation
Table sales_has_products
- sales_id FK
- products_id FK
I would recommend creating a fourth table, let's call it productSale, that has its own primary key, as well as foreign keys the sale ID and the product ID. Sale, then, will just have an ID and the foreign key of the customer ID (as well as any additional info you want, of course.)
This way, a sale can include multiple items of the same product (because all productSale rows have a unique ID), and all of the items included in a sale can be found by querying productSale by the specific sale ID.

MS Access Creating a New Order And Orderline

I have a Customer table, an Order table, an Orderline table and a Product table. All of them have an Autonumber field as their primary key, and Orderline has a foreign key reference to Order ID on Order table:
ORDER
-----
Order ID - Autonumber
Customer ID - Number
...
ORDERLINE
---------
OrderLine ID - Autonumber
Order ID - FK to Order
Product ID - FK to Product
Quantity
PRODUCT
-------
Product ID - Autonumber
Product details...
I have a form where I can choose a customer, and then a list of records from the Orderline table, and a query which I reference from this sub-form which lists the Order ID, Orderline ID, Product ID, Product details...
I have 2 problems.
All the orders appear, and I only want the ones associated with this order, (which should be none when the form first loads).
When I enter a Product ID that I want to add to a new order, I am expecting a new Order ID to appear, (Autoincremented) AND a new Orderline ID, (Autoincremented) and the details of the product that I have selected, corresponding to theProduct ID` I have entered, but instead I get this error message:
The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship. Change the data in the field or fields that contain duplicate data, remove the index, or redefine the index to permit duplicate entries and try again
The thing is, the tables should create me unique keys when I try to create the new record, and when I go into each table directly and enter a new record, the autonumber does work and does create a unique key - it is just when it is trying to create both the Order ID and the Orderline ID at the same time that it seems to be failing.
I should say, I have spent days on this, searched countless search engines, watched whole series of YouTube videos on creating Order forms but to no avail. Anyone who understands Access I am sure would be able to help me, as I would be able to help anyone in a similar circumstance in a matter of minutes if this was a problem in SQL.
When you create a sub-form you have to specify the relationship between the parent and sub-form. the same relationship you have created for your tables. Then only Access will filter the records for you.
Regarding your question. You should create a new [order] record in [order] table where you will be entering/selecting [customer_id, staff_id, order details ect]
one order can have more than one items so your [order_items] table (i assume orderline is the term you use for this table) exists of
order_id
product_id (order_id, product_id composite key)
quantity
price
etc..
Now when you want to start taking order you need to create a new form that is bound to tbl_order. In the frm_order you will have a sub-from which is bound to tbl_oder_items (in your case orderline)
The frm_order and frm_oder_items should be have a relationship. usually when you drag the table to create the subform ACCESS will ask to set the relationship. if you create the subform manually:
Select the sub-form
go to property sheet
select the link master field : order_id
select the link child field : order_id
Now when you open the frm_order it will show all the records (in other words all the products the order has in its list) from the tbl_order_items table.
Your tbl_order_item /orderline table also referencing to the product table via the product_id field.
Insert a combobox in the frm_order_items and bound it to the product_id. The combobox's rowsource would be
select product_id, product_name from tbl_product
This error message will occur:
'The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship. Change the data in the field or fields that contain duplicate data, remove the index, or redefine the index to permit duplicate entries and try again'
when you try to add a product twice for the same order. instead you should increase the quantity for the product.
Try this and let us know how it went.

Database Design Help-Online Checkout Project

I'm designing a database for an online checkout as a project. This is the first database I've made so go easy on me.
Anyways, I have tables "account", "order", "shopping cart" and "sku". Account holds the shipping info, name, phone, email, etc. The primary key is accountNumber. Order holds the billing information. It's foreign key is accountNumber and it's primary key is OrderID. Shopping Cart only has 2 columns-orderID and skuNumber. OrderID is the foreign key and skuNumber is the primary key. SKU has 4 columns-skuNumber, quanity, price, unit, and totalPrice. skuNumber is foreign and primary key.
My question is- the way I currently have this setup only 1 item sku can be ordered at a time. How would you design this differently so that wasn't necessary? I know I could do sku1, sku2, etc., but that seems like it would be against best practices.
Thanks!
Your database isn't normaized by this design, instead of the SKU table you will need two extra tables, becouse you simply need to separate the prodcts details(Name, price, etc...) from the Order details, so you will have to add the following tables:
Products:
Id.
Name.
Price.
...
Another one OrderItems or SKUItems that contains the following columns:
OrderID foreign key to the orders table.
ProductId a foreign key to the products table.
Quantity.
...
I didn't understand what SKU is but if it is some kind of product type, you can add an extra table ProductTypes: Id, name that holds product types, this table contains for example a product type called SKU then you can add a ProductTypeId to your Products, so you could have something like:
ProductTypeId | Name
------------------------
1 SKU
For the Products table:
ID | Name | ProductTypeId
-----------------------------
1 SKU1 1
2 SKU2 1
3 SKU3 1