A. Create EMP Table Using Data Definition Language (DDL) - (Hint: Include Constraints) (14 Marks)

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

a. Create EMP table using data definition language (DDL).

[Hint: include
constraints]    (14 marks)
-
CREATE TABLE dept (
dept_no varchar(50),
dept_name varchar(50),
manager_no varchar(50),
budget int,
PRIMARY KEY (dept_no)

-
CREATE TABLE emp (
emp_no varchar(50),
emp_name varchar(50),
dept_no varchar(50),
salary int,
marital_status varchar(1),
PRIMARY KEY (emp_no)

-
CREATE TABLE proj (
proj_no varchar(50),
dept_no varchar(50),
start_date varchar(50),
deadline varchar(50),
PRIMARY KEY (proj_no),
FOREIGN KEY (dept_no) REFERENCES dept(dept_no)

-
CREATE TABLE alloc (
emp_no varchar(50),
proj_no varchar(50),
FOREIGN KEY (emp_no) REFERENCES emp(emp_no),
FOREIGN KEY (proj_no) REFERENCES proj(proj_no)

-
ALTER TABLE emp
ADD FOREIGN KEY (dept_no) REFERENCES dept(dept_no)
b. Populate data into EMP table using data manipulation language (DML). [Hint:
use INSERT statement]    (10 marks)

- Inserting values
-
INSERT INTO dept VALUES
("D1","Production",NULL,1000000),
("D2","Sales","E5",250000),
("D3","Accounts","E9",95000),
("D4","Admin","E8",156000),
("D5","Personal", "E7",196000)

-
INSERT INTO emp VALUES
("E1","Smith","D1",9900,"W"),
("E2","Jones","D2",13200,"M"),
("E3","Roberts","D2",11000,"M"),
("E4","Evans","D3",16500,"S"),
("E5","Brown","D3",27500,"S"),
("E6","Green","D3",13200,"M"),
("E7","McDougal","D4",17600,"D"),
("E8","McNally","D5",12100,"M"),
("E9","Fletcher","D5",13200,"S")

-
INSERT INTO proj VALUES
("P1","D1","20-APR-94","23-FEB-98"),
("P2","D1","21-JAN-95","14-MAY-97"),
("P3","D2","02-FEB-96","03-MAR-99"),
("P4","D3","11-DEC-94","01-JAB-99"),
("P5","D4","08-OCT-95","31-DEC-99"),
("P6",NULL,"09-OCT-95","30-DEC-99")

-
INSERT INTO alloc VALUES
("E1","P1"),
("E1","P2"),
("E2","P1"),
("E2","P5"),
("E4","P4"),
("E5","P4"),
("E6","P4"),
("E9","P4"),
("E5","P3"),
("E7","P3")
c. Display a list of employee’s numbers    (5 marks)
-
SELECT emp_no FROM emp

d. Display details of employees with salaries in excess of RM 3,000.    (6 marks)


-
SELECT emp.emp_name , emp.salary FROM emp
WHERE salary > 3000

e. Find information on those employees and their departments who are working for
them but not their managers    (10 marks)
-
SELECT emp.emp_no, emp.emp_name,emp.salary,emp.marital_status,
dept.dept_name, dept.dept_name,dept.budget
FROM emp
INNER JOIN dept
ON emp.dept_no = dept.dept_no AND (dept.manager_no IS NULL)

f. Find  information  about  employees  who  are  currently  working  on  projects.
(10 arks)
-
SELECT emp.emp_no, emp.emp_name, emp.salary, emp.marital_status,
proj.proj_no FROM emp
INNER JOIN proj INNER JOIN alloc
ON
(emp.dept_no = proj.dept_no)
AND
(proj.proj_no = alloc.proj_no)
AND
(proj.dept_no IS NOT NULL)
GROUP BY emp.emp_name

g. Get information on all possible combinations of employees and departments.


(5 marks)
-

SELECT emp.emp_no, emp.emp_name, emp.salary, emp.marital_status , dept.dept_no,

dept.dept_name, dept.manager_no, dept.budget FROM emp

INNER JOIN dept

ON (dept.dept_no = emp.dept_no)

You might also like