No tienes acceso a esta clase

¡Continúa aprendiendo! Únete y comienza a potenciar tu carrera

Crea tus primeros triggers

31/34
Recursos

Aportes 11

Preguntas 0

Ordenar por:

¿Quieres ver más aportes, preguntas y respuestas de la comunidad?

Podríamos evitar el trigger modificando el campo updated_at de la siguiente forma:

ALTER TABLE `lines`
	CHANGE COLUMN `updated_at` `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP AFTER `created_at`;

donde estás JuanDC ? D:

Mi solucion al segundo Reto 😀

ALTER TABLE `lines`
	MODIFY `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;

Se puede usar el statement IF NOT EXISTS luego de la palabra TRIGGER para prevenir errores al ejecutar una y otra vez el mismo archivo cuando un Trigger ya existe previamente.

Acá la solución para el resto de tablas:

USE metro_cdmx;

CREATE TRIGGER IF NOT EXISTS update_updated_at_field_lines
BEFORE UPDATE ON `lines`
FOR EACH ROW 
SET NEW.updated_at = NOW();

CREATE TRIGGER IF NOT EXISTS update_updated_at_field_lines_stations
BEFORE UPDATE ON `lines_stations`
FOR EACH ROW 
SET NEW.updated_at = NOW();

CREATE TRIGGER IF NOT EXISTS update_updated_at_field_locations
BEFORE UPDATE ON `locations`
FOR EACH ROW 
SET NEW.updated_at = NOW();

CREATE TRIGGER IF NOT EXISTS update_updated_at_field_stations
BEFORE UPDATE ON `stations`
FOR EACH ROW 
SET NEW.updated_at = NOW();

CREATE TRIGGER IF NOT EXISTS update_updated_at_field_trains
BEFORE UPDATE ON `trains`
FOR EACH ROW 
SET NEW.updated_at = NOW();

para no utilizar los triggers realicé:

ALTER TABLE `stations`
MODIFY COLUMN `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
ALTER TABLE `lines`
MODIFY COLUMN `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
ALTER TABLE `trains`
MODIFY COLUMN `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;

Solución para el segundo reto:

mi solución :) ```js USE metro_cdmx; --Vamos a empezar a utilizar los triggers a ver que pasa con el desencadenamiento de la acción de actualizar el campo de updateted_at. vamos a ver que pasa. --Creando nuestro primer trigger :) CREATE TRIGGER update_updated_at_fiel_lines BEFORE UPDATE ON `lines` FOR EACH ROW SET NEW.updated_at = NOW(); CREATE TRIGGER update_updated_at_fiel_stations BEFORE UPDATE ON `stations` FOR EACH ROW SET NEW.updated_at = NOW(); CREATE TRIGGER update_updated_at_fiel_trains BEFORE UPDATE ON `trains` FOR EACH ROW SET NEW.updated_at = NOW(); CREATE TRIGGER update_updated_at_fiel_locations BEFORE UPDATE ON `locations` FOR EACH ROW SET NEW.updated_at = NOW(); CREATE TRIGGER update_updated_at_fiel_lines_stations BEFORE UPDATE ON `lines_stations` FOR EACH ROW SET NEW.updated_at = NOW(); ```USE metro\_cdmx; \--Vamos a empezar a utilizar los triggers a ver que pasa con el desencadenamiento de la acción de actualizar el campo de updateted\_at. vamos a ver que pasa. \--Creando nuestro primer trigger :) CREATE TRIGGER update\_updated\_at\_fiel\_linesBEFORE UPDATE ON `lines` FOR EACH ROWSET NEW.updated\_at = NOW(); CREATE TRIGGER update\_updated\_at\_fiel\_stationsBEFORE UPDATE ON `stations`FOR EACH ROWSET NEW.updated\_at = NOW(); CREATE TRIGGER update\_updated\_at\_fiel\_trainsBEFORE UPDATE ON `trains` FOR EACH ROW SET NEW.updated\_at = NOW(); CREATE TRIGGER update\_updated\_at\_fiel\_locationsBEFORE UPDATE ON `locations`FOR EACH ROW SET NEW.updated\_at = NOW(); CREATE TRIGGER update\_updated\_at\_fiel\_lines\_stationsBEFORE UPDATE ON `lines\_stations`FOR EACH ROW SET NEW.updated\_at = NOW();
UPDATE `lines` SET name = "Linea 20",update\_at=NOW() WHERE id = 1; CREATE TRIGGER update\_updated\_at\_field\_locations BEFORE UPDATEON `locations`FOR EACH ROWSET NEW.updated\_at=NOW();
USE metro_cdmx;

CREATE TRIGGER update_updated_at_field
BEFORE UPDATE
ON `lines`
FOR EACH ROW
SET NEW.updated_at = NOW();

--trigger for `stations`
USE metro_cdmx;

CREATE TRIGGER update_updated_at_field_stations
BEFORE UPDATE
ON `stations`
FOR EACH ROW
SET NEW.updated_at = NOW();
 -- trigger for `trains` 
 USE metro_cdmx;

CREATE TRIGGER update_updated_at_field_trains
BEFORE UPDATE
ON `trains`
FOR EACH ROW
SET NEW.updated_at = NOW();
--trigger for `lines_stations`
USE metro_cdmx;

CREATE TRIGGER update_updated_at_field_lines_stations
BEFORE UPDATE
ON `lines_stations`
FOR EACH ROW
SET NEW.updated_at = NOW();
-- Note I haven't update the last triggers into the DB `stations`, `lines_stations` & `trains` 

Despues de creadas las tablas hice esta modificacion y me ha funcionado:

ALTER TABLE `stations`
MODIFY COLUMN `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
ALTER TABLE `lines`
MODIFY COLUMN `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
ALTER TABLE `trains`
MODIFY COLUMN `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
ALTER TABLE `lines_stations`
MODIFY COLUMN `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
USE metro_cdmx;

CREATE TRIGGER Supdate_updated_at_field
BEFORE UPDATE 
ON `lines`
FOR EACH ROW 
SET NEW.updated_at=NOW()

CREATE TRIGGER update_updated_at_field
BEFORE UPDATE 
ON `lines_stations`
FOR EACH ROW 
SET NEW.updated_at=NOW();

CREATE TRIGGER update_updated_at_field
BEFORE UPDATE 
ON `lacations`
FOR EACH ROW 
SET NEW.updated_at=NOW();

CREATE TRIGGER update_updated_at_field
BEFORE UPDATE 
ON `stations`
FOR EACH ROW 
SET NEW.updated_at=NOW();

CREATE TRIGGER update_updated_at_field
BEFORE UPDATE 
ON `trains`
FOR EACH ROW 
SET NEW.updated_at=NOW();