0% found this document useful (0 votes)
18 views2 pages

Practica Final (JPP)

The document contains SQL statements for creating and modifying database tables, views, sequences, and indexes. It selects data from tables and joins tables to retrieve related data. Key actions include adding primary keys and foreign keys, creating views, inserting records with a sequence, building an index, granting privileges, and performing inner and outer joins across multiple tables.

Uploaded by

Javier Pinales
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views2 pages

Practica Final (JPP)

The document contains SQL statements for creating and modifying database tables, views, sequences, and indexes. It selects data from tables and joins tables to retrieve related data. Key actions include adding primary keys and foreign keys, creating views, inserting records with a sequence, building an index, granting privileges, and performing inner and outer joins across multiple tables.

Uploaded by

Javier Pinales
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

1- select uc.

constraint_name,
uc.constraint_type,
cc.column_name,
cc.position
from user_constraints uc
inner join user_cons_columns cc
on uc.constraint_name = cc.constraint_name
where UC.table_name = 'JOB_HISTORY'
AND uc.constraint_type ! = 'C'

2- ALTER TABLE emp


ADD CONSTRAINT emp_employee_id_pk PRIMARY KEY (employee_id)

3- ALTER TABLE dept


ADD CONSTRAINT dept_department_id_pk PRIMARY KEY (department_id);

4- ALTER TABLE emp ADD CONSTRAINT emp_dept_department_id_fk


FOREIGN KEY (department_id) REFERENCES dept (department_id) ON DELETE CASCADE;

5- Cuente el número de filas en la tabla EMP.


SELECT COUNT(*) FROM emp;
Elimine el departamento 10 de la tabla dept.
DELETE FROM dept WHERE department_id = 10;
Ahora vuelva a contar los empleados. Debería haber menos empleados.
SELECT COUNT(*) FROM emp;

6- WITH avg_sal_by_dept AS (SELECT NVL(department_id, -1) dpt_id,


AVG(NVL(salary,0)) avg_sal FROM employees
GROUP BY NVL(department_id, -1))
SELECT emp.last_name, TO_CHAR(ROUND(emp.salary,2),'$999999.99')"salary",
CASE WHEN avgs.dpt_id = -1 THEN NULL ELSE avgs.dpt_id END
"department ID", TO_CHAR(ROUND(avgs.avg_sal,2),'$999999.99') "average salary"
FROM employees emp INNER JOIN (SELECT * FROM avg_sal_by_dept) avgs ON
NVL(emp.department_id, -1) = avgs.dpt_id
WHERE emp.salary > avgs.avg_sal;

7- CREATE OR REPLACE VIEW v2 ("highest salary", "lowest salary", "average


salary","Department Name") AS
SELECT
TO_CHAR(ROUND(MAX(NVL(emp.salary,0)),2),'$999999.99'),
TO_CHAR(ROUND(MIN(NVL(emp.salary,0)),2),'$999999.99'),
TO_CHAR(ROUND(AVG(NVL(emp.salary,0)),2),'$999999.99'), dpt.department_name
FROM departments dpt
LEFT OUTER JOIN employees emp ON dpt.department_id = emp.department_id
GROUP BY (dpt.department_id, dpt.department_name);

SELECT * FROM v2;

8- CREATE OR REPLACE VIEW dept_managers_view AS


SELECT DISTINCT SUBSTR(NVL(mgr.first_name, '_'),1, 1) ||
SUBSTR(mgr.last_name,1, 1) initials, mgr.last_name surname, dpt.department_name
FROM employees mgr INNER JOIN employees emp ON mgr.employee_id = emp.manager_id
LEFT OUTER JOIN departments dpt ON mgr.department_id = dpt.department_id;

SELECT * FROM Dept_Managers_view ;

9- drop view v3;

10- CREATE SEQUENCE ct_seq ;


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

11- INSERT INTO emp


(employee_id, first_name, last_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 emp(employee_id DESC, UPPER(SUBSTR(first_name,1,1)||' '||Last_name));

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

14- SELECT * FROM user_tab_privs WHERE table_name = 'EMP';


GRANT SELECT ON emp to PUBLIC;

15- SELECT em.employee_id, dp.department_name FROM employees em, departments dp;

16- SELECT em.employee_id, dp.department_name FROM employees em, departments dp


WHERE em.department_id = dp.department_id;

17- select e.last_name, d.department_name,e.salary, c.country_name


from hr.employees e
inner join hr.departments d on e.department_id = d.department_id
inner join hr.locations l on d.location_id = l.location_id
inner join hr.countries c on l.country_id = c.country_id;

18- SELECT em.last_name "last name", dp.department_name "department


name",em.salary, con.country_name "country name" FROM employees em, departments dp,
locations loc, countries con
WHERE em.department_id = dp.department_id(+)
AND
dp.location_id = loc.location_id(+)
AND
loc.country_id = con.country_id(+);

Javier Pinales Pérez


2021-1398

You might also like