Question
Hello, below are my SQL statements for Oracle. The bold statements are the ones that I am getting errors on. Then when I do the
Hello, below are my SQL statements for Oracle. The bold statements are the ones that I am getting errors on. Then when I do the UPDATES they compile but still give an error If someone could look over it and correct them I would appreciate it.
--Drop Table if it already exists DROP TABLE Department;
-- Create Department relation schema CREATE TABLE Department ( name VARCHAR(50) PRIMARY KEY, managerID INT );
-- Create Employee relation schema CREATE TABLE Employee ( ID INT PRIMARY KEY, name VARCHAR(50), deptName VARCHAR(50), salary DECIMAL(10,2), CONSTRAINT ck_salary CHECK ((deptName = 'HR' AND salary >= 20000) OR (deptName <> 'HR')) );
--Update Department so table can be created first ALTER TABLE Department ADD CONSTRAINT fk_managerID FOREIGN KEY (managerID) REFERENCES Employee(ID) ON DELETE SET NULL;
--Update Employee so table can be created first ALTER TABLE Employee ADD CONSTRAINT fk_deptName FOREIGN KEY (deptName) REFERENCES Department(name) ON DELETE SET NULL;
"SQL command not properly ended"
-- Insert data into Department INSERT INTO Department(name, managerID) VALUES ('HR', NULL), ('RD', 666), ('SALES', 222), ('888', NULL);
"SQL command not properly ended"
-- Insert data into Employee INSERT INTO Employee (ID, name, deptName, salary) VALUES (123, 'John', 'RD', 56000), (578, 'Robert', 'HR', 37500), (46000, 'Jenny', 'RD', 39000), (39000, 'Christ', 'HR', 50000), (50000, 'Bill', 'SALES', 67500), (101, 'Susan', 'RD', NULL);
"Missing keyword"
-- Add ON UPDATE CASCADE constraint to Department relation schema ALTER TABLE Department ADD CONSTRAINT fk_deptName_employee FOREIGN KEY (name) REFERENCES Employee(deptName) ON UPDATE CASCADE;
-- Update managerID of Department to NULL when a manager is deleted CREATE OR REPLACE TRIGGER trg_manager_delete BEFORE DELETE ON Employee FOR EACH ROW BEGIN IF OLD.ID = (SELECT managerID FROM Department WHERE managerID = OLD.ID) THEN UPDATE Department SET managerID = NULL WHERE managerID = OLD.ID; END IF; END; /
-- Set deptName of Employee to NULL when a department is deleted CREATE OR REPLACE TRIGGER trg_dept_delete BEFORE DELETE ON Department FOR EACH ROW BEGIN UPDATE Employee SET deptName = NULL WHERE deptName = OLD.name; END; /
-- Update deptName of Employee when a department name is changed CREATE OR REPLACE TRIGGER trg_dept_update AFTER UPDATE ON Department FOR EACH ROW BEGIN UPDATE Employee SET deptName = NEW.name WHERE deptName = OLD.name; END; /
SELECT * FROM Department; SELECT * FROM Employee;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started