Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
13 de Abril del 2018
# Crearemos una base de datos CREATE DATABASE IF NOT EXISTS basePeliculas; # Seleccionamos la base de datos USE basePeliculas; # Creando tablas sin relación CREATE TABLE IF NOT EXISTS peliculas( id_pelicula INT NOT NULL, titulo VARCHAR(45) NOT NULL, anio DATE, nacionalidad VARCHAR(20), idioma VARCHAR(120), formato ENUM('BLANCO Y NEGRO','COLOR'), descripción VARCHAR(120), resumen VARCHAR(255), observaciones VARCHAR(255), PRIMARY KEY (id_pelicula) ) ENGINE = InnoDB; CREATE TABLE IF NOT EXISTS actores( id_actor INT NOT NULL, nombre VARCHAR(45) NOT NULL, nacionalidad VARCHAR(20), nombre_personaje VARCHAR(120), PRIMARY KEY (id_actor) ) ENGINE = InnoDB; CREATE TABLE IF NOT EXISTS directores( id_director INT NOT NULL, nombre VARCHAR(45) NOT NULL, fecha_nacimiento DATE, pais_origen VARCHAR(120), PRIMARY KEY (id_director), peliculas_id_pelicula INT NOT NULL, CONSTRAINT fkdirector_peliculas FOREIGN KEY(peliculas_id_pelicula) REFERENCES peliculas(id_pelicula) ) ENGINE = InnoDB; CREATE TABLE IF NOT EXISTS actores_peliculas( peliculas_id_pelicula INT NOT NULL, actores_id_actor INT NOT NULL, PRIMARY KEY (peliculas_id_pelicula, actores_id_actor), CONSTRAINT fkactor_peliculas_actor FOREIGN KEY(actores_id_actor) REFERENCES actores(id_actor), CONSTRAINT fkactor_actor_peliculas FOREIGN KEY(peliculas_id_pelicula) REFERENCES peliculas(id_pelicula) ) ENGINE = InnoDB; ALTER TABLE peliculas DROP COLUMN nacionalidad; show tables; ALTER TABLE peliculas DROP COLUMN idioma, DROP COLUMN formato, DROP COLUMN descripción, DROP COLUMN resumen, DROP COLUMN observaciones; ALTER TABLE peliculas ADD COLUMN rating VARCHAR(10) NULL AFTER anio, ADD COLUMN compania VARCHAR(50) NOT NULL AFTER rating; SHOW CREATE TABLE peliculas; ALTER TABLE actores DROP COLUMN nacionalidad, DROP COLUMN nombre_personaje; ALTER TABLE actores ADD COLUMN apellido VARCHAR(45) NOT NULL AFTER id_actor, ADD COLUMN sexo VARCHAR(6), ADD COLUMN fecha_nac DATE NOT NULL, ADD COLUMN fecha_muerte DATE; ALTER TABLE directores DROP FOREIGN KEY fkdirector_peliculas; ALTER TABLE actores DROP COLUMN pais_origen, DROP COLUMN peliculas_id_pelicula; ALTER TABLE directores ADD COLUMN apellido VARCHAR(45) NOT NULL AFTER id_director, ADD COLUMN fecha_muerte DATE AFTER fecha_nacimiento; SHOW CREATE TABLE directores; SHOW CREATE TABLE actores; DROP TABLE actores_peliculas; CREATE TABLE generos_peliculas( id_pelicula INT NOT NULL, id_director INT NOT NULL, genero VARCHAR(20) NOT NULL, FOREIGN KEY (id_pelicula) REFERENCES peliculas(id_pelicula), FOREIGN KEY (id_director) REFERENCES directores (id_director) ) ENGINE = InnoDB; CREATE TABLE directores_peliculas( id_pelicula INT NOT NULL, id_director INT NOT NULL, FOREIGN KEY (id_pelicula) REFERENCES peliculas(id_pelicula), FOREIGN KEY (id_director) REFERENCES directores(id_director) ) ENGINE = InnoDB; CREATE TABLE actores_peliculas( id_pelicula INT NOT NULL, id_actor INT NOT NULL, FOREIGN KEY (id_pelicula) REFERENCES peliculas(id_pelicula) ) ENGINE = InnoDB; CREATE TABLE revision( nombre VARCHAR(20) NOT NULL, hora TIMESTAMP, id_pelicula INT NOT NULL, rating INT, comentario VARCHAR(500) NOT NULL, FOREIGN KEY(id_pelicula) REFERENCES peliculas(id_pelicula) ) ENGINE = InnoDB; CREATE TABLE maxPersonaId( id int ) ENGINE = InnoDB; CREATE TABLE maxPeliculaId( id int ) ENGINE = InnoDB; USE `basepeliculas`; DROP procedure IF EXISTS `verificar_anio`; DELIMITER $$ USE `basepeliculas`$$ CREATE PROCEDURE `verificar_anio` () BEGIN IF anio < 1800 AND anio > 2100 THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'El año debe ser mayor o igual a 1800 y menor o igual que 2100'; END IF; END$$ DELIMITER ; DROP PROCEDURE verificar_anio; DELIMITER $$ CREATE DEFINER = 'root'@'localhost' PROCEDURE verificar_anio(IN anio INT) BEGIN IF anio < 1800 AND anio > 2100 THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'El año debe ser mayor o igual a 1800 y menor o igual que 2100'; END IF; END$$ DELIMITER ; DELIMITER $$ CREATE DEFINER = 'root'@'localhost' PROCEDURE verificar_id(IN id INT) BEGIN IF id < 0 THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'El id tiene que ser mayor que 0'; END IF; END$$ DELIMITER ; DELIMITER $$ CREATE DEFINER = 'root'@'localhost' PROCEDURE verificar_id(IN id INT) BEGIN IF id < 0 THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'El id tiene que ser mayor que 0'; END IF; END$$ DELIMITER ; DELIMITER $$ CREATE DEFINER = 'root'@'localhost' TRIGGER anio_antes_insertar BEFORE INSERT ON peliculas FOR EACH ROW BEGIN CALL verificar_anio(new.anio); END$$ DELIMITER ; DELIMITER $$ CREATE DEFINER = 'root'@'localhost' TRIGGER anio_antes_actualizar BEFORE UPDATE ON peliculas FOR EACH ROW BEGIN CALL verificar_anio(new.anio); END$$ DELIMITER ; INSERT INTO peliculas VALUES (273,'Baby Take a Bow',1700,'PG','Fox Film Corporation');
run
|
edit
|
history
|
help
0
par
delivery_history
gandu
Práctica MySQL 1
/Users/svetlanakanevskaa/Downloads/ACDB.sql
ITM_14210069
my sql
restaurants_menu
How to run query to keep maximum 3 rows with the same name
test