Using a Trigger instead of a Constraint to deny Dupilcates

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jessicastevens
    New Member
    • Sep 2009
    • 1

    Using a Trigger instead of a Constraint to deny Dupilcates

    Hello,

    My name is Jessica and i am currently employed as a 1st Level Help Desk operator at Sunlight Technologies. We mainly service employee's who require basic assitance with operating their PC's and software within the company.

    I have been working here for 4 years now, and the company has recently hired a new employee who has already started hassling me with how much he knows. I am not sure if this is because i am a woman and *THINKS* he can gain the respect of others in the workplace by undermining my IT experience, i dont know.

    Anyway, forgive me i had to vent L-O-L

    The purpose of this forum post was to show him up with an argument we had earlier. I KNOW i am right and its frustrating when you forget the basics thinking "This is pointless" when in college, and 4 years later you need that knowledge ha-ha.

    I recall my professor teaching us, a way to use a MYSQL command to prompt a message when the operator inserts DUPILCATE data into a table. However nowaday's we would use a CONSTRAINT which i understand, although the new employee was arguing that it can NOT be done. I KNOW it can be done, although have lost the ability to recall HOW to.

    The table and data is not really important, so if we use this random table:

    TABLE NAME - FLOWERS
    (Flower_ID, Flower_Name, Amount_Picked)
    1, Daisy, 3
    2, Pansy, 3
    3, Poppy, 1
    4, Sunflower, 9
    5, Lavender, 6

    How would i write a code that will display a message when an operator inserts say: INSERT INTO flowers (flower_id,flow er_name) values (1,Daisy); ?

    What am i doing wrong?

    Code:
    DELIMITER // 
    CREATE TRIGGER Flower_tg BEFORE INSERT ON flower FOR EACH ROW 
    
    BEGIN IF new.flower_id = flower_id 
    AND new.flower_name = flower_name 
    THEN 
    SET @message = 'This is a NO NO'; 
    
    ELSEIF new.flower_id = new.flower_id 
    AND new.flower_name = new.flower_name 
    THEN 
    SET @message = 'Permission Granted'; 
    END IF; END;//
    Any assitance would be amazing and good karma to you and yours :)
Working...