Question

Example i having some values in Employee with following fields.

CREATE TABLE Department (
D#      NUMBER(5)   NOT NULL, /* Department number      */
DName       VARCHAR2(30)    NOT NULL, /* Department name        */
Manager#    CHAR(5)     NOT NULL, /* Department manager number  */
MSDate      DATE,             /* Manager start date         */
    total_staff_number NUMBER(3),
CONSTRAINT Department_PK PRIMARY KEY(D#),
CONSTRAINT Department_CK UNIQUE(DName)
);

CREATE TABLE Employee (
E#      CHAR(5)     NOT NULL, /* Employee number        */
Name        VARCHAR2(30)    NOT NULL, /* Employee name      */
    D#      NUMBER(5),        /* Department number      */
CONSTRAINT Employee_PK PRIMARY KEY(E#),
CONSTRAINT Employee_FK2 FOREIGN KEY (D#) REFERENCES Department (D#)
);

In my database. DEPARTMENT = 'SPORTS' = D# = 5, DEPARTMENT = 'GAMES' = D# = 3; Merging the DEPARTMENT='SPORTS' INTO DEPARTMENT='GAMES' , and manager will still remain the same, the employee.D#=5 will change to employee.D#=3

MERGE INTO EMPLOYEE TARGET
USING EMPLOYEE SOURCE WHERE D#=5 ON (
TARGET.D# = SOURCE.D#;
}
WHEN MATCHED THEN
UPDATE SET SOURCE.D#=3;
UPDATE DEPARTMENT.D# SET total_staff_number = total_staff_number - 1 where DEPARTMENT.D# = SOURCE.D#;
UPDATE DEPARTMENT.D# SET total_staff_number = total_staff_number - 1 where DEPARTMENT.D# = TARGET.D#;

but my logic seem like wrong. any solution?

Was it helpful?

Solution

Moving the employees from Sports to Games would be simply updating the departpent column to point to the new department. The question is what you are going to do with the dangling department sports afterwards?

For all employees working in Sports, change to Games.

update employee
   set d# = 3
 where d# = 5;

Count the nr of employees in department Games, and update the employee counter.

update department
   set total_staff_number = (
          select count(*)
            from employee
           where d# = 3
       )
 where d# = 3;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top