Ejemplos de funciones en mysql

Prueba de variables:

CREATE FUNCTION `borrame` ()
RETURNS INTEGER
BEGIN

declare var int;
declare nombre varchar(50);
declare num int;

set nombre='juan';

set var=5;

set var=var+3;

select count(actor_id) into num from actor;

RETURN num+var+length(nombre);
END

Usar un parámetro y un into variable:

CREATE FUNCTION `actores_como`(cadena varchar(50)) 
RETURNS int(11)
BEGIN

declare total int;
select count(actor_id) into total from actor 
where first_name like concat('%',cadena,'%') 
or last_name like concat('%',cadena,'%') ;
RETURN total;
END

Uso de if:

CREATE 
FUNCTION `tipo_sueldo`(sueldo INT) 
RETURNS varchar(10) 
BEGIN
DECLARE a VARCHAR(10);

IF sueldo>1500 then
RETURN 'ALTO';
ELSE
RETURN 'BAJO';
END IF;

END

Uso de case:

CREATE FUNCTION `tipo_sueldo_ampliado`(sueldo int) 
RETURNS varchar(30) 
BEGIN
declare tipo varchar(30);

case
 when sueldo<1500 then
 set tipo='bajo';
 when sueldo<2500 then
 set tipo='medio';
 else
 set tipo='alto';
end case;

RETURN tipo;
END

Uso de while:

CREATE FUNCTION `factorial`(num int)
 RETURNS int(11)
BEGIN
declare total,cont int default 1;

while cont<=num do
 set total=total*cont;
 set cont=cont+1;
end while;

RETURN total;
END

Ejemplos sentencias sql

Actores con más de 10 películas

select first_name, last_name, count(film_id) peliculas from
actor join film_actor using (actor_id)
group by first_name,last_name
having peliculas>30

Países con más de 10 ciudades

select country, count(city) cities
from country join city using (country_id)
group by country
having cities>10

Los diez países que más gastan

select country, sum(amount) total, avg(amount) media
from country join city using (country_id)
join address using (city_id)
join customer using (address_id)
join payment using (customer_id)
group by country
order by total desc
limit 0,10

El actor con más películas

select first_name, last_name, count(film_id) peliculas
from actor join film_actor using(actor_id)
group by first_name,last_name
order by peliculas desc
limit 1

Ejercicios SQL

Actores con ‘ll’ en el apellido o en el nombre
Actores con id>=100 y menor igual que 200

Una de las dos anteriores o las dos
Insertar actor ‘Juan’ ‘Pérez’
Insertar actor ‘Rosa’ ‘Pi’

Actualizar el nombre del actor con id 10 a ‘PEPE’

Actualizar el nombre de los actores con el id > 200 a ‘Actor’ + id

Borrar los actores con id > 200

select * from sakila.actor
where first_name like '%ll%' or last_name like '%ll%';

select * from sakila.actor
where actor_id between 100 and 200;

select * from sakila.actor
where first_name like '%ll%' or last_name like '%ll%' or actor_id between 100 and 200;

insert into sakila.actor (first_name,last_name)
values ('Juan','Pérez');

insert into sakila.actor (first_name,last_name)
values ('Rosa','Pi');

update sakila.actor 
set first_name='PEPE'
where actor_id=10;

update sakila.actor 
set first_name=concat('Actor',actor_id)
where actor_id>200;

delete from sakila.actor
where actor_id>200;

Ejercicio ong

 


DROP TABLE IF EXISTS `poblacion`;

CREATE TABLE `poblacion` (
 `idpoblacion` int(11) NOT NULL AUTO_INCREMENT,
 `nombre` varchar(45) DEFAULT NULL,
 `habitantes` int(11) DEFAULT NULL,
 `pais` varchar(45) DEFAULT NULL,
 PRIMARY KEY (`idpoblacion`),
 KEY `ix_nombre` (`nombre`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


DROP TABLE IF EXISTS `sede`;

CREATE TABLE `sede` (
 `idsede` int(11) NOT NULL AUTO_INCREMENT,
 `ciudad` varchar(45) CHARACTER SET utf8 DEFAULT NULL,
 `pais` varchar(45) DEFAULT NULL,
 `direccion` varchar(45) DEFAULT NULL,
 `telefono` varchar(45) DEFAULT NULL,
 `director` varchar(45) DEFAULT NULL,
 PRIMARY KEY (`idsede`),
 KEY `ix_ciudad` (`ciudad`),
 KEY `ix_pais` (`pais`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;


DROP TABLE IF EXISTS `proyecto`;

CREATE TABLE `proyecto` (
 `idproyecto` int(11) NOT NULL AUTO_INCREMENT,
 `idsede` int(11) DEFAULT NULL,
 `titulo` varchar(45) DEFAULT NULL,
 `fini` date DEFAULT NULL,
 `ffin` date DEFAULT NULL,
 `presupuesto` decimal(10,2) DEFAULT NULL,
 `responsable` varchar(45) DEFAULT NULL,
 PRIMARY KEY (`idproyecto`),
 KEY `fk_sede_idx` (`idsede`),
 KEY `ix_titulo` (`titulo`),
 CONSTRAINT `fk_sede` FOREIGN KEY (`idsede`) REFERENCES `sede` (`idsede`) ON DELETE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;




DROP TABLE IF EXISTS `actuacion`;

CREATE TABLE `actuacion` (
 `idactuacion` int(11) NOT NULL AUTO_INCREMENT,
 `idproyecto` int(11) NOT NULL,
 `idpoblacion` int(11) DEFAULT NULL,
 `inversion` decimal(10,2) DEFAULT NULL,
 `descripcion` text,
 PRIMARY KEY (`idactuacion`),
 KEY `fk_proyecto` (`idproyecto`),
 KEY `fk_poblacion_idx` (`idpoblacion`),
 CONSTRAINT `fk_poblacion` FOREIGN KEY (`idpoblacion`) REFERENCES `poblacion` (`idpoblacion`),
 CONSTRAINT `fk_proyecto` FOREIGN KEY (`idproyecto`) REFERENCES `proyecto` (`idproyecto`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;