DELIMITER //
DROP TRIGGER IF EXISTS url_trigger_before_insert //
CREATE TRIGGER url_trigger_before_insert
BEFORE INSERT ON url_table
FOR EACH ROW
BEGIN
DECLARE no_proto CHAR;
SET NEW.scheme = SUBSTRING_INDEX(NEW.url, ':', 1);
SET @no_proto = REPLACE(NEW.url,CONCAT(NEW.scheme,'://'),'');
SET @no_proto = SUBSTRING_INDEX(@no_proto, '/', 1);
SET NEW.subdomain = SUBSTRING_INDEX(@no_proto, '.', 1);
SET NEW.host = SUBSTRING_INDEX(@no_proto, '.', -2);
END //
DROP TRIGGER IF EXISTS url_trigger_before_update //
CREATE TRIGGER url_trigger_before_update
BEFORE UPDATE ON url_table
FOR EACH ROW
BEGIN
DECLARE no_proto CHAR;
SET NEW.scheme = SUBSTRING_INDEX(NEW.url, ':', 1);
SET @no_proto = REPLACE(NEW.url,CONCAT(NEW.scheme,'://'),'');
SET @no_proto = SUBSTRING_INDEX(@no_proto, '/', 1);
SET NEW.subdomain = SUBSTRING_INDEX(@no_proto, '.', 1);
SET NEW.host = SUBSTRING_INDEX(@no_proto, '.', -2);
END //
DELIMITER ;