Lab 4 filtering at Database - JadenGil/Jaden-Tech-Journal GitHub Wiki
Triggers:
To start, I need to create a trigger that will replace numbers entered into a cat name with 'X'
I used the following articles to help me figure out how to create triggers in MySQL:
https://www.geeksforgeeks.org/mysql-create-trigger/ https://www.geeksforgeeks.org/delimiters-in-sql/
From what I gathered from the article this was the proper way to setup a trigger for our needs:
DELIMITER $$
CREATE TRIGGER replace_numbers_in_cat_name
BEFORE INSERT ON cats
FOR EACH ROW
BEGIN
SET NEW.cat_name = REGEXP_REPLACE(NEW.cat_name, '[0-9]', 'X');
END$$
DELIMITER ;
There were a couple of different ways I found that creating triggers could be done online and in some cases, a //
was used instead of $$
but I found that $$
was what worked. Whenever I attempted to use //
I would get an error on the website. Although from what I can tell there isn't much of a difference between using // and $$ so I'm unsure why I was getting errors.
Deliverable 1:
Checks:
I used the following article to learn about MySQL checks and constraints:
https://www.w3schools.com/mysql/mysql_check.asp
Commands used:
ALTER TABLE cats
ADD CONSTRAINT chk_birthday CHECK (birthday <= '2024-11-23');
Deliverable 2:
Deliverable 3:
ALTER TABLE cats
ADD CONSTRAINT chk_name_length CHECK (CHAR_LENGTH(name) <= 12);