I'm trying to delete a user and all the related records tied to him, and I have no clue how to use the SQL INNER JOIN statement, is there any way to do something in the style of:
DELETE * FROM tblUsers, tblEnrollment, tblLinkActivities, tblFullSchedule, tblSchedule, tblLinkMedical
WHERE [IDUser] = ?
(I know that's completely incorrect)
My relationships chart looks like so:
Would it be easier to use 6 delete commands? Or is there another command that does that? Thanks a bunch..
Since you already have defined relationships with referential integrity, simply set the Cascade Delete Related Records option for each relationship.
See https://support.office.com/en-us/article/create-edit-or-delete-a-relationship-dfa453a7-0b6d-4c34-a128-fdebc7e686af#__bmcascade
This way you only need to delete from tblUsers, all related records are deleted automatically.
If you can't or don't want to do this, you need to run separate delete queries on the related tables before deleting the main record.
There's no way to delete records in multiple tables at the same time in single sql query. You need to write multiple delete statements. The better way is to write an inner query with all tables involved and delete in each table.
For ex:
delete from dept where DEPTNO IN (Select a.DEPTNO from emp a , dept b where a.DEPTNO=b.DEPTNO and a.DEPTNO=10)
delete from emp where DEPTNO IN (Select a.DEPTNO from emp a , dept b where a.DEPTNO=b.DEPTNO
Related
I have table with millions of record and I added two new columns
alter table hr.employees add (ind char(1Byte), remove Char(1 Byte)); commit;
I have another view hr.department which has more data than this and it has these two columns .
So if I write and update for these records it takes so long.
update hr.employees a
set (ind, remove) =(select ind, remove
from hr.department b
where a.dept_id = b.dept_id ) ;
It's been an hour it still goes with the update. Can some one help in this ?
If your table has millions of rows, using an UPDATE will very likely take too much time.
I would rename the old table, create a new table with the new columns already filled, then add indexes, constraints, comments and gather statistics.
RENAME employees to employees_old;
CREATE TABLE employees AS
SELECT a.col1, a.col2, ... a.coln, b.ind, b.remove
FROM employees_old a
LEFT JOIN department b
ON a.dept_id = b.dept_id;
The simplest way to handle this update will likely be the following:
Find a time when no one else will be critically using the system.
Make a backup.
If they don't already exist, copy DDL for creating all indexes/constraints on that table.
Drop all indexes/constraints on that table.
Update your two columns.
Recreate indexes/constraints (this may take some time, but usually orders of magnitude less than updating every row in a big table with multiple indexes).
As usual, Burleson Consulting is a good resource: http://www.dba-oracle.com/t_efficient_update_sql_dml_tips.htm
I created a query that will delete rows from three tables that has 'Employee'.
When I execute it, it only deletes rows from one table tblEmployeeType. I tried adding Alias of other tables beside DELETE but SQL does not support it. Is there any alternative way of deleting rows from multiple tables? or I forgot some codes on my query or should I just separate delete queries? Thank you.
DELETE a
FROM tblEmployeeType a INNER JOIN
tbl_Selected_AccessType b
ON a.EmpTypeName = b.UserType INNER JOIN
tbl_AccessType_AllFunction c
ON a.EmpTypeName = c.UserType
WHERE a.EmpTypeName = 'Employee'`
INSERT and UPDATE statements can only directly affect one table at a time. If you have foreign keys configured with ON DELETE CASCADE then child records will be deleted along with the parent record. Regardless of using cascade, you should have foreign keys on the table so that your DELETE doesn't leave orphaned child records with broken referential integrity.
Another way to achieve affecting other tables in an INSERT or UPDATE is by using a trigger on the table. This can be desirable when you want to do checks before blindly deleting child records.
I have made a fiddle here: http://sqlfiddle.com/#!9/a67558/1
Due to external constraints, the IDs (EID and DID) were not specified with a PK. I feel like I am close but SQL is throwing syntax errors, problem may be with the program?
The goal is to remove the row where EID = 100 from tableJoin after the record is deleted from the employee table using a trigger.
Edit per Andrew's comment:
I have tried:
CREATE TRIGGER DeOrphan
AFTER DELETE ON employee
FOR EACH ROW DELETE FROM joinTable.* FROM employee
RIGHT JOIN tableJoin ON employee.eid=tableJoin.eid
WHERE employee.eid IS NULL;
DELETE FROM employee
WHERE employee.eid = 100;
The record is succesfully removed from the employee table but persists in the tableJoin.
Try this
DELETE FROM joinTable WHERE EID NOT IN (SELECT eid FROM employee)
This would be a separate call and would clean up ALL orphans, but if you trigger it regularly it should be a fairly quick process.
There's no need for a NOT IN/NOT EXISTS, within a trigger you got access to the deleted row:
CREATE TRIGGER DeOrphan
AFTER DELETE ON employee
FOR EACH ROW
DELETE FROM joinTable WHERE `eid` = old.`eid`;
See fiddle
I'm trying to delete all the rows in tblProInfo,tblOrderAA where tblProInfo.proInfoScienceName='shalosh'
Here is the command I wrote. I get a message that it couldn't generate my query.
DELETE tblOrderAA.*, tblProInfo.*
FROM tblProInfo INNER JOIN tblOrderAA ON tblProInfo.proInfoSerialNum = tblOrderAA.orderAASerialPro
WHERE (((tblProInfo.proInfoScienceName)='shalosh'
DELETE a1, a2
FROM tblProInfo as a1 INNER JOIN tblOrderAA as a2
ON a1.proInfoSerialNum = a2.orderAASerialPro
WHERE a1.proInfoScienceName='shalosh'
The syntax is correct as per msaccess but doesn't delete records. Access delete queries cannot delete from more than one table at a time. This is an Access limitation.
Refer here to know about deleting records from multiple tables in MS-Access
If both tables are connected and referential integrity set such that deletes are cascaded then you can cause records to be deleted from both tables at the same time by simply deleting the record on the One side of the link.
In Sql Server:
The concept of deleting rows from multiple tables in a single delete statement cannot be done in sql server. There is the concept of triggers on the tables that do deletes in a cascading style.
On this line you have 3 opening brackets and 1 closing bracket:
WHERE (((tblProInfo.proInfoScienceName)='shalosh'
You have to separate the statements. You can not delete from two tables with one query
DELETE FROM tblOrderAA
WHERE tblOrderAA.orderAASerialPro IN
(
SELECT orderAASerialPro
FROM tblProInfo WHERE tblProInfo
tblProInfo.proInfoScienceName='shalosh'
)
DELETE FROM orderAASerialPro
WHERE tblProInfo.proInfoScienceName='shalosh'
i'm trying to write a delete statement using sql, based off of certain criteria. i am having trouble with it. here is the criteria.
the program should prompt the user for a ssn and delete that employee from the Employee table (the records from the Technician table and from the Expert table that reference that employee will also be deleted as a result of the ON DELETE CASCADE action)
DELETE FROM Employees WHERE SSN='508-12-1234'
Assuming the Technician and Expert tables have foreign keys set up properly, this should be all you need to do.