Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to delete all the data of a UserName from all the table from database.
Actually I have UserName used in all the tables as FK.
Now i want to delete a specific User from my database.

What I have tried:

DELETE s.UserName, p.Name,p.Father_Name,s.Email,p.DOB,p.Gender,p.Nationality,p.Domicile,p.CNIC,p.Mobile,p.Address, e.SSC_OM,E.SSC_TM,E.SSC_EB,e.HSSC_OM,e.HSSC_TM,e.HSSC_EB, d.Choices_1,d.Choices_2,d.Choices_3
FROM Signup s
INNER JOIN Pers_D p ON p.UserName = s.UserName
INNER JOIN Edu_D e on e.UserName = p.UserName
INNER JOIN Dep_S d on d.UserName = e.UserName
WHERE [d].UserName LIKE '%Abuzar%'
Posted
Updated 24-Jul-16 23:34pm
v2
Comments
[no name] 21-Jul-16 7:09am    
One way would be to modify the foreign key constraints by including ON DELETE CASCADE
Stack Holder 21-Jul-16 7:11am    
what?
Stack Holder 21-Jul-16 7:15am    
in sql server 2012!
[no name] 21-Jul-16 7:18am    
Yes that is also possible in SQL Server 2012! :)

What 0x01AA meant was that when you establish the Foreign Key relationship between the Signup table UserName column and the Pers_D, Edu_D, and Dep_s Username entries you can add a Constraint ON DELETE CASCADE: SQL Server: Foreign Keys with cascade delete[^]
What happens then is that when you delete the row from the master table - Signup - the system will automatically remove all teh rows from dependant tables for you:
SQL
DELETE FROM Signup WHERE UserName = @UserName
Will do it automatically for you.

And don't use LIKE with DELETE! (Or even UPDATE - keep LIKE for SELECT where possible) it's very dangerous.
Suppose you want to DELETE user "old" and you say:
SQL
DELETE FROM Signup WHERE UserName LIKE '%old%'
The user "old" will go - but so will "bold", "oldsmobile" and ... "Stack Holder".
LIKE is dangerous - be very careful where you use it!

I would also suggest that using the UserName as a key field is a bad idea - use an ID value instead and it saves space, saves time, and allow the user to change his name without you having to update all related records.
 
Share this answer
 
v2
Comments
F-ES Sitecore 21-Jul-16 8:13am    
"it's very dangerous"

Not as dangerous as cascading deletes :)
OriginalGriff 21-Jul-16 8:25am    
Cascading deletes are dangerous, yes - but not half as bad as cascading deletes with LIKE! :laugh:
Stack Holder 21-Jul-16 9:33am    
with
DELETE * FROM Signup WHERE UserName = @Abuzar

error cames
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '*'.
OriginalGriff 21-Jul-16 9:48am    
Sorry, wrong database! Just remove the "*".
kaushik ahir 25-Jul-16 5:38am    
this is not select query it is Delete query so please follow synatax: delete from table name where unm=@unm
delete from Signup where UserName=@UserName
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900