0% found this document useful (0 votes)
59 views5 pages

Final BD

1. Tables emp and dept are created from the employees and departments tables. Primary keys are added to emp and dept. A foreign key constraint is added linking emp.department_id to dept.department_id. Views are created to show average salaries by department, department details including min/max/average salaries, and department managers. Sequences, indexes, grants and joins are also created.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views5 pages

Final BD

1. Tables emp and dept are created from the employees and departments tables. Primary keys are added to emp and dept. A foreign key constraint is added linking emp.department_id to dept.department_id. Views are created to show average salaries by department, department details including min/max/average salaries, and department managers. Sequences, indexes, grants and joins are also created.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

CREATE TABLE emp


AS select * FROM employees;

CREATE TABLE dept


AS SELECT * FROM departments;

select * from emp


select * from dept

2.SELECT c.constraint_name,
cc.column_name,
c.constraint_type,
cc.position
FROM user_constraints c JOIN user_cons_columns cc ON (c.table_name =
cc.table_name)
AND c.constraint_name = cc.constraint_name AND cc.position IS NOT NULL
AND c.table_name = 'JOB_HISTORY';

3.ALTER TABLE emp


ADD CONSTRAINT emp_employee_id_pk PRIMARY KEY (employee_id);
SELECT constraint_name,
constraint_type,
table_name,
status,
index_name
FROM user_constraints WHERE table_name = UPPER('emp') AND constraint_type = 'P';

4.ALTER TABLE dept


ADD CONSTRAINT dept_department_id_pk PRIMARY KEY (department_id);
SELECT constraint_name,
constraint_type,
table_name,
status,
index_name
FROM user_constraints WHERE table_name = UPPER('dept') AND constraint_type = 'P';
5.ALTER TABLE emp ADD CONSTRAINT emp_dept_department_id_fk FOREIGN KEY
(department_id)
REFERENCES dept (department_id)
ON DELETE CASCADE;

SELECT chld.table_name "Subject", chldcols.column_name "Subject Column Name",


chld.constraint_name "constraint_name in Subject",
chld.constraint_type "constraint_type in Subject", chld.delete_rule "delete_rule in Subject",
prnt.table_name "Parent Table",
prntcols.column_name "Parent table Column Name", prnt.constraint_name "Parent PK"
FROM user_constraints chld LEFT OUTER JOIN user_constraints prnt ON chld.r_constraint_name =
prnt.constraint_name
LEFT OUTER JOIN user_cons_columns chldcols ON chld.constraint_name =
chldcols.constraint_name
LEFT OUTER JOIN user_cons_columns prntcols ON prnt.constraint_name =
prntcols.constraint_name
WHERE chld.table_name = UPPER('emp') AND chld.constraint_type = 'R' AND
chldcols.column_name = UPPER('department_id') ;

VERIFICACION:
SELECT * FROM dept WHERE department_id = 20;

SELECT * FROM emp WHERE department_id = 20;

6.CREATE VIEW view_salarioMedio


AS SELECT department_id as depart_id,round(AVG(NVL(salary,0)),2) as Prom_sal
FROM hr.employees
GROUP BY department_id

VERIFICACION:
select * from view_salarioMedio

SELECT emp.last_name "LAST NAME"


,TO_CHAR(ROUND(emp.salary,2),'$999999.99') "SALARY"
,vsm.depart_id "DEPARTMENT NUMBER"
,TO_CHAR(ROUND(vsm.Prom_sal,2),'$999999.99') "SALAVG"
FROM hr.employees emp INNER JOIN (SELECT * FROM view_salarioMedio) vsm
ON (emp.department_id = vsm.depart_id)
WHERE emp.salary > vsm.Prom_sal;
7.CREATE VIEW V2
AS SELECT d.department_name "Nombre del Departamento"
,TO_CHAR(ROUND(MIN(NVL(e.salary,0)),2),'$999999.99') "Lowest Salary"
,TO_CHAR(ROUND(MAX(NVL(e.salary,0)),2),'$999999.99') "Highest Salary"
,TO_CHAR(ROUND(AVG(NVL(e.salary,0)),2),'$999999.99') "Average Salary"
FROM departments d INNER JOIN employees e ON (d.department_id = e.department_id)
GROUP BY (d.Department_name,d.department_id);

VERIFICACION:
SELECT * FROM V2

8.CREATE VIEW Dept_Managers_view


AS SELECT dpt.department_name DEPT_NAME
,SUBSTR(NVL(emp.first_name, '_'),1, 1) || '.' || emp.last_name MGR_NAME
FROM employees emp INNER JOIN departments dpt ON (emp.department_id =
dpt.department_id )

SELECT * FROM Dept_Managers_view ;

UPDATE Dept_Managers_view
SET DEPT_NAME = 'Sales'
WHERE DEPT_NAME = 'Soles';

9.CREATE VIEW V3 AS SELECT * FROM departments --


DROP VIEW V3

10.CREATE SEQUENCE ct_seq;

SELECT ct_seq.NEXTVAL as Siguiente


FROM user_sequences WHERE sequence_name = UPPER('ct_seq');

11.NSERT INTO emp(employee_id,


first_name,
email,
phone_number,
hire_date,
job_id,
salary,
commission_pct,
manager_id,
department_id)
VALUES (ct_seq.NEXTVAL, 'Kaare', 'Hansen', 'KHANSEN', '44965 832123',sysdate, 'SA_REP',
6500, null, 100, 20);

12.CREATE INDEX emp_indx


ON employees UPPER(employee_id DESC,SUBSTR(first_name,1.1) || ' ' || last_name);

SELECT DISTINCT ic.table_name


,ic.index_name
,idc.index_type
,ic.column_name
,ic.column_position
FROM user_indexes idc, user_ind_columns ic
WHERE idc.table_name = ic.table_name
AND ic.table_name = 'EMPLOYEES'

13.SELECT * FROM all_tables WHERE REGEXP_LIKE(table_name, '(PRIV)');

14.GRANT SELECT ON emp TO PUBLIC;

SELECT *
FROM user_tab_privs
WHERE table_name = 'EMP';

15.SELECT emp.employee_id
,dpt.department_name
FROM employees emp CROSS JOIN departments dpt

16.SELECT e.employee_id
,d.department_name
FROM employees e RIGHT OUTER JOIN departments d
ON (e.department_id = d.department_id)
WHERE employee_id IS NOT NULL;

17.SELECT e.last_name
,d.department_name
,e.salary,c.country_name
FROM employees e LEFT OUTER JOIN departments d
ON(e.department_id = d.department_id)
LEFT OUTER JOIN locations l ON(d.location_id =l.location_id)
LEFT OUTER JOIN countries c ON(l.country_id = c.country_id);

You might also like