You are on page 1of 2

create database DBempresa;

use DBempresa;

create table cliente(


cod_cli varchar(4) not null,
nmb_cli varchar(40),
loc_cli varchar(20) default 'lima',
tlf_cli varchar(9),
estado char,
primary key (cod_cli)
);

create table articulos(


cod_art varchar(4) not null,
des_art varchar(30),
pre_uni INT,
stock INT,
almacen varchar(3),
primary key (cod_art)
);

create table ventas(


id int auto_increment,
cod_cli varchar(4),
boleta varchar(4) unique key,
fecha date,
total int,

primary key (id),


foreign key (cod_cli) references cliente(cod_cli)
);

create table detalle(


boleta varchar(4),
cod_art varchar(4),
cantidad int,
foreign key (boleta) references ventas(boleta),
foreign key (cod_art) references articulos(cod_art),
primary key (boleta,cod_art)
);

insert into cliente (cod_cli, nmb_cli) values ('c001' , 'Jorge García');

insert into cliente values ('c002' , 'Carlos Perez', 'Ica', '963157689','1');

insert into cliente values


('c003' , 'Ines Tello', 'Puno', '989564112','1'),
('c004' , 'Jorge Diaz', 'Tacna', '946548988','1'),
('c005' , 'Maria Cardenas', 'Tacna', '987367689','1');

insert into ventas (cod_cli,boleta,fecha , total) values


('c001','1001','2019-10-04','28'),
('c002','1002','2019-10-05','34'),
('c004','1005','2019-10-04','40'),
('c005','1010','2019-10-03','28'),
('c002','1012','2019-11-02','13');

insert into articulos values


('a001' , 'Lapiz' , '2' , '50' , 'L01'),
('a002' , 'Regla' , '1' , '0' , 'L01'),
('a003' , 'Cuaderno' , '5' , '80' , 'L02'),
('a004' , 'Lapicero' , '3' , '60' , 'L01'),
('a005' , 'Tinta Roja' , '2' , '30' , 'L01'),
('a006' , 'Tinta Negra' , '2' , '30' , 'L01');

insert into detalle (boleta, cod_art,cantidad) values


('1001','a001',45),
('1002', 'a001', 30),
('1005', 'a005', 19),
('1010', 'a006', 22),
('1012', 'a003', 77);

#consultas locas
#1 total de lciente que reside en ica
select SUM(total) as 'total monto' from ventas where
cod_cli in (select cod_cli from cliente where loc_cli = 'ica');

#2 cliente sin compra alguna


select cod_cli,nmb_cli,loc_cli,tlf_cli, estado from cliente left join
ventas using (cod_cli) where ventas.cod_cli is null;

#3 total menor
select * from ventas order by total asc limit 1;

#4 prodcuto mayor de la tabla detallle


select a.cod_art,des_art, cantidad from articulos a inner join
detalle d on a.cod_art = d.cod_art order by d.cantidad desc limit 1;

#5
select v.cod_cli,v.boleta, d.cod_art,a.des_art from detalle d
inner join ventas v on d.boleta= v.boleta
inner join articulos a on d.cod_art = a.cod_art
where v.cod_cli in ('c001','c002')
and d.cod_art in (select cod_art from detalle group by cod_art having count(*)>1);

You might also like