0% found this document useful (0 votes)
32 views6 pages

Oracle

This document contains SQL statements for creating, modifying, and querying database tables, views, sequences, indexes, and users. It includes statements for selecting, inserting, updating, and deleting records from tables as well as creating views and sequences. Permissions are granted to a user and role to manage objects in the database.
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)
32 views6 pages

Oracle

This document contains SQL statements for creating, modifying, and querying database tables, views, sequences, indexes, and users. It includes statements for selecting, inserting, updating, and deleting records from tables as well as creating views and sequences. Permissions are granted to a user and role to manage objects in the database.
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/ 6

SELECT * FROM EMPLOYEES;

SELECT * FROM JOB_HISTORY;

SELECT employee_id, job_id


FROM employees
UNION
SELECT employee_id, job_id
FROM job_history;

SELECT employee_id, job_id, department_id


FROM employees
UNION ALL
SELECT employee_id, job_id, department_id
FROM job_history
ORDER BY employee_id;

SELECT employee_id, job_id


FROM employees
INTERSECT
SELECT employee_id, job_id
FROM job_history;

SELECT employee_id, job_id


FROM employees
MINUS
SELECT employee_id, job_id
FROM job_history;

SELECT employee_id
FROM employees
MINUS
SELECT employee_id
FROM job_history;

-- COINCIDENCIA DE LA SETNTENCIAS SELECT


SELECT location_id, department_name "Department",
TO_CHAR(NULL) "Warehouse location"
FROM departments
UNION
SELECT location_id, TO_CHAR(NULL) "Department",
state_province
FROM locations;

SELECT employee_id, job_id,salary


FROM employees
UNION
SELECT employee_id, job_id,0
FROM job_history;

SELECT * FROM DEPARTMENTS WHERE DEPARTMENT_ID = 280;

INSERT INTO departments(department_id,


department_name, manager_id, location_id)
VALUES (280, 'Informatica', 100, 1700);

INSERT INTO employees (employee_id,


first_name, last_name,
email, phone_number,
hire_date, job_id, salary,
commission_pct, manager_id,
department_id)
VALUES (500,
'Louis', 'Popp',
'[email protected]', '515.124.4567',
SYSDATE, 'AC_ACCOUNT', 6900,
NULL, 205, 110);

INSERT INTO employees


VALUES (600,
'Den', 'Raphealy',
'DRAPHEAL', '515.127.4561',
TO_DATE('FEB 3, 1999', 'MON DD, YYYY'),
'SA_REP', 11000, 0.2, 100, 60);

SELECT * FROM DEPARTMENTS;

INSERT INTO departments


(department_id, department_name, location_id)
VALUES (&department_id, '&department_name',&location);

-- PENDIENTE
INSERT INTO sales_reps(id, name, salary, commission_pct)
SELECT employee_id, last_name, salary, commission_pct
FROM employees
WHERE job_id LIKE '%REP%';

UPDATE employees
SET department_id = 50
WHERE employee_id = 113;

UPDATE employees
SET job_id = (SELECT job_id
FROM employees
WHERE employee_id = 205),
salary = (SELECT salary
FROM employees
WHERE employee_id = 205)
WHERE employee_id = 113;

DELETE FROM departments


WHERE department_name = 'Informatica';

DELETE FROM employees


WHERE department_id =
(SELECT department_id
FROM departments
WHERE department_name
LIKE '%Public%');

SELECT * FROM EMPLOYEES;

DELETE FROM employees


WHERE employee_id = 600;

INSERT INTO departments


VALUES (290, 'Corporate Tax', NULL, 1700);
ROLLBACK;
COMMIT;

-- BLOQUEAR REGISTROS PUEDE SER PARA HACER UN UPDATE


SELECT employee_id, salary, commission_pct, job_id
FROM employees
WHERE job_id = 'SA_REP'
FOR UPDATE
ORDER BY employee_id;

-- SENTENCIAS DDL PARA CREAR TABLAS

CREATE TABLE hire_dates


(id NUMBER(8),
hire_date DATE DEFAULT SYSDATE);

INSERT INTO sales_reps(id, name, salary, commission_pct)


SELECT employee_id, last_name, salary, commission_pct
FROM employees
WHERE job_id LIKE '%REP%';

DROP TABLE sales_reps;


CREATE TABLE sales_reps (id NUMBER(8),
name VARCHAR2(30),
salary NUMBER(9) DEFAULT 0,
commission_pct NUMBER(5,2) DEFAULT 0);

DESC sales_reps;

CREATE TABLE dept


(deptno NUMBER(2),
dname VARCHAR2(14),
loc VARCHAR2(13),
create_date DATE DEFAULT SYSDATE);

DESCRIBE dept;

--

CREATE TABLE employees_copia


( employee_id NUMBER(6)
CONSTRAINT emp_employee_id_1 PRIMARY KEY
, first_name VARCHAR2(20)
, last_name VARCHAR2(25)
CONSTRAINT emp_last_name_nn_1 NOT NULL
, email VARCHAR2(25)
CONSTRAINT emp_email_nn_1 NOT NULL
CONSTRAINT emp_email_uk_1 UNIQUE
, phone_number VARCHAR2(20)
, hire_date DATE
CONSTRAINT emp_hire_date_nn_1 NOT NULL
, job_id VARCHAR2(10)
CONSTRAINT emp_job_nn_1 NOT NULL
, salary NUMBER(8,2)
CONSTRAINT emp_salary_ck_1 CHECK (salary>0)
, commission_pct NUMBER(2,2)
, manager_id NUMBER(6)
CONSTRAINT emp_manager_fk_1 REFERENCES
employees (employee_id)
, department_id NUMBER(4)
CONSTRAINT emp_dept_fk_1 REFERENCES
departments (department_id));

DESC employees_copia;

--- crear tabla a partir de un select


CREATE TABLE dept80
AS
SELECT employee_id, last_name,
salary*12 ANNSAL,
hire_date
FROM employees
WHERE department_id = 80;

DESC dept80;

-- VISTAS

CREATE VIEW empvu80


AS SELECT employee_id, last_name, salary
FROM employees
WHERE department_id = 80;

DESC empvu80;

CREATE VIEW salvu50


AS SELECT employee_id ID_NUMBER, last_name NAME,
salary*12 ANN_SALARY
FROM employees
WHERE department_id = 50;

DESC salvu50;
SELECT * FROM salvu50;

-- Actualizar vistas
CREATE OR REPLACE VIEW empvu80
(id_number, name, sal, department_id)
AS SELECT employee_id, first_name || ' '
|| last_name, salary, department_id
FROM employees
WHERE department_id = 80;

SELECT * FROM empvu80;

-- vistas complejas

CREATE OR REPLACE VIEW dept_sum_vu


(name, minsal, maxsal, avgsal)
AS SELECT d.department_name, MIN(e.salary),
MAX(e.salary),AVG(e.salary)
FROM employees e JOIN departments d
ON (e.department_id = d.department_id)
GROUP BY d.department_name;
SELECT * FROM dept_sum_vu;

CREATE OR REPLACE VIEW empvu20


AS SELECT *
FROM employees
WHERE department_id = 20
WITH CHECK OPTION CONSTRAINT empvu20_ck ;

SELECT * FROM empvu20;

-- OBLIGA A Q SEA DE SOLO LECTURA


CREATE OR REPLACE VIEW empvu10
(employee_number, employee_name, job_title)
AS SELECT employee_id, last_name, job_id
FROM employees
WHERE department_id = 10
WITH READ ONLY ;

-- ELIMINACION DE VISTAS

DROP VIEW empvu80;

-- SECUENCIAS

CREATE SEQUENCE dept_deptid_seq


INCREMENT BY 10
START WITH 300
MAXVALUE 9999
NOCACHE
NOCYCLE;
--Recorrer la secuencia

SELECT dept_deptid_seq.CURRVAL
FROM dual;

SELECT dept_deptid_seq.NEXTVAL
FROM dual;

INSERT INTO departments(department_id,


department_name, location_id)
VALUES (dept_deptid_seq.NEXTVAL,
'Support', 2500);

SELECT * FROM departments;

-- MODIFICAR SECUENCIAS

ALTER SEQUENCE dept_deptid_seq


INCREMENT BY 20
MAXVALUE 999999
NOCACHE
NOCYCLE;

SELECT dept_deptid_seq.CURRVAL
FROM dual;

SELECT dept_deptid_seq.NEXTVAL
FROM dual;

-- BORRAR SECUENCIA
DROP SEQUENCE dept_deptid_seq;

-- INDICES
CREATE INDEX emp_last_name_idx
ON employees(last_name);

-- BORRAR INDICES
DROP INDEX emp_last_name_idx;

-- SINONIMOS

CREATE SYNONYM d_sum


FOR dept_sum_vu;

SELECT * FROM d_sum;


-- Borrar sinonimos
DROP SYNONYM d_sum;

CREATE USER ACASTILLO


IDENTIFIED BY admin;

GRANT create session, create table, create sequence, create view


TO ACASTILLO;

CREATE ROLE manager1;

GRANT create table, create view


TO manager1;

GRANT manager TO ACASTILLO;

You might also like