Question: I am using Oracle SQL Developer , and I am running into issues with this code- specifically Integrity Constraint errors. create table Employee(empID NUMBER, empFname
I am using Oracle SQL Developer, and I am running into issues with this code- specifically Integrity Constraint errors.
create table Employee(empID NUMBER, empFname VARCHAR2(20), empLname VARCHAR2(20), deptID NUMBER, PRIMARY KEY (empID) ); create table Department(deptID NUMBER, deptName VARCHAR2(20), chairID NUMBER, PRIMARY KEY (deptID)); ALTER TABLE Employee ADD FOREIGN KEY (deptID) REFERENCES Department(deptID);
ALTER TABLE Department ADD FOREIGN KEY (chairID) REFERENCES Employee(empID);
INSERT INTO DEPARTMENT (deptID ,deptName,chairID ) VALUES (401,'COMPUTER SCIENCE',301); INSERT INTO DEPARTMENT (deptID ,deptName,chairID ) VALUES (402,'ELECTRONICS',302); INSERT INTO DEPARTMENT (deptID ,deptName,chairID ) VALUES (403,'MATHEMATICS',303);
INSERT INTO EMPLOYEE (empID ,empFname ,empLname,deptID) VALUES (1001,'Alen','Zer',301); INSERT INTO EMPLOYEE (empID ,empFname ,empLname,deptID) VALUES (1002,'Beny','Ker',301); INSERT INTO EMPLOYEE (empID ,empFname ,empLname,deptID) VALUES (1003,'Clen','Ler',302); INSERT INTO EMPLOYEE (empID ,empFname ,empLname,deptID) VALUES (1004,'Dlen','Mer',302); INSERT INTO EMPLOYEE (empID ,empFname ,empLname,deptID) VALUES (1005,'Elen','Ner',303); INSERT INTO EMPLOYEE (empID ,empFname ,empLname,deptID) VALUES (1006,'Flen','Oer',303);
CREATE OR REPLACE TRIGGER chairID_after_update BEFORE UPDATE ON Department FOR EACH ROW
DECLARE v_username varchar2(10); v_count number; v_sql varchar2(50); BEGIN
select count(*) into v_count from Department where chairID = :new.chairID; IF v_count = 0 then v_sql := 'alter table set chairID ='+ :new.chairID; execute immediate v_sql; end if;
END; /
The original question is-
Create two tables: Employee: empID (PK), empFname, empLname, deptID(FK) and Department: deptID(PK), deptName, chairID
chairID is empID from Employee table
Insert at least 3 rows in the Department table and at least 6 rows in the Employee table.
Create trigger on update of chairID that enforces the following business rules
One employee can chair no more than one department.
Each department has exactly one chair. Please help me solve the integrity constraint errors with this code, so it will work properly. Make sure it works before posting.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
