ASGBD: add SQL samples

Signed-off-by: Nicolás A. Ortega Froysa <nicolas@ortegas.org>
This commit is contained in:
Nicolás A. Ortega Froysa 2023-02-10 19:37:15 +01:00
parent 5da929c96b
commit 78442e5fc1
6 changed files with 170 additions and 0 deletions

View File

@ -0,0 +1,14 @@
DECLARE
tbl_name USER_TABLES.TABLE_NAME%type;
CURSOR table_cursor IS SELECT TABLE_NAME FROM USER_TABLES;
BEGIN
OPEN table_cursor;
LOOP
FETCH table_cursor INTO tbl_name;
EXIT WHEN table_cursor%NOTFOUND;
dbms_output.put_line('ALTER TABLE ' || tbl_name || ' ADD fecha_creacion DATE;');
dbms_output.put_line('ALTER TABLE ' || tbl_name || ' ADD usuario_creacion VARCHAR2(32);');
END LOOP;
CLOSE table_cursor;
END;
/

View File

@ -0,0 +1,16 @@
DECLARE
office_id oficina.codigo_oficina%type;
cnt INT;
CURSOR my_cursor IS
SELECT codigo_oficina FROM oficina;
BEGIN
cnt := 0;
OPEN my_cursor;
LOOP
FETCH my_cursor INTO office_id;
EXIT WHEN my_cursor%NOTFOUND;
cnt := cnt + 1;
END LOOP;
dbms_output.put_line(cnt);
CLOSE my_cursor;
END;

View File

@ -0,0 +1,2 @@
alter session set "_ORACLE_SCRIPT"=true;
set serveroutput on;

View File

@ -0,0 +1,38 @@
alter session set "_ORACLE_SCRIPT"=true;
set serveroutput on;
CREATE TABLE PEDIDOS_COPIA AS (SELECT * FROM PEDIDOS);
DROP TRIGGER PEDIDO_LLEGA_TARDE;
CREATE OR REPLACE TRIGGER PEDIDO_LLEGA_TARDE
--AFTER
BEFORE
INSERT OR UPDATE OF FECHAESPERADA OR UPDATE OF FECHAENTREGA
ON PEDIDOS_COPIA
FOR EACH ROW
BEGIN
IF :NEW.FECHAESPERADA < :NEW.FECHAENTREGA
THEN
:NEW.COMENTARIOS := 'Pedido con retraso';
--UPDATE PEDIDOS_COPIA SET COMENTARIOS='Pedido con retraso' WHERE CODIGOPEDIDO=:NEW.CODIGOPEDIDO;
END IF;
END;
/
CREATE OR REPLACE TRIGGER PRODUCTO_SIN_STOCK
BEFORE
INSERT OR UPDATE OF CODIGOPRODUCTO OR UPDATE OF CANTIDAD
ON DETALLEPEDIDOS
FOR EACH ROW
DECLARE
STOCK PRODUCTOS.CANTIDADENSTOCK%type;
BEGIN
SELECT CANTIDADENSTOCK INTO STOCK FROM PRODUCTOS WHERE CODIGOPRODUCTO=:NEW.CODIGOPRODUCTO;
IF STOCK < :NEW.CANTIDAD
THEN
raise_application_error(-20669, 'Not enough of product in stock.');
END IF;
END;
/

View File

@ -0,0 +1,62 @@
/* Nicolás A. Ortega Froysa*/
ALTER SESSION SET "_ORACLE_SCRIPT" = TRUE;
CREATE TABLESPACE TBSP_COMERCIAL
DATAFILE 'C:\ABD\TS\tbsp_comercial.dbf'
SIZE 1M;
CREATE TABLESPACE TBSP_FABRICA
DATAFILE 'C:\ABD\TS\tbsp_fabrica.dbf'
SIZE 1M;
CREATE USER COMERCIAL IDENTIFIED BY COMERCIAL
DEFAULT TABLESPACE TBSP_COMERCIAL;
CREATE USER FABRICA IDENTIFIED BY FABRICA
DEFAULT TABLESPACE TBSP_FABRICA;
DROP TABLE COMERCIAL.clientes;
CREATE TABLE COMERCIAL.clientes(
codigo INT,
nombre VARCHAR(50),
fecha_registro DATE);
CREATE TABLESPACE TBSP_PEDIDOS
DATAFILE 'C:\ABD\TS\tbsp_pedidos.dbf'
SIZE 1M
AUTOEXTEND ON MAXSIZE 10M;
DROP TABLE COMERCIAL.pedidos;
CREATE TABLE COMERCIAL.pedidos(
num_pedido INT,
cliente_codigo INT,
articulo_codigo VARCHAR2(20),
precio_total NUMBER,
fecha_pedido DATE) TABLESPACE TBSP_PEDIDOS;
DROP TABLE FABRICA.articulos;
CREATE TABLE FABRICA.articulos(
codigo_articulo VARCHAR2(20),
fecha_registro_cliente DATE,
nombre VARCHAR2(20) NOT NULL,
precio NUMBER(2),
fecha_pedido DATE);
REVOKE ALL ON COMERCIAL.pedidos FROM PUBLIC;
GRANT READ ON COMERCIAL.pedidos TO COMERCIAL;
REVOKE ALL ON COMERCIAL.clientes FROM FABRICA;
GRANT READ,INSERT,UPDATE,DELETE ON FABRICA.articulos TO COMERCIAL
WITH GRANT OPTION;
CREATE USER JEFE_COMERCIAL
IDENTIFIED BY JEFE_COMERCIAL
DEFAULT TABLESPACE TBSP_COMERCIAL;
GRANT ALL ON COMERCIAL.pedidos TO JEFE_COMERCIAL
WITH GRANT OPTION;
GRANT ALL ON COMERCIAL.clientes TO JEFE_COMERCIAL
WITH GRANT OPTION;
SELECT c.nombre, SUM(p.precio_total)
FROM COMERCIAL.pedidos p
JOIN COMERCIAL.clientes c ON p.cliente_codigo=c.codigo
GROUP BY c.nombre;

View File

@ -0,0 +1,38 @@
CREATE OR REPLACE TRIGGER only_sales_comm
BEFORE INSERT OR UPDATE
ON EMP_COPIA
FOR EACH ROW
BEGIN
IF UPPER(:NEW.job) != 'SALESMAN' AND :NEW.comm IS NOT NULL
THEN
raise_application_error(-20669, 'Comm only allowed if SALESMAN');
END IF;
END;
/
CREATE TABLE EMP_COPIA AS (SELECT * FROM EMP);
SELECT * FROM EMP_COPIA;
UPDATE EMP_COPIA SET COMM=72 WHERE EMPNO=7782;
CREATE OR REPLACE TRIGGER sal_range
BEFORE INSERT OR UPDATE
ON EMP_COPIA
FOR EACH ROW
BEGIN
IF UPPER(:NEW.JOB) = 'CLERK' AND :NEW.SAL NOT BETWEEN 800 AND 1100
THEN
raise_application_error(-20669, 'Clerk salary must be between 800-1100');
ELSIF UPPER(:NEW.JOB) = 'ANALYST' AND :NEW.SAL NOT BETWEEN 1200 AND 1600
THEN
raise_application_error(-20669, 'Analyst salary must be between 1200-1600');
ELSIF UPPER(:NEW.JOB) = 'MANAGER' AND :NEW.SAL NOT BETWEEN 1800 AND 2000
THEN
raise_application_error(-20669, 'Manager salary must be between 1800-2000');
END IF;
END;
/
UPDATE EMP_COPIA SET SAL=5 WHERE EMPNO=7788;