Ejercicios

Ejercicio1: Utilización de los diferentes componentes de Oracle en un ejemplo con tres tablas:
• INSTRUCCIÓN SQL PARA CREAR UN TABLESPACE
CREATE TABLESPACE "TBLSEPTIMO141"
DATAFILE 'D:\DATA\TBLSEPTIMO141.ORA' SIZE 10M
AUTOEXTEND ON NEXT 1024K
PERMANENT
ONLINE;
• PARA VER LAS TABLAS CREADAS
SELECT * FROM
DBA_TABLESPACES;
• PARA VER LAS FILAS CREADAS
SELECT * FROM
DBA_DATA_FILES;
• PARA ALTERAR UN NUEVO TABLESPACE
ALTER TABLESPACE TBLSEPTIMO141
ADD DATAFILE 'D:\DATA\TBLSEPTIMO1412.ORA' SIZE 10M AUTOEXTEND ON NEXT 1024K;
• INSTRUCCIÓN SQL PARA CREAR UN USARIO
CREATE USER CRPASTAZ
IDENTIFIED BY “0401608997” DEFAULT TABLESPACE TBLSEPTIMO141
ACCOUNT UNLOCK;
• CONEXION DEL USUARIO
GRANT CONNECT, RESOURCE TO CRPASTAZ;
EJERCICIO DE APLICACIÓN
• INSTRUCCIÓN CON CLAVE PRIMARIA EN ORACLE
Tabla Alumnos:
CREATE TABLE ALUMNOS (COD_ALUMNO NUMBER(5) CONSTRAINT PK_ALUMNOS PRIMARY KEY,
NOM_ALUMNO VARCHAR(50) NOT NULL,
DIR_ALUMNO VARCHAR(50) NOT NULL,
TEL_ALUMNO NUMBER(9) NOT NULL,
REP_ALUMNO VARCHAR(50) NOT NULL)
Tabla Materias:
CREATE TABLE MATERIAS (
COD_MATERIA NUMBER(5) NOT NULL,
NOM_MATERIA VARCHAR2(50) NOT NULL,
"PENSUM" NUMBER(10) NOT NULL,
"NUM_CREDITOS" NUMBER(10) NOT NULL,
CONSTRAINT "PK_MATERIAS" PRIMARY KEY("COD_MATERIA"))
Tabla Notas:
CREATE TABLE NOTAS (
"NOTA1" NUMBER(2) NOT NULL,
"NOTA2" NUMBER(2) NOT NULL,
"NOTA3" NUMBER(2) NOT NULL,
"COD_ALUMNO" NUMBER(5) NOT NULL constraint fk_alumno references alumnos,
"COD_MATERIA" NUMBER(5) NOT NULL constraint fk_materias references materias,
"COD_NOTAS" NUMBER(4) NOT NULL,
CONSTRAINT "PK_NOTAS" PRIMARY KEY("COD_NOTAS"))

• CODIFICACIÓN DE LAS SIGUIENTES SECUENCIAS:
Secuencias de alumnos
CREATE SEQUENCE SQ_ALUMNOS
START WITH 1
INCREMENT BY 1
Secuencias de materias
CREATE SEQUENCE SQ_materias
START WITH 1
INCREMENT BY 1
Secuencias de notas
CREATE SEQUENCE SQ_notas
START WITH 1
INCREMENT BY 1
• INSTRUCCIÓN
ALTER TABLE "CRPASTAZ"."NOTAS"
ADD (CONSTRAINT "FK_NOTASMATERIAS" FOREIGN KEY("COD_MATERIA")
REFERENCES "CRPASTAZ"."MATERIAS"("COD_MATERIA"))
• CODIFICACIÓN PARA INSERTAR DATOS:
Alumnos:
Insert into alumnos
values(null,'Cristian','Pastaz','2280762','Darwin');
Materias:
Insert into materias
Values(null,’programación’,’2’,’10’);


Notas:
Insert into notas
Values(‘8’,’5’,’10’,’1’,’1’,null);
Para mostrar o seleccionar las tablas:
select * from alumnos;
select * from materias;
selct * from notas;
• CÓDIGO PARA CAMBIAR DE USUARIO:
SQL> SHOW USER;
el usuario es "SYSTEM"
SQL> CONNECT CRPASTAZ/0401
Conectado.
SQL> SHOW USER
• CÓDIGO PARA CREAR UN DISPARADOR:
Tabla Alumnos:
CREATE OR REPLACE TRIGGER clave_primaria_alumno
Before insert on alumnos
For each row
declare
Valor number;
Begin
Select sq_alumnos.nextval
into valor from dual;
:new.cod_alumno:=valor;
End;
Tabla Materias:
CREATE OR REPLACE TRIGGER clave_primaria_materias
Before insert on materias
For each row
declare
Valor number;
Begin
Select sq_materias.nextval
into valor from dual;
:new.cod_materia:=valor;
End;
Tabla Notas:
CREATE OR REPLACE TRIGGER clave_primaria_notas
Before insert on notas
For each row
declare
Valor number;
Begin
Select sq_notas.nextval
into valor from dual;
:new.cod_nota:=valor;
End;
• CÓDIGO PARA CREAR UNA VISTA:
Create view Notas_por_materia_alumno
As
Select nom_alumno, nom_materia, Nota1, Nota2, Nota3,
Nota1+Nota2+Nota3 as Total, (Nota1+Nota2+Nota3)/3 as Promedio
From alumnos, materias, notas
Where alumnos.cod_alumno= notas.cod_alumno
And materias.cod_materia= notas.cod_materia;


Ejercicio 2: Utilización de los diferentes componentes de Oracle en un ejemplo con cinco tablas:

CREACIÓN DE UN TABLESPACE:

create tablespace "tblexamen142"

datafile 'd:\data\tblexamen142.ora'size 10m

autoextend on next 1024k

permanent

online;

CREACIÓN DE UN USUARIO:

CREATE USER damejia

IDENTIFIED BY ssss DEFAULT TABLESPACE TBLEXAMEN142

ACCOUNT UNLOCK;

grant connect, resource to SAPASTAZ;

MODELO IMPLEMENTAR:

Tabla Socios:

create table socios (num_socio number(5) constraint pk_socios primary key,

nom_socio varchar(50) not null,

dir_socio varchar(50) not null,

fecha_nac_socio varchar (50) not null,

sexo_socio varchar(50) not null);

Tabla Cuentas:

create table cuentas (num_cta number(5) constraint pk_cunetas primary key,

fecha_aprob varchar (50) not null,

fecha_entrega varchar (50) not null,

num_socio number(5) not null constraint fk_socio references socios);

Tabla Prestamos:

create table prestamos (num_prestamo number(5) constraint pk_prestamos primary key,

fecha_aprob varchar (50) not null,

fecha_entrega varchar (50) not null,

plazo varchar (10) not null,

monto_entregado number(20) not null,

saldo number(20) not null);

num_socio number(5) not null constraint fk_socio references socios);

Tabla pagos:

create table pagos (num_pago number(5) constraint pk_pagos primary key,

fech_pago varchar (50) not null,

monto_pago varchar (50) not null,

num_prestamo number(5) constraint fk_prestamo references prestamos);

Tabla Transacciones:

create table transacciones (num_transaccion number(5) constraint pk_transaccion primary key,

tipo_transaccion varchar(50) not null,

fecha_transaccion varchar (50) not null,

monto_transaccion number (50) not null,

num_cuenta number(5) constraint fk_cuenta references cuentas);

Codificación de las diferentes secuencias:

Secuencia de Socios:

create sequence sq_socios

start with 1

increment by 1

Secuencia de Cuentas:

create sequence sq_cuentas

start with 1

increment by 1

Secuencia de Prestamos:

create sequence sq_prestamos

start with 1

increment by 1

Secuencia de Pagos:

create sequence sq_pagos

start with 1

increment by 1

Secuencia de transacciones:

create sequence sq_transacciones

start with 1

increment by 1

CÓDIGO PARA CREAR UN DISPARADOR:

Tabla Socios:

create or replace trigger clave_primaria_socios

before insert on socios

for each row

declare

valor number;

begin

select sq_socios.nextval

into valor from dual;

:new.num_socio:=valor;

end;

Tabla Cuentas:

create or replace trigger clave_primaria_cuentas

before insert on cuentas

for each row

declare

valor number;

begin

select sq_cuentas.nextval

into valor from dual;

:new.num_cta:=valor;

end;

Tabla Transacciones:

create or replace trigger clave_primaria_transacciones

before insert on transacciones

for each row

declare

valor number;

begin

select sq_transacciones.nextval

into valor from dual;

:new.num_transaccion:=valor;

end;

Tabla Préstamos:

create or replace trigger clave_primaria_prestamos

before insert on prestamos

for each row

declare

valor number;

begin

select sq_prestamos.nextval

into valor from dual;

:new.num_prestamo:=valor;

end;

Tabla Pagos:

create or replace trigger clave_primaria_pagos

before insert on pagos

for each row

declare

valor number;

begin

select sq_pagos.nextval

into valor from dual;

:new.num_pago:=valor;

end;







CÓDIGO PARA INSERTAR DATOS:

Tabla Socios:

insert into socios

values(2,'juan','Ibarra','04 de julio de 1995','Masculino');

Tabla Cuentas:

insert into cuentas

values(01,'03 de junio del 2010','250','2');

Tabla Transacciones:

insert into transacciones

values(001,'diferido','07 de julio del 2010','500','01');

Tabla Préstamos:

insert into prestamos

values(03,'2 de julio del 2010','09 de julio del 2010','2','120','250');