Lets say I have 2 tables in my db
user:
id, username
userpost:
userid, post
I have defined a Relation to connect userpost.userid to user.id in side the db (mysql).
Is there a query (as simple as possible) to pull the whole row (including user's row)
WITHOUT knowing the relationship at the programmer side? meaning, relaing on the relationship defined in the db itself
something like "SELECT * from userpost include foreign key"
SELECT user.*, userpost.*
FROM user, userpost
WHERE user.id=userpost.userid
Possible problem would be the 1:many relationship between user and userpost: You would get each user-record multiple times (once for each userpost record)
Related
I have created 3 tables: accounts, products, claims. These are the relationships:
Accounts: PK - username
Products: PK - serial number, FK - username
Claims: FK - username, FK - serial number
My issue is that a user can add a claim even for products the user has not purchased, as long as the user knows that serial number, while I should allow the user to add a claim only for products the user purchased. For inserting claims I am using this query:
INSERT INTO claims (username, serial_no, date, issue) VALUES (%s, %s, %s, %s)
Do I need to change the table relationship with keys to fix the above, or use a more specific query? Better, what is the logic behind how this is supposed to work?
You should filter the products by username of the current user to avoid claiming products that are not related to the user. If you filter the product to be claimed then you won't need to change your table structure, it's only up to you on how you'll implement the filter in the programming language you are using.
First select all the products related to the user.
SELECT * FROM products WHERE username=#currentusername;
Then in your program, just allow the user to claim only from the fetched products.
Currently I have a Users table and a User model. The Users table looks like this:
Table: Users
Columns: id, username, password, email
I assume if I want to implement friendship between users, I'd need to create a Friend model and Friends table which would contain the ids of the 2 users that have become friends
Table: Friends
Columns: id, userId, userId
Where both userId would be a foreign keys from the Users table. I'm using Sequelize ORM but I'm not sure what the relationship between the User and Friend models should be. I'm thinking many-to-many relationship but I'm a bit confused how to do it since both foreign keys are from the same table, therefore I need only 2 tables instead of 3.
I have a database with 15 tables that are connected between each other by different relations.
The main table is the login table:
login_id
user_name
user_pass
area_id
The area_id means that each area have it's own user and password so each action made, we can figure out in which area it happened.
Now instead of connecting all 15 tables into the login table with 1 to many relation and area_id is the foreign key, can I save the area_id in a session (because the backend language is PHP) and add it in each table without the relation.
So now, the user_info table contains:
name
address
phone
mother_name
contact
alternative_contact
area_id
But here this table is not connected directly to login table but it contains a similar field area_id and which it's value is added from session, and so we can now create a query by saying:
SELECT something FROM login JOIN user_info WHERE user_info.area_id = login.area_id
Does this makes data consistent or should I connect the login table with all other tables ?
I currently have 2 tables in my database, one called User, and the other Product, where each row in User stores information about a user, and each row in Product stores a product that one user has:
User (Username, password, phone_num, address, email)
Product (Username, item_name, type, taste, price, image)
User has primary key Username, and Product has primary keys Username and item_name, and Username in Product is also a foreign key that refers to Username in User.
I'm trying to use a join query that, when executes, deletes both a user and all the products that are related to this particular user. Right now I have two separate working queries in PHP:
"DELETE from User where Username='$username'";
"DELETE from Product where Username='$username'";
Is there a way that I can combine these two queries using join to achieve the same goal? Thanks.
No, you cannot delete from two tables simultaneously in one command. Here is a link to the documentation: http://dev.mysql.com/doc/refman/5.7/en/delete.html
What you could do is set up a cascading delete through your foreign key constraint. When you delete a User if there is a foreign key reference in the Product table that row will automatically get deleted too. You'll achieve the your objective of deleting both with a single command but you'll do it not with a join but a constraint. Here's the constraint documentation: http://dev.mysql.com/doc/refman/5.7/en/create-table-foreign-keys.html
I had to implement the following into my database:
The activities that users engage in. Each activity can have a name with up to 80 characters, and only distinct activities should be stored. That is, if two different users like “Swimming”, then the activity “Swimming” should only be stored once as a string.
Which activities each individual user engages in. Note that a user can have more than one hobby!
So I have to implement tables for this purpose and I must also make any modifications to existing tables if and as required and implement any keys and foreign key relationships needed.
All this must be stored with minimal amount of storage, i.e., you must choose the appropriate data types from the MySQL manual. You may assume that new activities will be added frequently, that activities will almost never be removed, and that the total number of distinct activities may reach 100,000.
So I already have a 'User' table with 'user_id' as my primary key.
MY SOLUTION TO THIS:
Create a table called 'Activities' and have 'activity_id' as PK (mediumint(5) ) and 'activity' as storing hobbies (varchar(80)) then I can create another table called 'Link' and use the 'user_id' FK from user table and the 'activity_id' FK from the 'Activities' table to show user with the activities that they like to do.
Is my approach to this question right? Is there another way I can do this to make it more efficient?
How would I show if one user pursues more than one activity in the foreign key table 'Link'?
Your idea is the correct, and only(?) way.. it's called a many to many relationship.
Just to reiterate what you're proposing is that you'll have a user table, and this will have a userid, then an activity table with an activityid.
To form the relationship you'll have a 3rd table, which for performance sake doesn't require a primary key however you should index both columns (userid and activityid)
In your logic when someone enters an activity name, pull all records from the activity table, check whether entered value exists, if not add to table and get back the new activityid and then add an entry to the user_activity table linking the activityid to the userid.
If it already exists just add an entry linking that activity id to the userid.
So your approach is right, the final question just indicates you should google for 'many to many' relationships for some more info if needed.