Hello as the title says I need to relate the attribute Id_libro of nested table that is in the table Prestamo with an Id_libro attribute of the table Books in POO oracle, a kind of foreign key so that there are no errors that in a loan there is a book that does not correspond to the existing books in the book table, doing this in MR is simple but with Programming object oriented I do not know how to do it.
This is the image of the model that I'm trying to pass to SQL
In these two things is where I have the problem, since I do not know how to relate them.
--creo objeto con nombre pedidos_type
CREATE TYPE pedidos_type AS OBJECT(
id_libro INT, --<<<<<<<<<<<<<<<<<<<<<<<<<<<< DUDA COMO RELACIONO ESO CON EL IDLIBRO DE TABLA LIBRO
fecha_p DATE,
fecha_devolucion DATE,
fecha_devuelto DATE
)
CREATE TABLE libros(
id_libro INT NOT NULL,
datos datos_libros_type,
CONSTRAINT pk_libros PRIMARY KEY(id_libro)
);
SQL ORACLE CODE:
--/////////////////////////////SENTENCIAS PARA TABLA ESTUDIANTE//////////////////////////////////
--creo el varray telefonos_type de tamaño 5 y de tipo varchar2
CREATE TYPE telefonos_type AS VARRAY(5) OF VARCHAR2(20);
--creo un objeto llamado contacto_type
CREATE TYPE contacto_type AS OBJECT(
correo VARCHAR2(50),
telefono telefonos_type
)
--creo un objeto llamado direccion_type
CREATE TYPE direccion_type AS OBJECT(
ciudad VARCHAR2(20),
calle VARCHAR2(20),
provincia VARCHAR2(20),
cod_postal INT
)
--creo un objeto llamado nombres_type
CREATE TYPE nombres_type AS OBJECT(
nombre VARCHAR2(20),
apellido VARCHAR2(20),
genero CHAR(1)
)
--creo un objeto llamado estudiantes_type que contendra a los otros objetos creados anteriormente
CREATE TYPE estudiantes_type AS OBJECT(
nombres nombres_type,
direccion direccion_type,
contacto contacto_type
)
--FINALMENTE creo tabla estudiante
CREATE TABLE estudiantes(
id_lector NVARCHAR2(20) NOT NULL,
datos estudiantes_type,
CONSTRAINT pk_estudiantes PRIMARY KEY(id_lector)
);
--/////////////////////////////SENTENCIAS PARA TABLA AUTOR//////////////////////////////////
--creo un objeto llamado datos_nacimiento_type
CREATE TYPE datos_nacimiento_type AS OBJECT(
ciudad VARCHAR2(20),
pais VARCHAR2(20),
anio INT
)
--creo un objeto llamado autores_type que contendra a los otros objetos creados anteriormente
CREATE TYPE autores_type AS OBJECT(
nombres nombres_type, --uso el objeto creado en las /////sentencias de estudiantes///////
datos_nac datos_nacimiento_type
)
--FINALMENTE creo tabla autores
CREATE TABLE autores(
id_autor NVARCHAR2(20) NOT NULL,
datos autores_type,
CONSTRAINT pk_autores PRIMARY KEY(id_autor)
);
--/////////////////////////////SENTENCIAS PARA TABLA LIBRO//////////////////////////////////
CREATE TYPE datos_libros_type AS OBJECT(
titulo NVARCHAR2(50),
editorial NVARCHAR2(50),
area NVARCHAR2(50),
anio INT
)
CREATE TABLE libros(
id_libro INT NOT NULL,
datos datos_libros_type,
CONSTRAINT pk_libros PRIMARY KEY(id_libro)
);
--/////////////////////////////SENTENCIAS PARA TABLA LIBRO-AUTOR//////////////////////////////////
CREATE TABLE libro_autor(
id_libro INT NOT NULL,
id_autor NVARCHAR2(20) NOT NULL,
CONSTRAINT fk_libro_autor1 FOREIGN KEY(id_libro) REFERENCES libros(id_libro) ON DELETE CASCADE,
CONSTRAINT fk_libro_autor2 FOREIGN KEY(id_autor) REFERENCES autores(id_autor) ON DELETE CASCADE,
CONSTRAINT pk_libro_autor PRIMARY KEY(id_libro, id_autor)
);
--/////////////////////////////SENTENCIAS PARA TABLA PRESTAMO//////////////////////////////////
--creo objeto con nombre pedidos_type
CREATE TYPE pedidos_type AS OBJECT(
id_libro INT, --<<<<<<<<<<<<<<<<<<<<<<<<<<<< DUDA COMO RELACIONO ESO CON EL IDLIBRO DE TABLA LIBRO
fecha_p DATE,
fecha_devolucion DATE,
fecha_devuelto DATE
)
--creo coleccion de tablas llamado pedidos_table como tabla de tipo "pedidos_type" para despues anidarla.
CREATE TYPE pedidos_table AS TABLE OF pedidos_type;
--FINALMENTE creo tabla prestamos
CREATE TABLE prestamos(
id_lector NVARCHAR2(20) NOT NULL,
datos_pedido pedidos_table,
CONSTRAINT pk_prestamos PRIMARY KEY(id_lector),
CONSTRAINT fk_prestamos FOREIGN KEY(id_lector) REFERENCES estudiantes(id_lector) ON DELETE CASCADE
)
NESTED TABLE datos_pedido STORE AS datos_pedido_table_save;