You are on page 1of 14

BASE DE DATOS: libreria.

CREANDO LAS TABLAS

1.Creando Tablas, ciudad, autor, asunto, tipo_cliente, tipo_doc.

create table ciudad


(id_ciudad INTEGER primary key,
ciudad VARCHAR (30) not null);

create table autor


(id_autor INTEGER primary key,
autor VARCHAR (30) not null);

create table asunto


(id_asunto INTEGER primary key,
autor VARCHAR (30) not null);

create table tipo_cliente


(id_tipo_cliente INTEGER primary key,
tipo_cliente VARCHAR (30) not null);

create table tipo_doc


(id_tipo_doc INTEGER primary key,
tipo_doc VARCHAR (30) not null);
2.Creando Tabla: editorial.

create table editorial


(id_editorial INTEGER not null,
editorial VARCHAR (20) not null,
cod_editorial VARCHAR(4) not null,
direccion VARCHAR(20) not null,
id_ciudad INTEGER not null,
email VARCHAR(20) not null,
gerente VARCHAR(20) not null,
CONSTRAINT editorial_pk primary key(id_editorial),
CONSTRAINT editorial_ciudad_fk FOREIGN KEY (id_ciudad) REFERENCES ciudad (id_ciudad));

3.Creando Tabla: libro.


create table libro
(id_libro INTEGER not null,
titulo VARCHAR (30) not null,
id_autor INTEGER not null,
id_editorial INTEGER not null,
año_publicacion INTEGER not null,
id_asunto INTEGER not null,
stock INTEGER not null,
precio INTEGER not null,
CONSTRAINT libro_pk primary key(id_libro),
CONSTRAINT libro_autor_fk FOREIGN KEY (id_autor) REFERENCES autor (id_autor),
CONSTRAINT libro_editorial_fk FOREIGN KEY (id_editorial) REFERENCES editorial (id_editorial),
CONSTRAINT libro_asunto_fk FOREIGN KEY (id_asunto) REFERENCES asunto (id_asunto));
4.Creando Tabla: clientes.

create table cliente


(id_cliente INTEGER not null,
id_tipo_cliente INTEGER not null,
cod_cliente VARCHAR(15),
id_tipo_doc INTEGER,
num_doc_cliente INTEGER,
nombre_cliente VARCHAR(20),
direccion VARCHAR(20),
id_ciudad INTEGER,
telefono INTEGER,
email VARCHAR(20),
CONSTRAINT cliente_pk primary key(id_cliente),
CONSTRAINT cliente_tipo_cliente_fk FOREIGN KEY (id_tipo_cliente) REFERENCES tipo_cliente
(id_tipo_cliente),
CONSTRAINT cliente_tipo_doc_fk FOREIGN KEY (id_tipo_doc) REFERENCES tipo_doc (id_tipo_doc),
CONSTRAINT cliente_ciudad_fk FOREIGN KEY (id_ciudad) REFERENCES ciudad (id_ciudad));
5.Creando Tabla: ventas.

create table ventas


(id_venta INTEGER not null,
no_factura VARCHAR(6) not null,
id_cliente INTEGER not null,
id_libro INTEGER not null,
precio INTEGER,
fecha_venta VARCHAR(10) not null,
CONSTRAINT ventas_pk primary key(id_venta),
CONSTRAINT venta_cliente_fk FOREIGN KEY (id_cliente) REFERENCES cliente (id_cliente),
CONSTRAINT venta_libro_fk FOREIGN KEY (id_libro) REFERENCES libro (id_libro));

6.Creando Tabla: compras.

create table compras


(id_compra INTEGER not null,
no_factura VARCHAR(6) not null,
id_editorial INTEGER not null,
detalle VARCHAR(50) not null,
precio INTEGER,
fecha_compra VARCHAR(10) not null,
CONSTRAINT compras_pk primary key(id_compra),
CONSTRAINT compra_editorial_fk FOREIGN KEY (id_editorial) REFERENCES editorial (id_editorial));
CARGANDO VALORES EN LAS TABLAS

insert INTO ASUNTO VALUES(1, 'Narrativa');


insert INTO ASUNTO VALUES(2, 'Fantasía Epopeya');
insert INTO ASUNTO VALUES(3, 'Libro Autoayuda');
insert INTO ASUNTO VALUES(4, 'Drama');
insert INTO ASUNTO VALUES(5, 'Novela Policiaca');
insert INTO AUTOR VALUES(1, 'Gabriel Garcia Maquez');
insert INTO AUTOR VALUES(2, 'Dante Alighieri');
insert INTO AUTOR VALUES(3, 'Miguel Ruiz');
insert INTO AUTOR VALUES(4, 'Jose Asunción Silva');
insert INTO AUTOR VALUES(5, 'Paulo Cohelo');
insert INTO CIUDAD VALUES(1, 'Bogota');
insert INTO CIUDAD VALUES(2, 'Medellin');
insert INTO CIUDAD VALUES(3, 'Cali');
insert INTO CIUDAD VALUES(4, 'Pamplona');
insert INTO TIPO_CLIENTE VALUES(1, 'Persona Natural');
insert INTO TIPO_CLIENTE VALUES(2, 'Persona Jurídica');

insert INTO TIPO_DOC VALUES(1, 'NIT');


insert INTO TIPO_DOC VALUES(2, 'Cedula');
insert INTO EDITORIAL VALUES(1, 'Planeta', 'E001', 'Cra 33 12 43', 1, 'planeta@planeta.com', 'Pablo
Riveros');
insert INTO EDITORIAL VALUES(2, 'Indigo', 'E002', 'Cl 12 76 45', 1, 'indigo@indigo.org', 'Jose Ruiz');
insert INTO EDITORIAL VALUES(3, 'Libro Verde', 'E003', 'Av 23 32 56', 2, 'liverde@lverde.com', 'Diana Perez');
insert INTO EDITORIAL VALUES(4, 'Edimat', 'E004', 'Cr 21 23 21', 2, 'edimat@edimat.com', 'Paola Duarte');
insert INTO EDITORIAL VALUES(5, 'Urano', 'E005', 'Cl 67 54 32', 3, 'urano@urano.com', 'Ricardo Santos');
insert INTO LIBRO VALUES(1, 'Cien Años de Soledad', 1, 1, 1964, 1, 20, 134000);
insert INTO LIBRO VALUES(2, 'La Divina Comedia', 2, 3, 1985, 1, 15, 20000);
insert INTO LIBRO VALUES(3, 'Los Cuatro Acuerdos', 3, 5, 1983, 1, 23, 15000);
insert INTO CLIENTE VALUES(1, 1, 'CDC2435565', 1, 2435565, 'Jose Santos', 'Cr 89 54 34', 1, 3145646556,
'jsantos@gmail.com');
insert INTO CLIENTE VALUES(2, 1, 'CDC2345675', 1, 2345675, 'Diana Quiroz', 'Cr 20 23 56', 1, 3145646556,
'dquiroz@gmail.com');
insert INTO CLIENTE VALUES(3, 2, 'CDC2345675', 2, 892003457, 'CAMARA DE COMERCIO', 'Cr 25 18A 49', 3,
3115459644, 'cca@cca.com');

insert INTO VENTAS VALUES(1, 'F23541', 1, 1, 134000, '15/05/2019');


insert INTO VENTAS VALUES(2, 'FV4562', 2, 1, 134000, '21/05/2019');
insert INTO VENTAS VALUES(3, 'FC3451', 3, 2, 20000, '22/05/2019');
insert INTO COMPRAS VALUES(1, 'F23541', 1, '50 Doña Barbara', 345000, '2/05/2019');
insert INTO COMPRAS VALUES(2, 'FV4567', 1, '20 Psicosis', 280000, '5/05/2019');
insert INTO COMPRAS VALUES(3, 'FV7654', 3, '30 Lencho Alfaro', 245500, '12/05/2019');
REALIZANDO CONSULTAS

1. Consulta Tablas: Editorial, Ciudad

SELECT a.editorial, a.cod_editorial, a.direccion, b.ciudad, a.gerente


FROM editorial a, ciudad b
where a.id_ciudad = b.id_ciudad;

2. Consulta Tablas: Libro, autor

SELECT a.titulo, b.autor, a.año_publicacion, a.stock, a.precio


FROM libro a, autor b
where a.id_autor = b.id_autor;
3. Consulta tablas: venta, cliente

SELECT a.no_factura, a.precio, b.nombre_cliente, a.fecha_venta


FROM ventas a, cliente b
where a.id_cliente = b.id_cliente;

4. Consulta tablas: Compras, editorial

SELECT a.no_factura, a.detalle, a.precio, b.editorial, a.fecha_compra


FROM compras a, editorial b
where a.id_editorial = b.id_editorial;

You might also like