CREATE DATABASE institucionEDU; USE institucionEDU; CREATE TABLE departamento ( id…
- User
CREATE DATABASE institucion_EDU; USE institucion_EDU;
CREATE TABLE departamento ( id INT(10) AUTO_INCREMENT PRIMARY KEY, nombre VARCHAR(50) NOT NULL );
CREATE TABLE profesor ( id INT(10) AUTO_INCREMENT PRIMARY KEY, nif VARCHAR(9) NOT NULL, nombre VARCHAR(25) NOT NULL, apellido1 VARCHAR(50) NOT NULL, apellido2 VARCHAR(50), ciudad VARCHAR(25), direccion VARCHAR(50), telefono VARCHAR(9), fecha_nacimiento DATE, sexo ENUM('M', 'F'), id_departamento INT(10), FOREIGN KEY (id_departamento) REFERENCES departamento(id) );
CREATE TABLE alumno ( id INT(10) AUTO_INCREMENT PRIMARY KEY, nif VARCHAR(9) NOT NULL, nombre VARCHAR(25) NOT NULL, apellido1 VARCHAR(50) NOT NULL, apellido2 VARCHAR(50), ciudad VARCHAR(25), direccion VARCHAR(50), telefono VARCHAR(9), fecha_nacimiento DATE, sexo ENUM('M', 'F') );
CREATE TABLE grado ( id INT(10) AUTO_INCREMENT PRIMARY KEY, nombre VARCHAR(100) NOT NULL );
CREATE TABLE curso_escolar ( id INT(10) AUTO_INCREMENT PRIMARY KEY, anyo_inicio YEAR(4) NOT NULL, anyo_fin YEAR(4) NOT NULL );
CREATE TABLE asignatura ( id INT(10) AUTO_INCREMENT PRIMARY KEY, nombre VARCHAR(100) NOT NULL, creditos FLOAT NOT NULL, tipo ENUM('Obligatoria', 'Optativa') NOT NULL, curso TINYINT(3) NOT NULL, cuatrimestre TINYINT(3) NOT NULL, id_profesor INT(10), id_grado INT(10), FOREIGN KEY (id_profesor) REFERENCES profesor(id), FOREIGN KEY (id_grado) REFERENCES grado(id) );
CREATE TABLE alumno_se_matricula_asignatura ( id_alumno INT(10), id_asignatura INT(10), id_curso_escolar INT(10), PRIMARY KEY (id_alumno, id_asignatura, id_curso_escolar), FOREIGN KEY (id_alumno) REFERENCES alumno(id), FOREIGN KEY (id_asignatura) REFERENCES asignatura(id), FOREIGN KEY (id_curso_escolar) REFERENCES curso_escolar(id) ); /* Departamento */ INSERT INTO departamento VALUES (1, 'Informática'); INSERT INTO departamento VALUES (2, 'Matemáticas'); INSERT INTO departamento VALUES (3, 'Economía y Empresa'); INSERT INTO departamento VALUES (4, 'Educación'); INSERT INTO departamento VALUES (5, 'Agronomía'); INSERT INTO departamento VALUES (6, 'Química y Física'); INSERT INTO departamento VALUES (7, 'Filología'); INSERT INTO departamento VALUES (8, 'Derecho'); INSERT INTO departamento VALUES (9, 'Biología y Geología');
/* Persona / / Persona */ INSERT INTO alumno VALUES (1, '89542419S', 'Juan', 'Saez', 'Vega', 'Almería', 'C/ Mercurio', '618253876', '1992-08-08', 'M'); INSERT INTO alumno VALUES (2, '26902806M', 'Salvador', 'Sánchez', 'Pérez', 'Almería', 'C/ Real del barrio alto', '950254837', '1991-03-28', 'M'); INSERT INTO alumno VALUES (4, '17105885A', 'Pedro', 'Heller', 'Pagac', 'Almería', 'C/ Estrella fugaz', NULL, '2000-10-05', 'M'); INSERT INTO alumno VALUES (6, '04233869Y', 'José', 'Koss', 'Bayer', 'Almería', 'C/ Júpiter', '628349590', '1998-01-28', 'M'); INSERT INTO alumno VALUES (7, '97258166K', 'Ismael', 'Strosin', 'Turcotte', 'Almería', 'C/ Neptuno', NULL, '1999-05-24', 'M'); INSERT INTO alumno VALUES (9, '82842571K', 'Ramón', 'Herzog', 'Tremblay', 'Almería', 'C/ Urano', '626351429', '1996-11-21', 'M'); INSERT INTO alumno VALUES (11, '46900725E', 'Daniel', 'Herman', 'Pacocha', 'Almería', 'C/ Andarax', '679837625', '1997-04-26', 'M'); INSERT INTO alumno VALUES (19, '11578526G', 'Inma', 'Lakin', 'Yundt', 'Almería', 'C/ Picos de Europa', '678652431', '1998-09-01', 'F'); INSERT INTO alumno VALUES (21, '79089577Y', 'Juan', 'Gutiérrez', 'López', 'Almería', 'C/ Los pinos', '678652431', '1998-01-01', 'M'); INSERT INTO alumno VALUES (22, '41491230N', 'Antonio', 'Domínguez', 'Guerrero', 'Almería', 'C/ Cabo de Gata', '626652498', '1999-02-11', 'M'); INSERT INTO alumno VALUES (23, '64753215G', 'Irene', 'Hernández', 'Martínez', 'Almería', 'C/ Zapillo', '628452384', '1996-03-12', 'F'); INSERT INTO alumno VALUES (24, '85135690V', 'Sonia', 'Gea', 'Ruiz', 'Almería', 'C/ Mercurio', '678812017', '1995-04-13', 'F');
- Assistant
The SQL code you provided defines a function to calculate the total credits of a student (referred to as
AlumnoID) for a specific academic year (referred to asAnio). The function retrieves the credits from theasignaturatable by joining it with thealumno_se_matricula_asignaturaandcurso_escolartables to filter the records according to the student ID and the academic year. - User
CREATE FUNCTION TotalCreditosAlumno(AlumnoID INT, Anio INT) -> RETURNS FLOAT -> BEGIN -> DECLARE TotalCreditos FLOAT; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 4 mysql> SELECT SUM(asg.creditos) INTO TotalCreditos -> FROM asignatura asg -> JOIN alumno_se_matricula_asignatura am ON asg.id = am.id_asignatura -> JOIN curso_escolar ce ON am.id_curso_escolar = ce.id -> WHERE am.id_alumno = AlumnoID AND ce.anyo_fin = Anio; ERROR 1327 (42000): Undeclared variable: TotalCreditos mysql> RETURN TotalCreditos; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'RETURN TotalCreditos' at line 1 mysql> END; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'END' at line 1
- Assistant
It looks like you're having trouble with the syntax for creating the function in MySQL. Let's ensure we have the correct structure, including handling the delimiter properly and using declarations correctly.
The error messages you're seeing suggest that there are problems with variable declaration and how the function blocks are defined. Here’s a corrected version of the function:
Corrected Function Definition
DELIMITER $$ CREATE FUNCTION TotalCreditosAlumno(AlumnoID INT, Anio INT) RETURNS FLOAT BEGIN DECLARE TotalCreditos FLOAT DEFAULT 0; SELECT SUM(asg.creditos) INTO TotalCreditos FROM asignatura AS asg JOIN alumno_se_matricula_asignatura AS am ON asg.id = am.id_asignatura JOIN curso_escolar AS ce ON am.id_curso_escolar = ce.id WHERE am.id_alumno = AlumnoID AND ce.anyo_fin = Anio; RETURN IFNULL(TotalCreditos, 0); END$$ DELIMITER ; - User
dame la slucion CREATE FUNCTION TotalCreditosAlumno(AlumnoID INT, Anio INT) -> RETURNS FLOAT -> BEGIN -> DECLARE TotalCreditos FLOAT DEFAULT 0; -> -> SELECT SUM(asg.creditos) INTO TotalCreditos -> FROM asignatura AS asg -> JOIN alumno_se_matricula_asignatura AS am ON asg.id = am.id_asignatura -> JOIN curso_escolar AS ce ON am.id_curso_escolar = ce.id -> WHERE am.id_alumno = AlumnoID AND ce.anyo_fin = Anio; -> -> RETURN IFNULL(TotalCreditos, 0); -> END$$ ERROR 1418 (HY000): This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you might want to use the less safe log_bin_trust_function_creators variable) mysql> mysql> DELIMITER ;
- Assistant
The error message you encountered indicates that your function needs to specify whether it is deterministic or nondeterministic, especially when binary logging is enabled on your MySQL server. In this case, you should declare the function as
DETERMINISTICbecause it will always produce the same output given the same input parameters, with no side effects.Here's how you can modify your function:
Revised Function Definition
DELIMITER $$ CREATE FUNCTION TotalCreditosAlumno(AlumnoID INT, Anio INT) RETURNS FLOAT DETERMINISTIC BEGIN DECLARE TotalCreditos FLOAT DEFAULT 0; SELECT SUM(asg.creditos) INTO TotalCreditos FROM asignatura AS asg JOIN alumno_se_matricula_asignatura AS am ON asg.id = am.id_asignatura JOIN curso_escolar AS ce ON am.id_curso_escolar = ce.id WHERE am.id_alumno = AlumnoID AND ce.anyo_fin = Anio; RETURN IFNULL(TotalCreditos, 0); END$$ DELIMITER ; - User
dame la solucion CREATE FUNCTION PromedioHorasPorAsignatura(AsignaturaID INT) -> RETURNS FLOAT -> BEGIN -> DECLARE PromedioHoras FLOAT; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 4 mysql> SELECT AVG(horas) INTO PromedioHoras -> FROM asignatura -> WHERE id = AsignaturaID; ERROR 1327 (42000): Undeclared variable: PromedioHoras mysql> RETURN PromedioHoras; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'RETURN PromedioHoras' at line 1 mysql> END; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'END' at line 1
- Assistant
The errors you're encountering suggest there are issues with the function's syntax and the variable declarations. I'll guide you through the correct way to create the function
PromedioHorasPorAsignatura, which calculates the average hours for a specific subject based on theAsignaturaID.Steps to Create the Function
- Verify that you are clear about the data type for hours in the table.
- User
dame la solucion CREATE FUNCTION TotalHorasPorDepartamento(DepartamentoID INT) -> RETURNS FLOAT -> BEGIN -> DECLARE TotalHoras FLOAT; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 4 mysql> SELECT SUM(asg.horas) INTO TotalHoras -> FROM asignatura asg -> JOIN profesor p ON asg.id_profesor = p.id -> WHERE p.id_departamento = DepartamentoID; ERROR 1327 (42000): Undeclared variable: TotalHoras mysql> RETURN TotalHoras; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'RETURN TotalHoras' at line 1 mysql> END; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'END' at line 1
- Assistant
It looks like you're encountering errors related to the syntax of the function definition as well as variable declaration and initialization. Let's go through this step by step to ensure that your function for calculating the total hours per department is defined correctly.
- User
dame la solucion CREATE FUNCTION VerificarAlumnoActivo(AlumnoID INT) -> RETURNS BOOLEAN -> BEGIN -> DECLARE Activo BOOLEAN; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 4 mysql> SELECT COUNT(*) > 0 INTO Activo -> FROM alumno_se_matricula_asignatura am -> JOIN curso_escolar ce ON am.id_curso_escolar = ce.id -> WHERE am.id_alumno = AlumnoID AND ce.anyo_fin >= (SELECT MAX(anyo_fin) FROM curso_escolar) - 1; ERROR 1327 (42000): Undeclared variable: Activo mysql> RETURN Activo; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'RETURN Activo' at line 1 mysql> END; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'END' at line 1
- Assistant
The errors you are encountering while trying to create the function
VerificarAlumnoActivomainly stem from variable declaration and SQL syntax issues. Let's correct these problems step-by-step.Key Points to Fix
- User
dame la solucion CREATE FUNCTION EsProfesorVIP(ProfesorID INT) -> RETURNS BOOLEAN -> BEGIN -> DECLARE Satisfactorio BOOLEAN; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 4 mysql> SELECT COUNT(a.id) > 5 INTO Satisfactorio -- Suponiendo que un profesor es VIP si imparte más de 5 asignaturas -> FROM asignatura a -> WHERE a.id_profesor = ProfesorID; ERROR 1327 (42000): Undeclared variable: Satisfactorio mysql> RETURN Satisfactorio; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'RETURN Satisfactorio' at line 1 mysql> END; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'END' at line 1
- Assistant
The errors you’re encountering in your function
EsProfesorVIPstem from the improper syntax and the use of theBOOLEANdata type in MySQL. MySQL represents Boolean values as , so let's correct the function definition accordingly. - User
dame la solucion CREATE TRIGGER ActualizarTotalAsignaturasProfesor AFTER INSERT ON asignatura -> FOR EACH ROW -> BEGIN -> UPDATE profesor SET total_asignaturas = total_asignaturas + 1 WHERE id = NEW.id_profesor; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 4 mysql> END; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'END' at line 1
- Assistant
The errors you're encountering while trying to create the trigger
ActualizarTotalAsignaturasProfesorseem to relate to SQL syntax, especially around the use of delimiters. In MySQL, when you create triggers, you need to change the statement delimiter temporarily to avoid confusion with the default semicolon (). - User
dame la solucion en codigo
- Assistant
Sure! Below is the SQL code to create the trigger
ActualizarTotalAsignaturasProfesorcorrectly, ensuring that you incorporate the necessary changes like setting delimiters and correctly defining the trigger:DELIMITER $$ CREATE TRIGGER ActualizarTotalAsignaturasProfesor AFTER INSERT ON asignatura FOR EACH ROW BEGIN UPDATE profesor SET total_asignaturas = total_asignaturas + 1 WHERE id = NEW.id_profesor; END$$ DELIMITER ;Steps for Implementation
- User
dame la solucion CREATE TRIGGER AuditarActualizacionAlumno AFTER UPDATE ON alumno -> FOR EACH ROW -> BEGIN -> INSERT INTO auditoria_alumno (alumno_id, cambio, fecha) -> VALUES (OLD.id, CONCAT('Cambios en: ', OLD.nombre, ' a ', NEW.nombre), NOW()); ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 5 mysql> END; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'END' at line 1
- Assistant
The errors you encountered while creating the
AuditarActualizacionAlumnotrigger indicate an issue with the syntax, particularly with the delimiters used in MySQL. When creating triggers, changing the statement delimiters temporarily is necessary to avoid confusion with the default semicolon (;). - User
dame la solucion CREATE TRIGGER RegistrarHistorialCreditos AFTER UPDATE ON asignatura -> FOR EACH ROW -> BEGIN -> INSERT INTO historial_creditos (asignatura_id, creditos_old, creditos_new, fecha_cambio) -> VALUES (OLD.id, OLD.creditos, NEW.creditos, NOW()); ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 5 mysql> END; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'END' at line 1
- Assistant
The errors you are encountering when attempting to create the
RegistrarHistorialCreditostrigger in MySQL are likely due to incorrect syntax, particularly regarding the use of delimiters. When defining triggers, it is essential to change the statement delimiter temporarily so that MySQL can correctly parse the entire trigger definition without prematurely terminating at the first semicolon. - User
dame la solucion CREATE TRIGGER NotificarCancelacionMatricula AFTER DELETE ON alumno_se_matricula_asignatura -> FOR EACH ROW -> BEGIN -> INSERT INTO notificaciones (alumno_id, mensaje, fecha) -> VALUES (OLD.id_alumno, 'Matricula cancelada.', NOW()); ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 5 mysql> END; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'END' at line 1
- Assistant
The errors you're encountering while trying to create the
NotificarCancelacionMatriculatrigger likely stem from the syntax regarding delimiters. When defining triggers, it is essential to change the delimiter temporarily to avoid premature termination at the first semicolon (). - User
dame la solucion CREATE TRIGGER RestringirAsignacionExcesiva BEFORE INSERT ON asignatura -> FOR EACH ROW -> BEGIN -> DECLARE total INT; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 4 mysql> SELECT COUNT(*) INTO total -> FROM asignatura -> WHERE id_profesor = NEW.id_profesor; ERROR 1327 (42000): Undeclared variable: total mysql> IF total >= 10 THEN -> SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'El profesor no puede tener más de 10 asignaturas.'; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF total >= 10 THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'El profe' at line 1 mysql> END IF; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'END IF' at line 1 mysql> END; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'END' at line 1
- Assistant