SQL delete rows using full join - mysql

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

Related

Is it possible to have multiple many-to-many relationships between the same two tables?

I have a few tables that need to be joined together multiple times but I'm not sure if there can be multiple join tables for the same two tables. The tables i have are as follows:
User
Group
Record
Id PK
Id PK
Id PK
Name
Name
Name
etc
etc
Date
GroupID FK
//A record can only belong to 1 group
UserID FK
//A record can only have 1 creator/admin
I then want to have two join tables that can keep track of which group a user is a member of, and which member is an admin of the group.
Group Members
Group Admins
GroupID
GroupID
UserID
UserID
A Group can have many members while a user can be a member of many groups.
A Group can have many admins while a user can be an admin of many groups.
I get an error when trying to create the foreign keys for the "Group Members" and "Group Admins" tables.
SQL Query: "ALTER TABLE GroupMembers ADD CONSTRAINT GroupId FOREIGN KEY (GroupId) REFERENCES Group(Id);"
SQL Error: "Error creating foreign key on GroupId (check data types)"
Is this possible or is there a better way of doing this?
While updating the question, I found that there were restrictions being placed on the foreign keys by my workbench which was causing the error. The solution was to remove the "on delete" and "on update" restrictions.

Any simple way to set one primary key as a foreign key to multiple tables

I have one table
program(p_id(pk), program_name)
and three other tables:
graduate_survey(id(pk),PO1_avg,PO2_avg,program_name,session),
alumni_survey(id(pk),PO1_avg,PO2_avg,program_name,session),
faculty_survey(id(pk),PO1_avg,PO2_avg,program_name,session)...
I have to link the three tables with program table...How to link these tables in MySQL? graduate_survey, alumni_survey, faculty_survey are some forms which is calculated for some specific program...If in the forms graduate_survey, alumni_survey, faculty_survey there is no text box for entering the program_name, but if i made a column name 'program_name' in the database tables, can i enter the program_name by referencing to the program table? Is there will be any join query?
Use the program id as foreign key in the other tables, like so:
program(p_id(pk), program_name)
graduate_survey(id(pk),PO1_avg,PO2_avg,p_id(fk),session),
alumni_survey(id(pk),PO1_avg,PO2_avg,p_id(fk),session),
faculty_survey(id(pk),PO1_avg,PO2_avg,p_id(fk),session)
You don't really need the program name for the sake of the FK constraints but it's still a nice to have at some point, maybe.
This way you can easily join based on p_id, for instance:
"SELECT * FROM program INNER JOIN graduate_survey ON program.p_id=graduate_survey.p_id WHERE <your condition here>"
Hope this helps.

Foreign keys in mysql

I have a doubt about the way of relating some tables. I have these tables:
User table: username (primary key)
Team table: team_name(primary key), username (foreign key references User(username))
With this relationship, I get that an user can have more than one team.
Group table: group_name (primary key)
I want that a group can have many teams, but these teams have to be of different users, so two teams of a user cannot be in the same group.
I have thought to do a relationship with the three tables of this way:
Group_teams table: (group_name, username, team_name). This table would have a composite primary key (group_name and username), in this way I would be sure that an user cannot has more than one team in a same group.
In addition, I think that I should create a composite foreign key references User(username) and Team (team_name) to be able to control that the team of a user exists. Finally, I should create another foreign key references Group (group_name) to control that a group exists.
I'm not sure that it would be of this way because I have errors when I try to do it. Could yo help me and tell me your opinions?
If a user can be on (at most) only one team, then you have a 0/1 - many relationship.
The easiest approach is to have TeamId in the Users table. This would be a foreign key reference to Teams.
There is no need for a third table to represent this relationship.
There's no need to reproduce the username field in the Group_teams table, just have group_name & team_name.
Create a trigger on the Group_teams table to fire before a new row is inserted, checking for more other teams with the same username.
Have a look at the question "How do you check constraints from another table when entering a row into a table?", specifically this answer from Jim V describing such a setup.

pull db row with foreign key

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)

How would I add a foreign key in mysql for a column that has unrelated data?

I have two tables that need to be related--a users table and an address_book table
I want each address book entry to be associated with a user_id so that when I delete a user, their address book entries get deleted. But I also have about 10000 entries in the address book that are shared among all users. The user id field for each of those entries is set to 0. There is no user with an id of 0. MySQL won't let me create a foreign key for that field if there's a broken child/parent relationship there. How would I go about setting a foreign key in the address_book table for all users except with an id of 0?
have the user_id of the shared addresses be NULL instead of 0.