sqlite trigger on delete update tuple with value from another table? - sql

I have two tables:
Customer (id, name, advisorID) and
Employee (id, name, managerID)
When an employee is deleted from table Employee, how to make trigger that update Customer advisorID to the value of the deleted employees managerID?
So far I have:
CREATE TRIGGER updateAdvisor BEFORE DELETE ON Employee
BEGIN
UPDATE Customer set Customer.advisorID = Employee.managerID
from Employee
WHERE Employee.managerID IN(
SELECT managerID
FROM Employee)
Thanks for helping!!

The UPDATE statement works on a single table; you have to look up the new value with a correlated subquery:
UPDATE Customer
SET advisorID = (SELECT managerID
FROM Employee
WHERE Employee = OLD.EmployeeID)
...
Furthermore, you want to update only those customers that actually have the to-be-deleted employee as advisor:
...
WHERE advisorID = OLD.EmployeeID;

This worked great!
CREATE TRIGGER updateAdvisor BEFORE DELETE ON employee
BEGIN
UPDATE customer
SET advisorID = (SELECT managerID FROM employee WHERE employeeID = old.employeeID)
WHERE advisorID = old.employeeID;
END;

Related

How to write a sql update query for below condition?

I have 2 tables. First is employee table and has (id,emp_num,name,address_id,text) columns.
Second is address table with (adress_id,emp_num,adress,state) columns.
Now when I am inserting/updating a new record into employee table, I also want to update TEXT column for all other entries for that emp_num who are from the same state.
EMPLOYEE table:
enter image description here
Address table:
enter image description here
I am trying to do a update like below:
update employee set id_text='ABCABC' where id = 'ID1';
some other update query
after that employee table should look like:
enter image description here
(note: text field is updated for ID1 and ID4)
Thanks
To me, it looks as if you need a row-level trigger which fires whenever you insert or update a row in employee table; using :new.address_id, fetch state from address table and put it into employee's text column:
create or replace trigger trg_biu_emp
before insert or update on employee
for each row
begin
select a.state
into :new.text
from address a
where a.address_id = :new.address_id;
end;
/
UPDATE employee e
SET e.text = (SELECT a.state FROM address a WHERE a.address_id = e.address_id)
WHERE e.emp_num IN (SELECT a.emp_num FROM address a WHERE a.state = (SELECT a.state FROM address a WHERE a.address_id = e.address_id));

SQL: delete parent when child does not exist

I have two tables:
tblEmployee
EmployeeID (PK), Name, Age, Department
tblDesignation
EmployeeID(FK), Designation, DesignationID
I need help to write a stored procedure to delete an employee record only if it does not have a record in the designation table. (delete employeeID only if there is no designation assigned)
I am not sure if I should use the ON DELETE RESTRICT constraint or NOT EXISTS clause.
Well you can have below code for your purpose. Please excuse with syntax
create procedure usp_deleteemployee
as
begin
delete from tblEmployee
where id not in (select empid from tblDesignation);
end
Or you can try with exist also. Not sure about syntax
DELETE tblEmployee
FROM tblEmployee
WHERE NOT EXISTS (SELECT 1
FROM tblDesignation
WHERE tblEmployee.Id = tblDesignation.empid)
You could try something like below.
DELETE
E
FROM
tblEmployee E
LEFT JOIN tblDesignation D
ON E.EmployeeID = D.EmployeeID
WHERE
D.EmployeeID IS NULL
Hope this Helps!!

Trigger Update another table After Insert

I have 2 tables Employee and Employee_Backup
Employee has 3 columns
IDEmployee
Name
Status
and Employee_Backup also has 3 columns:
IDEmployee
Detail
Status
For every row inserted into or updated in table Employee, I want to set the Status in table Employee_Backup using this criteria
WHERE employee_backup.IDEmployee = employee.IDEmployee (inserted / updated)
Something like that??
CREATE TRIGGER tr_Employee_Insert
ON dbo.Employee
FOR INSERT
AS
UPDATE b
SET Status = 'Inserted'
FROM dbo.Employee_Backup b
INNER JOIN Inserted i ON b.EmployeeID = i.EmployeeID
CREATE TRIGGER tr_Employee_Update
ON dbo.Employee
FOR UPDATE
AS
UPDATE b
SET Status = 'Updated'
FROM dbo.Employee_Backup b
INNER JOIN Inserted i ON b.EmployeeID = i.EmployeeID
You basically need to join the Inserted pseudo table which contains all rows that have been inserted (or updated) from the base table (dbo.Employee) and the Employee_Backup table - and then use that result set from the JOIN as the basis for your UPDATE statement.
Note: this will NOT insert any new rows into Employee_Backup when you add new rows to dbo.Employee - is that what you want? If not, you'd have to change the FOR INSERT trigger a bit ....

SQL update in single query

I have 2 tables: employee and employeedetails.
employee looks like this:
id name
----------------
1 Suresh
2 Ram
3 Ravi
employeedetails looks like this:
empid salary
----------------
1 10000
2 5000
3 40000
I want to update salary field of a particular person. For this i know the employee name of the first table. Based on this, I need to write a single query that will update the salary field with single query. How can I do this?
For example, if I have to update Ravi's salary details, how can I do it in a single query?
update employeedetails
inner join employee on employeedetails.empid = employee.id
set salary = 1000
where employee.name = 'Ram'
Try this :
update employeedetails set salary=1000000 where empid in (select id from employee where name='suresh');
MERGE INTO employeedetails
USING employee
ON employeedetails.empid = employee.id
AND employee.name = 'Ravi'
WHEN MATCHED THEN
UPDATE
SET salary = 10000;
UPDATE employeedetails det
SET salary=100
WHERE EXISTS ( SELECT NULL
FROM employee emp
WHERE name='Ravi'
AND det.empid = emp.id
)
;

same table, 1 field to 2 field query

I have 2 tables: 1st holds employees (of ones in any position) and the 2nd holds manager employee relations with id numbers.
I want to write a query like
1st field: name(employee),
2nd field: name(manager)
How can I do that?
No nested queries required, just use standard joins:
select e.*, m.*
from
employee e
left join employee_managers em
on e.id = em.emp_id
left join employee m
on m.id = em.man_id
Each row will contain all fields of employee (possibly several rows for one employee if it has several associated managers) and all fields of his corresponding manager (or NULLs if employee has no manager).
You can do that with one table:
Employee
--------
EmployeeId int
Name varchar(50)
ManagerId int
ManagerId points to the manager's entry in the same table. The CEO will have a ManagerId of null. An example table definition:
create table Employees (
EmployeeId int auto_increment primary key
, Name varchar(50)
, ManagerId int
, foreign key (ManagerId) references Employees(EmployeeId)
);
With some example data:
insert into Employees (Name) select 'The Chief';
insert into Employees (Name, ManagerId) select 'Grunt 1',
(select EmployeeId from Employees where Name = 'The Chief');
insert into Employees (Name, ManagerId) select 'Grunt 2',
(select EmployeeId from Employees where Name = 'The Chief');
insert into Employees (Name, ManagerId) select 'Secretary',
(select EmployeeId from Employees where Name = 'The Chief');
To find the name of the second Grunt's manager, you could query like:
select mgr.Name
from Employees mgr
inner join Employees grunt
on grunt.managerid = mgr.employeeid
where grunt.name = 'Grunt 2';