In my Users table I have a row called "products_in_sale" that contains the product ids of the products the user sells.
For one user it looks like this. "33" <-- therefore this very user sells only one product with the id 33.
Now, since users can sell more than one product, I have to join another number (preferably with a "," in between) to this field when a user creates a new product.
How does the necessary UPDATE statement have to look?
maybe 'UPDATE Users SET products_in_sale = products_in_sale + ", '.$id.'"'; ?
thanks for your help!
Use the CONCAT_WS function:
UPDATE Users SET products_in_sale = CONCAT_WS(',', products_in_sale, newProductId) where userId = ?
This query should work also for the first insert, if your products_in_sale column defaults to NULL.
Anyway, as suggested by juergen, using another table would be a better option.
Never never store multiple values in a single cell, this is a violation of first normal form (1NF). Read more: http://www.ntu.edu.sg/home/ehchua/programming/sql/Relational_Database_Design.html
Every value (e.g. product_id) should have its cell in a relational database table. In your case, there should be a table say "user_product" with at least 2 fields - "user_id" and "product_id", and both are composite primary key.
Of course, there should be a "user" table storing user details which would have a "user_id" field linking to the "user_id" field of the "user_product" table. Likewise, there should be a "product" table storing product details which would have a "product_id" field linking to the "product_id" field of the "user_product" table.
Related
So I want to make a webapp that needs to store the following.
"Users"
some "Events"
and some "Customers"
each "Event" has a "User" which is represented by a userId column.
But each "Event" can have multiple customers, to be able to store all this info,
I thought it's better to have a new table "Customers_In_Events" that has 3 columns:
id, -- Not sure if that's needed
e_id -- Event ID
c_id -- Customer ID
I have 2 questions...
Is there some better way to do this?
If I decide to go with the way I mentioned (an extra table), Would the "id" column be needed?
I am using MySQL by the way.
You can use a bridge model to represent your data, which is also normalized.
every event can have multiple customers
every customer can have multiple events
so the tabkes would look like
Events events_coustmers cutonuers
e_id(PK) e_id(fk) c_id(Pk)
e_name c_id(fk) c_name
PK(e_id,c_id)
I am trying to create a View using certain fields from a single MYSQL table. But in one of the fields in the Table, I need to populate the field from either one field in the table or another field in the same table based on the value of a third field in the table. Is this possible, and if so, how?
To explain, The table contains data from two suppliers. The entries in the table from "Supplier A" store the Account Number in field CustomerNumber while the entries in the table from "Supplier B" store the Account Number in field CustomerAcct. The view I need to generate needs to list all of the customer orders from both suppliers combined, and populate a single field in the view called AccountNumber with either the value from CustoomerNumber or CustomerAcct, depending on which supplier filled the order.
Is this possible?
You'd use the "case" function, as in mysql case
CASE
WHEN Supplier = 'A' THEN customernumber
ELSE customeraccount
END as accountnumber
I just was wondering if is it possible to associate an user to the data inserted in a table.
For example:
I have a table Customers with columns customerID, name and address. Is there any method in MySql to know what user has inserted each row in the table or must I add a column userID in the table Customer to add the user in my SQL Insert statement?.
If I have only one table, it is not a proble to add the userID column. But when i have multiple tables, maybe it becomes a messy task.
Thanx in advance.
If you are trying to find out the user_id caused the insert then NO, there is no other way than you storing it explicitly likewise you already have thought of.
If there is multiple tables for which you want to store the same information; then you can probably have a separate table where you can have the user_id column and can use a AFTER INSERT TRIGGER to insert the user id in this table.
No, no such functionality is provided by MySQL. You'll have to add a column for user_id in your table(s) and insert the user id yourself.
I am newly responsible for a database.
It contains the tables "users", "role" and "users_roles".
In "users_roles", "uid" (user id) from "users" and "rid" (role id) from "role" are mapped, as the former administrator told me.
WHAT I NEED TO DO:
I need to create a form by which users can register. Every newly registered user needs to appear in the table "users" of course and at the same time he must be attributed the role "7" and his "uid" must appear together with "rid" = 7 in the table "users_roles".
QUESTION:
I already know how to add the user to the "users" table. But how do I achieve, that his user id is mapped to the role id = 7 and that this entry appears in the table "users_roles"? Which SQL query do I need to write? $db->query(' ????????? ')
Regarding, "But how do I achieve, that his user id is mapped to the role id = 7 and that this entry appears in the table "users_roles"? "
There is always more than one way to do something. In this case, I would make 7 the default value of the role_id column in the users_roles table. That would make providing a role_id optional as you add new records.
I want to store user followers and following member list. Now in order to this, i am thinking to create two columns namely FOLLOWING and FOLLOWER in USER table to store comma separated values of following and followers respectively.
USER TABLE FIELDS:
userid
firstname
lastname
date_of_birth
following //in this we store multiple following_id as comma separated
follower //in this we store multiple follower_id as comma separated
Another way is to create tables namely FOLLOWER and FOLLOWING to store user's followers and following members id in it.
USER TABLE FIELDS:
userid
firstname
lastname
date_of_birth
and
FOLLOWER TABLE FIELDS:
userid
follower_id (also is an user)
and
FOLLOWING TABLE FIELDS:
userid
following_id (also is an user)
Since i am learning database designing, i don't have enough knowledge. So, here i am not getting proper idea of which way is proper? I have searched that using comma separated way is not a good idea but at the same time is it a good way to have multiple tables with NF ? Is there any drawback of using JOINS? Or is there any other effective way to deal with this scenario?
You need just two tables - one to list your users, and one to list who each user is following.
The resulting tables would be like your second proposal, except that the followers table is unnecessary because all of the required data is already in the following table - it's just keyed from the second column instead of the first. That table will need an index on both columns.
The following table whould have one row per relationship per direction. If the users are following each other, you would put two entries in the following table.
CREATE TABLE following (
userid ... NOT NULL,
following_id ... NOT NULL
);
CREATE INDEX idx_user ON following(userid);
CREATE INDEX idx_following on following(following_id);
CREATE UNIQUE INDEX idx_both ON following (userid, following_id); // prevents duplicates
To find the IDs that a particular user is following:
SELECT following_id FROM following WHERE userid = ?
or to find that user's followers:
SELECT userid FROM following WHERE following_id = ?
Use appropriate JOIN clauses if required to expand those queries to return the users' names.
None of the above. One row per follower. Normalize your data and using it will be easy. Make it an abstract mess like you're proposing and you're life will get tougher and tougher as your application grows.