SQL: delete parent when child does not exist - sql

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!!

Related

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

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;

How to update table using foreign key in sql

I am trying to update a table, but my query is not correct. I don't know where I am making a mistake. Here it is:
UPDATE employee
SET image = '123.jpg'
WHERE employee.emp_id=personal_data.emp_Id;
Where emp_Id is a primary key in personal_data table and a foreign key in employee table .
Try like this:
UPDATE E
SET E.image = '123.jpg'
FROM personal_data P
INNER JOIN employee E
ON E.emp_id = P.emp_id
You can use this query.
UPDATE employee
SET E.image = '123.jpg'
FROM employee,personal_data
where employee.emp_id = personal_data.emp_id

Using output of one select query as input for another

I have an employee table which includes ManagerID as a foreign key. To get the currently logged ManagerId on Employees, I use a statement like -
SELECT MgrId FROM Employees WHERE EmpId=#EmpId
#EmpId is currently Logged on Employees.
I need to get the currently logged on employees manager and the manager's manager in one statement. This involves using the output of the above statement as input of another select statement. I am unsure how to do this. Any help is much appreciated.
Assuming your Employee ID field is named Id
SELECT e.Id [EmployeeId], e.MgrId [ManagerId], m.MgrId [ManagerManagerId]
FROM Employees e
LEFT JOIN Employees m ON e.MgrId = m.Id
WHERE e.EmpId=#EmpId

How to change this db2 merge query?

can someone help me? i am trying to convert the following merge into another query and i am allowed only to use insert and update once:
MERGE INTO MYEMPLOYEE ME USING EMPLOYEE E ON ME.EMPNO = E.EMPNO
WHEN MATCHED THEN
UPDATE SET ME.SALARY = CASE WHEN ME.SALARY > E.SALARY THEN ME.SALARY ELSE E.SALARY END
WHEN NOT MATCHED THEN
INSERT VALUES(E.EMPNO, E.FIRSTNME, E.MIDINIT, E.LASTNAME, E.WORKDEPT, E.PHONENO, E.HIREDATE, E.JOB, E.EDLEVEL, E.SEX, E.BIRTHDATE, E.SALARY, E.BONUS, E.COMM);
How can I achieve this? the above merge copies the data if not exists, and if it exists it checks for the salary and selects the higher one and copies that one.
how can I achieve the same thing by only using one insert and one update? Can someone give me hints, please?
Thanks in advance :)
The purpose of the MERGE command is suppose to take into account multiple actions of UPDATE, INSERT, DELETE. MERGE statement explained
If you cannot/unable to use MERGE, then you have to resort to doing each request individually.
UPDATE MYEMPLOYEE ME
SET ME.SALARY = (
SELECT CASE WHEN ME.SALARY > E.SALARY THEN ME.SALARY ELSE E.SALARY END
FROM EMPLOYEE E
WHERE ME.EMPNO = E.EMPNO
)
WHERE EXISTS(
SELECT 1
FROM EMPLOYEE E
WHERE ME.EMPNO = E.EMPNO
);
Then do an insert where the employee don't exist in the master table.
INSERT INTO MYEMPLOYEE ME
SELECT *
FROM EMPLOYEE E
LEFT OUTER JOIN MYEMPLOEE ME ON E.EMPNO=ME.EMPNO
WHERE ME.EMPNO IS NULL;
If you need to do in one full sweep you can use the IMPORT command. But then you are dealing with files. You would need to export the EMPLOYEE table (probably with salary already formatted) and then import using the INSERT_REPLACE ability.
After trying I noticed that the INSERT code should actually be like this:
1) Don't use the Name ME twice
2) Select the necessary columns because after the join there are twice the column count
INSERT INTO MYEMPLOYEE (
SELECT E.EMPNO, E.FIRSTNME, E.MIDINIT, E.LASTNAME, E.WORKDEPT, E.PHONENO, E.HIREDATE, E.JOB, E.EDLEVEL, E.SEX, E.BIRTHDATE, E.SALARY, E.BONUS, E.COMM
FROM EMPLOYEE E LEFT OUTER JOIN MYEMPLOYEE ME ON E.EMPNO = ME.EMPNO WHERE ME.EMPNO IS NULL
);

does anyone know how to list the empid and the name of all supervisors if supervisors are in employee table too?

I have a table that contains empid, name, salary, hiredate, position and supervisor (which includes empid, not the name). How do I list the empid and name of all supervisors?
The output has to have to columns supervisor (and a list of their empid) and their names. This is the create statement used to create the Employee table:
/* Create table Employee */
IF OBJECT_ID('Employee', 'U') IS NOT NULL
DROP TABLE Employee
GO
CREATE TABLE Employee (
emp_id NCHAR(5),
name NVARCHAR(20),
position NVARCHAR(20),
hire_date DATETIME,
salary MONEY,
bcode NCHAR(3),
supervisor NCHAR(5)
)
I have tried a variety of statements using having statement and count but they don't seem to work.
select emp_id, name from employee where position='manager';
I tried this but it doesn't work. Anyone smart that knows how to do it?
You will have to join the table back on itself:
select a.name, a.position, a.hiredate, a.salary, a.supervisorid,
isnull(b.name, '') as SupervisorName
from EmployeeTable a
left join EmployeeTable b
on a.SupservisorID=b.ID
The left join will make sure that the employees who do not have supervisors are returned, and isnull(b.name, '<NONE>') can be used if you would like to have something other than NULL as a value in those cases.
SELECT e.empid ,ISNULL(b.name, 'No supervisor') SupervisorName
FROM employee e LEFT JOIN employee b
ON e.supervisorid = b.empid
Inner join will leave out the people who do not have a supervisor , Use left join to get all the employees
If you want supervisors only, you just need to select rows whose emp_id values are found in the supervisor column:
SELECT
SupervisorID = emp_id,
SupervisorName = name
FROM dbo.Employee
WHERE emp_id IN (SELECT supervisor FROM dbo.Employee)
;