How to connect and relationship between these two tables? - mysql

I have two tables:
users (user_id, user_name, user_email, user_pass)
&
toys (name, box)
I need each user to have their own toys table. Basically the table for users is for my login form and the toys is where the user choose the toys once they login.

Add an extra table that have FK (foreign keys) to both Users and Toys, that acts as the table for the many-to-many relation
i.e. create a table called user_toys that has toy_id and user_id combination per row. Then if you want to get all the toy names for a particular user, you can just do
SELECT t.name
FROM Toys t,
User_toys relation,
WHERE relation.toy_id = t.toy_id
AND relation.user_id = :user_id
(Note, you don't need to join to the Users table if you already have the user_id in the relation table)
If each toy can only belong to one user (1-to-many), then adding an extra column for user_id FK on the Toys table would suffice. But sounds like from your question each user have their independent set of toys to pick.
reference: https://en.wikipedia.org/wiki/Associative_entity

hello you need one field in table Toy to make your table become relation, here is my schema based on yours
here is toys
+--------+-------------+--------------+-----+
| id_box | id_box_user | id name | box |
+--------+-------------+--------------+-----+
| 1 | 1 | Name toys 1 | box |
| 2 | 2 | Name toys 3 | box |
+--------+-------------+--------------+-----+
then this is user
+---------+-------------+------------+-----------+
| user_id | user_name | user_email | user_pass |
+---------+-------------+------------+-----------+
| 1 | user name 1 | email | pass |
| 2 | user name 2 | email | pass |
+---------+-------------+------------+-----------+
and you need query like this to get all data with spesific user
SELECT * FROM user a, toys b where a.user_id=b.id_box_user_id
but for spesific user use this
SELECT * FROM user a, toys b where a.user_id=b.id_box_user_id and a.user_id='variable which store session'
and this is the screen shot

Related

Having extra columns in join between 3 tables MySQL

I have 3 tables : Car, Client and Car_client the latter is a junction table between car and client, since as I said Car_Client is a junction table this means the relationship between car and client is many to many. Tables have this structure:
_____________________ ___________________ ______________________
| Car | | Client | | Car_Client |
|___________________| |_________________| |____________________|
| license_plate (PK)| | Name | | license_plate(fk) |
| Model | | Surname | | Id_Code(fk) |
|___________________| | Id_Code (PK) | |____________________|
| Phone |
|_________________|
Now, what I am trying to achieve is to allow the user to research the client by name. When the name is searched I want to show all the info about the car he owns and his personal info. So basically like a select * from Car, Client but just for a specific name.To achieve this I tried different methods, the one that works best, although not perfectly is this one :
SELECT * FROM
Car_Client car_cli
INNER JOIN Car ON ( car_cli.license_plate = car.license_plate)
INNER JOIN Client ON ( car_cli.Id_Code = Client.Id_Code)
WHERE
Car_Client.Id_Code in (SELECT Id_Code FROM Client WHERE Name = 'emanuele');
The problem with this solution is that I have two extra columns of license_plate and Id_Code
SELECT Client.*, Car.* FROM
Car_Client car_cli
INNER JOIN Car ON ( car_cli.license_plate = car.license_plate)
INNER JOIN Client ON ( car_cli.Id_Code = Client.Id_Code)
WHERE
Car_Client.Id_Code in (SELECT Id_Code FROM Client WHERE Name = 'emanuele');

MySQL link two tables together implicitly

Suppose we have two tables
A table called people with people linked to a bank account balances
| id | name | account_id |
--------------------------
| 1 | bob | 11 |
--------------------------
| 2 | sam | 22
A table called accounts with bank account balances
| id | value |
--------------
| 11 | 200 |
--------------
| 22 | 500 |
In order to link the two tables you can do
SELECT a.value as account_balance
FROM people p
WHERE p.name="bob"
LEFT JOIN accounts a ON p.account_id = a.id`
This would return
id => 1
name => bob
account_balance => 200
That's cool - but I am wondering if there is a more implicit way to do this via SQL linkage (foreign keys or otherwise). Can we in MySQL add links in some other way so that when we do a SELECT, it already knows to return value instead of **account_id **?
I'm asking this because I am creating a system where my users can create lookup tables and link them to other tables - but it must be do-able without any programming. The only other way I can think of is to set the name of account_id for example to accounts.value and treat that as a foreign key when doing a SELECT.
I would have to get the column structure and analyze and then determine that there is a foreign key and then return the appropriate foreign column by looking at the column name.

SQL Tables of the Same Column to Join

I currently have a problem as how to fetch data separately, in the same table, but of different conditions.
To better illustrate, take the example below as my tables.
disputes table
id | user_to | bidder_id
1 | 1 | 2
users table
user_id | user_name
1 | userone
2 | usertwo
I'd like to have an output that combines both like this:
final output table
id | user_to | bidder_id | user_to_name | bidder_id_name
1 | 1 | 2 | userone | usertwo
I do not know how to really put it into words but I hope the illustration helps :
It seeks for the "user_to" and "bidder_id" rows, associates them to the "user_id" in the users table, where it creates two new columns that associates the "user_id" and "bidder_id" to the respective ids in the users table and fetches the user_name in the id given in the field.
LEFT JOIN is your friend. see the exsample:
sample
SELECT d.*,
utn.user_name AS user_to_name ,
bin.user_name AS bidder_id_name
FROM disputes d
LEFT JOIN users utn on utn.user_id = d.user_to
LEFT JOIN users bin on bin.user_id = d.bidder_id;

How to keep staff and users in separate tables with another table that gives them a general accountID?

I have two tables, staff and users. Below are shortened versions of the tables.
(* = PK, # = FK)
staff { staffID*, username, ... }
users { userID*, username, ... }
I want to make it so that these tables have a table that links them together in a way that they are given an accountID.
I was thinking that this will be something along the lines of...
accounts { accountID*, accountType, localID# }
...where localID is either; the staffID if the account is from the staff table, or the userID if the account is from the users table. The accountType would be the used to tell which table the account is from and would have possible values of staff and user.
First off - is this possible to accomplish this way? Is this a good idea? Since both the staff and users tables have the field username, should I instead use username in the accounts table in place of accountType and localID? (If there exists a record in the staff table with the username 'foo', then there cannot be a record in the user table with the username 'foo')
Secondly - If the tables above are the way that I should implement this, what constraint would I need to add to make it so accountType & localID are unique such that all the examples in Figure 1 would be accepted, whereas the examples in Figure 2 that are duplicate combinations of accountType and localID would not?
Figure 1
|-----------|-------------|---------|
| accountID | accountType | localID |
|-----------|-------------|---------|
| 1 | A | 1 |
| 2 | B | 1 |
| 3 | A | 2 |
| 4 | B | 2 |
|-----------|-------------|---------|
Figure 2
|-----------|-------------|---------|
| accountID | accountType | localID |
|-----------|-------------|---------|
| 1 | A | 1 |
| 2 | A | 1 |
| 3 | B | 2 |
| 4 | B | 2 |
|-----------|-------------|---------|
Reasoning - I want these separate tables for staff and users to because I want role based access control, but I want it such that there are roles only a staff account can have & there are other roles that only a user account can have.
Thanks in advance for any help/guidance offered.
Add the accountId in staff and user table, that would accomplish what your looking for.

Database table that has many-to-many and one-to-many relationship

In the interest of learning more about database design im was drawing up a database model, i choose to draw a simple database model for a social network website to keep a little more interesting than your average student/teacher/class models.
The question i have is about the relationships between the different tables.
Im not that good drawing these text database drawings like other peoples has on stack exchange ill try to just list the tables and explain the relationships, if its unclear i can try to draw a text drawing.
Database tables:
User
Friend
Group
Newsfeed
User has a one-to-many relationship to Friend and Group based on that one user can have many friends and a user can be a member of several groups and a group can have many users.
Friend has a many-to-many relationship with Group based on that one friend can be a part of many groups and a group can contain many friends. There is a one-to-many relationship to Newsfeed based on that one friend can have many newsfeeds.
Group has a many-to-many relationship with Friend based on that one group can contain many friends and one friend can be part of many groups. Group has a one-to-many relationships with Newsletter based on that one group can have many newsfeeds.
So now there is one many-to-many relationship and a one-to-many relationship in one table point to two other tables, is this correct ? Some part of this feels wrong, especially the Friend part but maybe im just misunderstanding something here. This might be a stupid database model but i need to ask stupid questions sometimes in order to get smarter at something. Ive read about and watch some videos about database relationships and to be they seem easy but when drawing this database model im getting confused since i suddently end up with a many-to-many and a one-to-many relationship in one table which seems weird.
This is how I'd start:
Let's say we have two groups, Group A and Group B.
groups
id unsigned int(P)
name varchar(30)
...
+----+---------+-----+
| id | name | ... |
+----+---------+-----+
| 1 | Group A | ... |
| 2 | Group B | ... |
| .. | ....... | ... |
+----+---------+-----+
Let's say Group A has two newsfeeds and Group B doesn't have any:
newsfeeds
id unsigned int(P)
group_id unsigned int(F groups.id)
name varchar(30)
...
+----+----------+--------------------+-----+
| id | group_id | name | ... |
+----+----------+--------------------+-----+
| 1 | 1 | Interesting Things | ... |
| 2 | 1 | Other Information | ... |
| .. | ........ | .................. | ... |
+----+----------+--------------------+-----+
Let's say we have three users: Bob, Mary and John:
users
id unsigned int(P)
name varchar(30)
...
+----+------+-----+
| id | name | ... |
+----+------+-----+
| 1 | Bob | ... |
| 2 | Mary | ... |
| 3 | John | ... |
| .. | .... | ... |
+----+------+-----+
A "Friend" is really just another user so let's create a table that allows many-to-many relationships between two users. My sample data shows that Bob is friends with Mary and John while Mary is only friends with John. (user_id and friend_id form the Primary Key)
users_friends
user_id unsigned int \_ (P) (F users.id)
friend_id unsigned int / (F users.id)
+---------+-----------+
| user_id | friend_id |
+---------+-----------+
| 1 | 2 |
| 1 | 3 |
| 2 | 3 |
| ....... | ......... |
+---------+-----------+
Users can belong to many groups and each group can have many users so we need to have a table that gives us that many-to-many relationship. In my example data we see that Bob is a member of Group A and Group B while Mary and John are only members of Group B. (user_id and group_id form the Primary Key)
users_groups
user_id unsigned int \_ (P)(F users.id)
group_id unsigned int / (F groups.id)
+---------+----------+
| user_id | group_id |
+---------+----------+
| 1 | 1 |
| 1 | 2 |
| 2 | 2 |
| 3 | 2 |
| ....... | ........ |
+---------+----------+
Finally we need a table that shows the relationship between newsfeeds and users. I haven't entered any example data here but this table works exactly like the users_groups table. Tables like this are called many different things, you can read more about them at Wikipedia. (user_id and newsfeed_id form the Primary key)
users_newsfeeds
user_id unsigned int \_ (P) (F users.id)
newsfeed_id unsigned int / (F newsfeeds.id)
IMO, when thinking about relation modeling, one should remember about the 'direction' of the relation, otherwise it gets very confusing and also, one should remember every 'many to many' relation must have to be modeled using 'one to many'. Anyway, take a look here http://screencast.com/t/sJbPrvO53MS
even though it took a min to read the question...this was interesting problem...