Adding a multiple values to single column in mysql - 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.

Related

Database design for dynamic columns

I am trying to design a database in which users can set the cost of different products based on how much they sell them and their availability in their store. The users can only set their prices for the products and the availability but the products are created by the system admin for the users. I have tried by designing the database to have three tables one for the products and another for the users then the third is a modal for linking the users to the existing products using user_id and the product_id. What I am not able to do is to know how and where to set the price attribute of the products
As stated in the comment, using pivot tables then attaching all the additional data is the best option with mysql database in your case, you could have the users table with user_id as the primary key, products table with product_id as the primary key then create a modal table product_user with the fields of product_id and user_id as index, cost as decimal and availability or quantity.
During insertion or update, you will use attach as follows
$userProduct = Product::where(['id'=>$data['product_id']])->first();
$userProduct->userProducts()->attach(User::where('id', $data['user_id'])->first(), ['amount' =>$data['cost'],'availability' =>$data['quantity']]);
In your Product model, you will need to create the userProducts() function that will hande the one to many relationship

How to use a single table for multiple users

I want to create a database which has a table named warehouse which contains the details of each product. I have another table named users. At present there are 2 users in the users table - ABC and DEF.
My main question is user ABC has it's own warehouse and so for user DEF. How can I use the warehouse table for 2 or more different users ?
One to one
If one user own only one warehouse then you should add user_id column in warehouse table. then reference the user_id from user table via foreign key.
One to many
if multiple user owns an warehouse then you should add warehouse column in user table. then reference the warehouse from warehouse table via foreign key.
Many to Many
if an user owns multiple warehouse and and warehouse has multiple owner then you should add a table like warehouse_users and add keep warehouse and user_id.

Mysql creating 1 table to be used for multiple tables

I currently have made tables Person, branch, and clients. They all need to have data for where they are located.
My question is can I have 1 locations table for all three tables and locationid will auto_increment or should I create Location table for each one; person_location, branch_location, and client_location? Maybe I could add all the columns within each table Person, Branch, and Clients instead in a separate table.
no u dont need to create location table for each other tables.just make one table location..
but in other table add an column locationid which will hold the location id from location table.
thus u can get the location in each table.

MYSQL auto fill column values from another table

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

How to insert values of one table into another

I have two tables user and friends where users from user table can become friends.I have inserted 3 users into user table,now i want to make friends out of for example user_id = 1 and user_id = 2 but i don't know how to add this two id's in friends table.Photos of these two tables are attached.
... I don't really know what software you are using, but what you need is to declared a foreign key "UserId" column in your friend's table. Once that is done you can insert data in anyway possible by either using Mysql or a simple SQL insert:
INSERT INTO `friend`(UserId) VALUES (1)