You are on page 1of 5

CREATE TABLE IF NOT EXISTS books (

book_id INTEGER UNSIGNED PRIMARY KEY AUTO_INCREMENT, author_id INTEGER UNSIGNED ,


title VARCHAR(100) NOT NULL,
year int(11) UNSIGNED NOT NULL DEFAULT 1900,
language varchar(2) NOT NULL DEFAULT 'es' COMMENT 'ISO 639-1 language', cover_url
varchar (500),
price double(6,2) NOT NULL DEFAULT 10.0,
sellable tinyint(1) DEFAULT 1,
copies int(11) NOT NULL DEFAULT 1,
description text);

CREATE TABLE IF NOT EXISTS authors(


author_id INTEGER UNSIGNED PRIMARY KEY AUTO_INCREMENT,
name varchar(100) NOT NULL,
nationality varchar(100));

CREATE TABLE IF NOT EXISTS clients(


client_id INTEGER UNSIGNED PRIMARY KEY AUTO_INCREMENT,
name varchar (100) NOT NULL,
email varchar (100) NOT NULL UNIQUE,
birthdate DATETIME,
gender ENUM('M','F','ND')NOT NULL,
active TINYINT(1) NOT NULL DEFAULT 1,
creat_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
update_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE
CURRENT_TIMESTAMP);

CREATE TABLE IF NOT EXISTS operations(


operation_id INTEGER UNSIGNED PRIMARY KEY AUTO_INCREMENT,
client_id INTEGER UNSIGNED,
book_id INTEGER UNSIGNED,
type ENUM('PRESTADO','DEVUELTO','VENDIDO'),
creat_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
update_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
finished TINYINT(1) NOT NULL);

FORMAS DE INSERT DE INSERT


INSERT INT TABLA (IDVALUES,VALUES,VALUES)
VALUES('','',......);
INSERT INT TABLA (VALUES,VALUES)
VALUES('','',......);
INSERT INT TABLA
VALUES('','',......);

INSERT INTO authors (name,nationality)


VALUES('Marios Mendoza','COL'),
('Juan Rulfo', 'MEX'),
('Gabriel García Márquez', 'COL'),
('Juan Gabriel Vasquez', 'COL'),
('Julio Cortázar', 'ARG'),
('Isabel Allende', 'MEX'),
('Octavio Paz', 'MEX'),
('Juan Carlos Onelli', 'URU'),
('Fernando Vallejo', 'COL');

INSERT INTO clients(client_id, name, email, birthdate, gender, active)


VALUES
(1,'Maria Dolores Gomez','Maria Dolores.95983222J@random.names','1971-06-
06','F',1),
(2,'Adrian Fernandez','Adrian.55818851J@random.names','1970-04-09','M',1),
(3,'Maria Luisa Marin','Maria Luisa.83726282A@random.names','1957-07-
30','F',1),
(4,'Pedro Sanchez','Pedro.78522059J@random.names','1992-01-31','M',1),
(5,'Carlos Quejada','bizzyrap31@gmail.com','1990-09-13','M',1);

\G * MOSTRAR INFORMACION DE CONSULTA MAS ORGANIZADO

MODIFICAR DATOS
INSERT INTO clients(name, email, birthdate, gender, active)
VALUES ('Carlos Quejada','bizzyrap31@gmail.com','1990-09-13','M',0)
ON DUPLICATE KEY UPDATE active=VALUES(active);

INSERT INTO clients(name, email, birthdate, gender, active)


VALUES ('Carlos Quejada','bizzyrap31@gmail.com','1990-09-13','M',1)
ON DUPLICATE KEY UPDATE gender=VALUES(gender);

INSERT INTO books (title,author_id,price,copies,description)


VALUES ('SATANAS',1,25.00,5,'Capitulos amargos de la historia de bogota');

INSERT INTO books (title,author_id,price, year,copies,description)


VALUES ('BUDA BLUES',1,50.00,2009,6,'historia de bogota');

modificar datos
UPDATE books SET year = 2002 where book_id =2;
eliminar datos
delete from books where book_id = 3;
ejemplo)))))))))))))))))))))
DELETE FROM usuarios
WHERE edad >35
ORDER BY edad
LIMIT 15
)))))))))))))))))))))))))))
INSERT INTO transactions(transaction_id,book_id,client_id,type,finished)
VALUES(1,12,34,'sell',1),
(2,54,87,'lend',0),
(3,3,14,'sell',1),
(4,1,54,'sell',1),
(5,12,81,'lend',1),
(6,12,81,'return',1),
(7,87,29,'sell',1);

select book_id,title, name AS nombre_Autor from books INNER JOIN authors ON


books.author_id = authors.author_id Where authors.author_id >0 and
authors.author_id <=5 ;

select b.title,a.name as nombre_autor, c.name as nombre_cliente, t.type,


t.created_at
from transactions as t
inner join books as b ON t.book_id = b.book_id
inner join clients as c ON t.client_id = c.client_id
inner join authors as a ON b.author_id = a.author_id;

select a.author_id, a.name, a.nationality, b.title


from authors as a
join books as b
on b.author_id = a.author_id
where a.author_id between 1 and 5
order by a.author_id;

select book_id,title, name AS nombre_Autor from books INNER JOIN authors ON


books.author_id = authors.author_id Where authors.author_id between 1 and 5;

select a.author_id, a.name, a.nationality, count(b.book_id)


from authors as a
left join books as b
on b.author_id = a.author_id
where a.author_id between 1 and 5
group by a.author_id
order by a.author_id;

select a.nationality, count(b.book_id)


from authors as a
join books as b
on b.author_id = a.author_id
group a.nationality
order a.nationality;

Que nacionalidades hay


funcion distinct para que no repita los registros
select distinct nationality
from authors
order by nationality;
cuantos escritores hay por cada nacinalidad

condicion where por hay nacionalidades nulas, cuando usemos la funcion count se
debe agrupar con primari key y otro campo
select nationality, count(author_id)
from authors
where nationality IS NOT NULL
group by nationality
order by nationality ;
cuantos libros hay de cada nacionalidad
select a.nationality, count(b.book_id) as num_libros
from authors as a
join books as b
on a.author_id = b.author_id
where nationality IS NOT NULL
group by a.nationality
order by num_libros desc, a.nationality;

select a.nationality, count(b.book_id) as num_libros


from authors as a
join books as b
on a.author_id = b.author_id
where nationality IS NOT NULL AND nationality NOT IN ('USA', 'ENG') EJEMPO DE
EXCLUIR ALGUNA NACIONALIDADES
group by a.nationality
order by num_libros desc, a.nationality;
cual es el promedio del precion de los libros

stt desviacion estandar


select stddev(price), Avg(price)
from books;

select nationality,
count(book_id) as libros,
Avg(price) as promedio,
stddev(price) as desviacion
from books as b
JOIN authors as a ON a.author_id = b.author_id
group by nationality
order by libros desc;
idem pero con nacionalidad
cual es el precion maximo / minimo

select nationality,MAX(price),MIN(price)
from books as b
join authors as a on a.author_id = b.author_id
group by nationality;
como quedario el reporte de prestamos

select c.name,t.type, b.title,concat(a.name,"(", a.nationality,")") as autor,


TO_DAYS (NOW()) - TO_DAYS(t.created_at) AS ago
from transactions as t
LEFT JOIN clients as c ON c.client_id=t.client_id
LEFT JOIN books as b on b.book_id=t.book_id
LEFT JOIN authors as a ON b.author_id=a.author_id;

delete from auhore where author id = 161 limit 1 ;


ver los cliente desactivados
select client_id , name from clients where active <> 1;

select client_id, name, active from clients order by rand() limit 1;

modificar datos
estructura
UPDATE tabla
SET
(columna = valor,......)
WHERE (codicions ) IN (,) para varios
LIMIT 1 ;
update clients set active = 0 where client_id = 80 limit 1

UPDATE clients
SET active = 0
WHERE client_id IN (1,6,8,7,5,6) OR name like '%Lope%';

Select client_id, name, active


FROM clients
WHERE client_id IN (1,6,8,7,5,6) OR name like '%Lope%';

Para eliminar todo el contenido de una tabla y dejarla vacía:

TRUNCATE nameTable;
TRUNCATE nameTable;
elimina tabla
DROP nameTable;
DROP nameTable;

UPDATE authors
SET nationality = 'GBR'
WHERE nationaliTy = 'ENG';

contar registro con las funciones count y sum agregar 1 dentro del parentesis
select count(book_id), sum(1) from books ;

super querys con funcioens


select sum(price*) from books where sellable = 1 ;
select sellable,sum(price*copies) from books group by sellable ;
condicinales en una funcion
esctructura
SUM(if(condicional, 1(true),0(false))
select count(book_id), sum(if(year <2010,1,0)) as '<1990' from books;

select nationality, count(book_id),


sum(if(year <1990,1,0)) as '<1990',
sum(if(year >=1990 and year < 2000,1,0)) as '>2000',
sum(if(year >=2000 and year < 2010,1,0)) as '>2010',
sum(if(year >=2010,1,0)) as '<hoy'
from books as b
JOIN authors as a ON a.author_id = b.author_id
WHERE a.nationality IS NOT NULL
GROUP BY nationality ;

agrer dato auna tabla


alter table authors add column birthyear integer after name; table despues

cambiar tipo de datos en una tabla


alter table authors modify column birthyear year default 1920;

eliminar columna
alter table authors drop column birthyear ;

varios registros segun una funcion


SELECT nombre_columna,AVG(nombre_columna)
FROM nombre_tabla
GRUOP BY nombre_columna
ORDER BY nombre_columna;

You might also like