I am trying to create a link table in mySQL to manage the association of products and skus. An example, the product is a t-shirt. The sku is each t-shirt's size and color combination. I want to handle products and skus many-to-many relationships because we sell items and bulk. Bulk products may be comprised of individual skus. Ergo, one sku could be associated with many products.
So, I am pretty sure I need products and skus to have a many-to-many relationship. What I don't understand though is how to poulate the Link table. In other words, when I create a product I generate a productid in the product table. When I create the sku I have a skuid in the sku table. How to I take the productid and skuid (each primary keys in the thier own table) and add them to the link table. Each skuID and productID pair should be unique so I would like those two fields to serve as my primary key.
Anyway, when I try to create the relationship in phpmyadmin using the designer I get "Error: relation not added".
so basically you are trying to link 2 tables 1 for t-shirt product and 2 thsirt size and color, for table 1 make a product id which is primary and for table 2 create a product id field similar to table 1 which will link your two tables the only difference is that in table 2 product id is not your primary key
Related
I am trying to build complex query in laravel for making sum for item quantity.
I have total four tables used in this query. My main table Inventories table
Inventories
My datatable is displaying by looping on inventories table.
I have another table is composite item table, which contains bunch of multiple items.
Composite Inventories
composite item and inventory table has pivot table which is relation between inventory table & composite item. relations something like many to many.
Composite has inventories
Now individual items or composite item are added to purchased order table
Purchase order
Purchase order table has one child table purchase order details which Contains item & composite items.
Purchase order details
In the purchase order details inventory_item field contains item or composite item name.
I need to get total quantity of item which has approved status in purchase order table.
For example inventory_item (item) = "stest2" (quantity="10"),
inventory_item (composite item) = "aa-buycomp" (quantity="2")
Now see setes2 item = 10 + composite item (stest2 quantity)
see stest2 item id in inventories table is "22"
Now check composite item "aa-buycomp" in composite item table it has id = "1"
Now see composite_inventorie_id = "1" & inventory_id="22" & type="purchase" in composite_has_inventories table which has quantity = "1"
I need to final sum of quantity = 10 + 1 = "11"
Please help to build laravel complex query.
I'm using PHP, and SQL to create a small website application for a bake shop.
I have established 2 data tables:
First one named Inventory with columns:
itemid INT(Primary key)
itemname text,
volume float,
volume type text (kg,g,lb,L,ml...)
quantity INT.
Second one named Product with columns:
prodid INT Primary Key
prodname TEXT
recipes (don't know which data type yet)
quantity INT.
The column recipes should contain numerous items from the inventory table. My friend wants it that if she make new product, the inventory database will automatically decrease the number of quantities while increasing the quantities
FOR EXAMPLE:
If she makes 100 buns(prodname) and the recipe for that 100 buns is
(quantity- itemname)
20- flour,
20- eggs,
10- sugars
then the quantity column in the inventory table should decrease by -20,-20,-10 Where the itemname are flour, eggs, sugars respectively and at the same time the product table with prodname buns should have an increase of 100 in quantity column.
In my database I have one table that contains a complete list of products, and another table that contains the same list of products on the x-axis, with a list of customers on the y-axis, where the value for each product can be 1 or 0 depending on whether that customer can view that product. My SQL looks like this:
SELECT products.product_code, products.product_type, products.product_category, products.product_title, products.product_description
FROM product_lists
INNER JOIN products
ON product_lists.product_code=products.product_code
WHERE product_lists.customer="1"
ORDER BY products.product_code
My problem is that I would like to create a view of this result for each customer to use as that customers product table, however when I create it I get the message "This table does not contain a unique column. Grid edit, checkbox, Edit, Copy and Delete features are not available." even though the product_code field is set as a primary key in both the products table and the product_lists table.
How can I create a join/view that uses the primary key from the table(s) it was created from? In short I would like the product_code field to become the primary key of my view.
Thanks!
I think the problem is the join. You can fix this by moving the condition to the where clause. MySQL doesn't allow subqueries in the from, but it does in the where:
SELECT p.product_code, p.product_type, p.product_category, p.product_title, p.product_description
FROM products p
WHERE EXISTS (SELECT 1
FROM product_lists pl
WHERE pl.product_code = p.product_code AND
pl.customer = 1
)
ORDER BY p.product_code;
Im building a shop and each product has a unique set of attributes.
the product db looks like this:
products_id, relation_id, product_name, description,
price, glass, shipping, img, cat, subcat, model
Since every product has several (~40) different attributes unique to that product only ive created a second table to store them.
products_id, att_name, att_val, att_head, att_standard, att_order
This works fine, because there will never be two unique rows.
The problem, however, is when i need to modify the attributes content.
using MySQL Workbench i can modify a row using something like
UPDATE product_attributes SET att_val='1500' WHERE products_id='112' AND att_head='threshold'
This however, doesn't seem to work when i update from my php script.
Is there an easy way to modify the table to support updating?
Im well aware of the stupidity not having an unique column.
But im not sure how to make the two tables relate.
Where should i store the unique id of the attributes?
One choice,
add a primary key "auto_incremented" into the product_attributes table...
ALTER TABLE `product_attributes` ADD `id` INT( 10 ) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST
This Id is just for CRUD (Create Read Update Delete) task.
The only relation you can have between your two tables is the products_id wich allow you to have few product_attributes for one product
Since 1 product has more than 1 unique attributes that u store in a second table, you should use the ID of the table product and store it in the second table with the attributes.
Hope this is what u need?
What is the best practice to do a searching form with filters, where the filters are depending on the category?
e.g.:If you go to visit the ebay and select a category then the filters are different on the left hand side in the cell phones (filters: brand, operating system...) or the fashion category (filters: size, color...)...
In my mind I would do more table in DB. Each table for one category (cat_cellphone, cat_fashion...). Then put a product one of these tables depending on the category (not one products table where one column contains the category ID).
These tables are different where the columns names are characterized by the category.
Next, should do more searching forms and call a form where the filter belongs to the category.
Is it a good concept or there is an other accepted practice in big projects?
No, having multiple tables is a bad idea.
In general. use a table with a primary key over two columns instead.
(The primary key may span more than one column.)
The columns would be categoryname / filtername.
If you don't like such primary keys, and always use an autoincrement column, you can still create an index over the two columns.
The columns would be: id / categoryname / filtername / filtertext
Using multiple tables to store your products isn't a good idea. Because there will be products that overlap in categories which would result storing the same product in more than one table.
Just use a product table with the ID, product_number, description, etc. and a category table to store the different categories. Then you could link them directly like:
Product table:
ID product_number description category
1 00001 Screwdriver 1
Category table:
ID description
1 Tools
And you could even expand the category table with an extra column to use subcategories by addressing the parent of the subcategory:
Category table with subcategories:
ID description parent
1 Tools NULL
2 Automatic tools 1
And if you don't like directly linking to the category table from the product table you could use a link table:
Product_category:
Product_ID Category_ID
1 1
Hope this answers your question.
Edit, added filter table:
To add filtering for a product you can use a table for a the filters and a link table, like so:
Filter:
Filter_ID description value
1 brand Bosch
2 brand Bahco
3 type Phillips
Product_Filter:
Product_ID Filter_ID
1 1
1 3
That way you can link more than one filter to each product and use the same filter more than once.
You could even expand this further by using another table for the filter values, but that could make things a bit to complicated:
Filter:
Filter_ID description value
1 brand 1
Filter_value:
Filter_ID value
1 Bosch
2 Bahco