MYSQL auto fill column values from another table - mysql

im very new to databases :) now with that said.
I have a table (sellers) with the columns: Seller_id, Seller_logo. There are 5 sellers.
I have a table (details) with the columns : Details_id, Seller_logo, Product, Price. There are 1000 Products.
Today i decided to insert a new column at the (sellers) table, with name "seller_name" and i fill in the values very easy since there are just 5 sellers.
The problem : How can i add in the (Details) table, a new column "seller_name" and automatic fill the values of the from the (sellers) table?
*i created a relation between Seller_logo on the tables (Sellers and Details)
Thanks in advance.

The relationship should be by Seller_id and I suppose the details table is for products? I would recommend this structure and then when you query the details table you would ideally use MySQL Join Syntax http://dev.mysql.com/doc/refman/5.0/en/join.html to get the related seller_name
**Sellers**
1. Seller_id
2. Seller_name
3. Seller_logo
**Details**
1. Details_id
2. Seller_id (foreign key)
3. Product
4. Price

Related

Mysql - normalization of database

I have a MySQL database and I want to normalize it in an efficient way. I have a table in my database in which i am getting data into column in the form of array. Actually I have three tables (details, country, material). In table "country" I have two column that are (id,country_name). In table "material" I have two column (id, material) and in table "details" i have four column (id, vendor, countries, materials). In this scenario, each vendor has multiple countries and materials and I am inserting ids of country and material in details table. Here is the screenshot:
so how I accurately normalize my database? Thanks in advance
Remove countries and materials columns from Details table.
You can create a fourth table which will depict the relationship between vendor,country and material:
vendor_id country_id material_id
1 3 5
1 6 9
1 7 24
Here all the columns will be foreign key and together they will form composite primary key
you need to design two new tables detail_country and detail_material also you need to modify your detail table as fellow
detail : id , vendor
detail_country : detail_id , country_id
detail_material : detail_id , material_id

INSERT data from one table with unique id to another table with multiple ids

i have a big problem i have been trying to solve for hours without luck.
I need to edit or insert lines into an existing table and getting the data from another table, but the second table has multiple lines with same productid.
The structure of these tables are different, one of them as an unique id (i think this is the right definition, but i am not sure), meaning that each line has its own unique id (i am sorry but i am not able to explain this with proper language, so i hope you understand) and that you will not find 2 lines with the same id. Table 2 has another structure and it has no unique id, it only has 2 fields, productid and categoryid, and i have multiple lines with the same productid
The table from which i need to get data has the following structure:
id
cid
The table where i need to insert or update data has the following structure:
productid
categoryid
The problem is that table 2 has multiple productids, meaning that i can have more than 1 line with the same productid (ex. productid 20 categoryid 3 and again another line with productid 20 and categoryid 4), and in my existing table 2 there are already many productid entries that have more than 1 line with the sameproduct id, because this is used to select multiple categories for a single product, and i would like not to lose existing information but also i would like to understand how to recreate this table 2 from scratch inserting new lines.
I need table 2 to have at least 1 line with table 1 id as productid and table1 cid as categoryid, if possible without deleting existing data of multiple categories in table 2.
So i have 2 solutions:
lose existing multiple categories information deleting table 2 and recreate it with some code similar to the code i used below inserting data from table 1, please check my code because i am a noob.
update table 2 and avoid losing data for multiple category but assure that i have at least 1 entry with the main categoryid for each productid.
What i need is to set the categoryid field of table 2 equal to the cid field of table 1 where productid of table 2 is equal to id of table 1
So basically something like:
INSERT INTO dz3_properties_product_category (productid, categoryid) SELECT
id, cid FROM dz3_properties_products;
These last query gives me duplicated primary key error, i do not understand what to do..
Maybe something like
update properties_products p join
properties_category l
on p.id = l.productid
set l.categoryid = p.cid;

MYSQL Move data from one table column to another table column if condition is matched

i'm not really an mysql guy more like a php guy :)
I have an issue with copying values from one table column to another table column.
The trick is that the data should be copied only if condition is matched. So basically i want to transfer categoryID from one table to another if postIDs are the same.
i have two tables news and news_in_category
in news table i have the following columns (id, title, categoryID)
in news_in_category i have the following columns (newsId, newsCategoryId)
So i want to move newsCategoryId to categoryID if newsId is the same as id.
For example if news table id=99 it should look up in news_in_category table find the newsId with value 99 and copy the newsCategoryId value to news table categoryID with id 99
Hope it makes sense to you :)
Thank you!
Try:
UPDATE news n
JOIN news_in_category nic ON n.id = nic.newsId
SET n.categoryID = nic.newsCategoryId
It looks like you might have a redundant functional dependency: newsId -> categoryId. If so, you could drop the news_in_category table without any loss of information, after running the query above.

Adding a multiple values to single column in mysql

in my project a table in mysql database contains two columns one is "UserId"(Primary Key) and other is "Merchants". For example user_A has merchants a,b,c. Now how can i add or delete a merchant for the user_A ?
You should normalise your data structure a have a separate table for users' merchants with user id and merchant as 2 columns. The 3 merchants in the above example will be 3 records in this table. You can easily delete a merchant from this table using a delete statement.
You probably will need another table for merchant details as well.

Store a unique reference to a Mysql Table

I am creating a site that is sort of ecommerce-ish. I want to give my users a perfect search ability using specific attributes that differ from product to product. I plan to create 1 products table storing the basic information that is shared among products i.e Name, Description, Price and a few others. Then I plan to create several "details" table say categories_computers with columns Processor, HDD, RAM, etc and another table say table_shoes with columns MATERIAL, SIZE, GENDER, etc.
I am new to Mysql but not to the concept of Databases. I don't think I will have a problem storing this data to each table. My issue comes about from reads. It won't be hard to query a product id but I think it would be extremely wasteful to query all details tables to get the details of the product since 1 product can only have 1 details.
So my question is how can I store a reference to a table in a column so that a product has say ID, Name, Description, Price, Details_Table_ID or something similar to save on queries. Do tables have unique ids in Mysql? Or how does the Stackoverflow community suggest I go about this? Thanks.
EDIT
Silly me, I have just remembered that every table name is uniques so I can just use that, so my question changes to how I can write a query that contains one cell in a table A to be used as a reference to a Table name.
Don't use separate details tables for each category, use a generic details table that can store any attribute. Its columns would be:
Product_ID INT (FK to Products)
Attribute VARCHAR
Value VARCHAR
The unique key of this table would be (Product_ID, Attribute).
So if Product_ID = 1 is a computer, you would have rows like:
1 Processor Xeon
1 RAM 4GB
1 HDD 1TB
And if Product_ID = 2 is shoes:
2 Material Leather
2 Size 6
2 Gender F
If you're worried about the space used for all those attribute strings, you can add a level of indirection to reduce it. Create another table Attributes that contains all the attribute names. Then use AttributeID in the Details table. This will slow down some queries because you'll need to do an additional join, but could save lots of space
Think about just having a single ProductDetails table like this:
ProductDetailID (PK)
ProductID (foreign key to your Products table)
DetailType
DetailValue
this way you do not have to create new columns every time you add a new product detail type. and you'll have many ProductDetail rows for each productid, which is fine and will query ok. Just be sure to put an index on ProductDetails.ProductID !
Since this is an application so you must be generating the queries. So lets generate it in 2 steps. I assume you can add a column product_type_id in your Product table that will tell you which child table to user. Next create another table Product_type which contains columns product_type_id and query. This query can be used as the base query for creating the final query e.g.
Product_type_id | Query
1 | SELECT COMPUTERS.* FROM COMPUTERS JOIN PRODUCT ON COMPUTERS.PRODUCT_ID = PRODUCT.PRODUCT_ID
2 | SELECT SHOES.* FROM SHOES JOIN PRODUCT ON COMPUTERS.PRODUCT_ID = PRODUCT.PRODUCT_ID
Based on the product_id entered by the user lookup this table to build the base query. Next append your where clause to the query returned.