I have multiple tables that I need to delete rows from and they are all contingent on data from one table. For instance, I have a Users Table and in the Users Table the Users have a UserId. I am trying to get rid of all the data that is in relation to that one user in all the other tables.
For instance, there is a Members Table and in the Members table, there is a Foreign Key relationship to the Users table so it's tied by the UserId. The Members table can have multiple members associated with the UserId (A User can be members to more than one thing so it's a one to many relationship). Then there is also a Permissions table and in the Permissions table it has a foreign key relationship to the Members table. And in that table the Members can have multiple permissions (A Member can have permissions to more than one thing is that's a one to many relationship).
What I am needing to do is delete all the rows in the Permissions table that are in relation to the all the MemberId's that are in relation to the UserId from the User's Table (i.e. - Bob has a UserId of 7 and in the Members Table he is a Member of 3 things so he has 3 MemberId's associated to his name and in the Permissions Table those 3 MemberIds also have 3 Permissions associated to those 3 MemberIds. I am needing to delete all 9 Permission rows according to those 3 MemberIds, then delete all the 3 Member rows according to the 1 UserId, and then delete the one UserId according to a UserName).
I've tried to Inner Join multiple tables and also connect them with Unions but I am having difficulty of tying all those tables down to that one UserId and carrying that data through the flow of logic.
User has a UserId of 7
User is is associated with 3 MemberId's
The MemberId is associated with multiple PermissionId's
So I need to delete all those PermissionId's (I did a WHERE statement just on the User's MemberId's), all 3 instances of the MemberId's, and the User.
If you are aware of UserID that you want to delete from different table, then this is easy to handle from table Users and Members. You need to use a Sub Query when you will delete records from Permissions table. But if there are relation (PK/FK) established between tables, you need to maintain some sequence as below-
Important Note: Delete is a risky operation unless you have proper Backup. So please try to execute scripts on test data first.
At first you have to delete records from Permission table with following script-
DELETE FROM Permissions
WHERE MemberID IN
(
SELECT MemberID FROM Members
WHERE UserID = 7
)
In second step, you have to delete records from Members table as below-
DELEET FROM Member WHERE UserID = 7
In third step, you have to delete users from Users table as below-
DELETE from Users WHERE UserID = 7
Related
I'm trying to pull the list of users (with user_id being the related variable) and list them so we can see which users satisfy the condition of each field/column.
There are two tables in concern:
Table_A (stores the location of the users)
Table_B (stores the characteristics of the users)
I want to join these two tables using the related variable user_id to pull where each of these users are also present in each columns/fields under the two tables.
Basically, I want the output to have a value of 1 if the user_id is present in the field (column) and 0 if the user_id is not present in the field (column).
How I want the data output to look
I tried concatenating the column outputs per user but it became very messy and the user_id values were duplicated - I want to show unique user_id in rows.
I am developing a database with TypeORM, and will have tables such as user, company and user_roles.
Each user can be part of multiples companies, and each user can have one user_role associated within that company. For example: user "A" is part of company "company_x" and has a role of admin. However, user "A" is also part of company "company_z" and has a role of member.
How can I achieve this using entities in TypeORM in nestJS framework in order to create my migrations? I was trying to do this while creating a pivot table with #ManyToMany() and #JoinTable(), but how can I relate three tables?
My desired output would be:
user_id
company_id
user_role_id
1
1
1
1
2
2
and I have three tables users, company and user_roles.
PLEASE HELP
Suppose in table A,there are two columns say Agent and policy_number where agent=1101 has 20 policies under it.In table B,that same agent has 21 policies under it.I need a solution where it will check if the agent has the number of policies in Table B.If the records matches it will insert into a new table otherwise the non-matched records will go into an error table.
I have three tables in my database: Contacts (master table), Users and Staff. The Contacts table has a primary key that is referenced as a foreign key from the Users and Staff tables.
Contacts
ContactID (Primary Key)
First Name
Last Name
Email
Users:
ContactID (Foreign Key)
Username
Password
Staff:
ContactID (Foreign Key)
Position
Comments
Pulling data from these tables is fairly simple with INNER JOIN and depends what data you need from the Users or Staff table. Updating and inserting new records is something that I'm not sure which way to approach.
Since the Contacts table is my master table and holds unique ID's but at the same time is shared between Users and Staff, I'm not sure about this situation.
Let's say I want to enter new user record. I need to enter First, Last name and email into the Contacts table, and User Name and Password into the Users table. At the same time I have to check if First, Last name and email already exist in the Contacts table, since maybe this Contact record has been previously entered for a Staff record.
I'm not sure how to prevent duplicates but at the same time there might be user or staff with the same name. Should I limit/filter by email and not let them enter the records if the email already exist in contacts table? Or is there a better way to handle this?
The whole point of having three tables is to prevent redundant data since User might be a Staff, but Staff might not be a User if that make sense. If anyone can help me pick the best approach please let me know. Thanks
You have to start by using a SELECT query and whatever business rules you want to test to decide if you will perform the INSERT at all, or UPDATE an existing record instead.
Then if you are going to INSERT, you have to INSERT into Contacts first, get the newly inserted ID, and then INSERT into the other two tables.
I have two table.
tbl_User and tbl_Follow for create relationship many to many
Like a picture
http://i.imgur.com/5JGsfSU.jpg
I don't set Action Delete or change Roles
When i delete one row in table user sql alert error conflict.
As per the attached image there is a foreignkey realtionship between the tables tbl_User and tbl_Follow through the column id (tbl_User) and userid (tbl_Follow). For every row in tbl_Follow there is will be an entry in tbl_User with same value in columns id and userid. But it is not necessary in vice versa.
I think you are deleting a row in tbl_Users which has an entry in tbl_Follow. First delete the row in tbl_Follow then proceed to tbl_Users.
Also you can confirm the foreignkey relationship through:
Type "tbl_Follow", select the table name and press "ALT+F1" you can view a set of results where refer the resultset with columns constraint_type,name etc. There shows the foreignkey defined for the table.