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
Related
I'm using Oracle HR database.
I was wondering why the following query isn't working:
create table ecopy
as select *
from employees;
create table dcopy
as select *
from departments;
UPDATE (select d.location_id, e.salary
from ecopy e inner join dcopy d
on e.department_id=d.department_id)
set salary = salary+1
where location_id = 1800
SQL Error: ORA-01779: cannot modify a column which maps to a non key->preserved table
While the this one, on the origin tables is doing it's job:
UPDATE (select d.location_id, e.salary
from employees e inner join departments d
on e.department_id=d.department_id)
set salary = salary+1
where location_id = 1800
Could anyone explain it to me?
Here is the explanation:
In your real life your relation supported by keys - reference constraint
employee.department_id(MANY) = departments.department_id(ONE)
In the case of UPDATE with JOIN, you can update only columns in your "MANY" table and only if they have real reference.
Your Create as select. . . tables definitely don't have these references, hence Oracle optimizer throws this error.
Here are some references
Reference 1
Reference 2
I have 2 tables as following:
Tam trying to get the department name of each employee (DepName column in table Emp table) from Dep table:
I have written this query:
update Emp
set DepName= (
select DepName
from Dep
where array_to_string(EmpID, ',') like EmpID
);
It did not update the table Emp with the requested information, although I haven't got any error. Any help?
You can do:
update emp
set dept = d.depname
from dep
where emp.empid = any (dep.empid);
Having pointed that out, you should not do this. Instead, I would suggest that you have a proper link to the department table and use join to bring in the department name.
you have to convert id int to character varying array data type and then use contains operator with table dept and update as usual
UPDATE emp t1
SET dept = dname
from dept t2
where t2.eid #> concat(concat('{',(t1.id::text)),'}') ::varchar[]
https://dbfiddle.uk/?rdbms=postgres_9.6&fiddle=e5c24b26b3479b68adf0b17c2050f715
I have table Student with a column called IdStudent.
The value of IdStudent is 0
Table Student also have a column called UID
I need to update IdStudent in table Student with IdCandidate in table Candidate.
Table Candidate also have UID column containing the same UID of table Student.
So we can do this to have IdCandidate:
select C.IdCandidate from Candidate as C inner join Student as S
on C.UID = S.UID
How can I update IdStudent in table Student with this IdCandidate obtained in this select?
Thanks!
Use JOIN in update
update s set s.IdStudent = C.IdCandidate
from Candidate as C
inner join Student as S on C.UID = S.UID
You can do it using the following query :
update S set S.IdStudent = C.IdCandidate
from Student S
inner join Candidate C on S.UID=C.UID
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!!
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;