How to create a trigger to UPDATE this for me when a new record is inserted.

I want to create a column called "fullthumb" where by default it has the words "" and add the words in "thumb" column.

UPDATE files SET
fullthumb = CONCAT('', thumb);

Dani AI

Generated

As noted, the manual covers triggers, but here is a concrete, ready approach for that should work immediately.

Use a BEFORE INSERT trigger to build fullthumb while the row is being created (BEFORE triggers can change NEW values; AFTER cannot). Also add a BEFORE UPDATE trigger so changes to thumb keep fullthumb in sync.

DELIMITER //

CREATE TRIGGER files_before_insert
BEFORE INSERT ON files
FOR EACH ROW
BEGIN
  SET NEW.fullthumb = CONCAT('http://example.com/thumbs/', IFNULL(NEW.thumb, ''));
END;
//

DELIMITER ;
DELIMITER //

CREATE TRIGGER files_before_update
BEFORE UPDATE ON files
FOR EACH ROW
BEGIN
  IF NOT (NEW.thumb <=> OLD.thumb) THEN
    SET NEW.fullthumb = CONCAT('http://example.com/thumbs/', IFNULL(NEW.thumb, ''));
  END IF;
END;
//

DELIMITER ;

Notes and tips: prefer storing just the URL (not HTML) and render an anchor tag in your application layer — it keeps presentation separate from data. If some thumb values might already be full URLs, add a check (e.g., IF NEW.thumb LIKE 'http://%' OR NEW.thumb LIKE 'https://%' THEN ...) to avoid double-prefixing. If operating on a modern MySQL (5.7.6+), consider a generated column instead of triggers:

ALTER TABLE files
ADD COLUMN fullthumb VARCHAR(255) AS (CONCAT('http://example.com/thumbs/', thumb)) STORED;

Troubleshooting: if CREATE TRIGGER fails, check that the fullthumb column exists and is wide enough, that the server version supports triggers, that the client uses a proper DELIMITER, and that you have TRIGGER privileges. Test with single-row inserts first to confirm behavior.

Recommended Answers

All 4 Replies

Checkout MySQL Manual.

Checkout MySQL Manual.

I did and I am having trouble doing it...Can anyone help

What you have made so far?

What will i put in my sql command to get that?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.