Connecting Tech Pros Worldwide Forums | Help | Site Map

Using a Trigger instead of a Constraint to deny Dupilcates

Newbie
 
Join Date: Sep 2009
Posts: 1
#1: Sep 8 '09
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,flower_name) values (1,Daisy); ?

What am i doing wrong?

Expand|Select|Wrap|Line Numbers
  1. DELIMITER // 
  2. CREATE TRIGGER Flower_tg BEFORE INSERT ON flower FOR EACH ROW 
  3.  
  4. BEGIN IF new.flower_id = flower_id 
  5. AND new.flower_name = flower_name 
  6. THEN 
  7. SET @message = 'This is a NO NO'; 
  8.  
  9. ELSEIF new.flower_id = new.flower_id 
  10. AND new.flower_name = new.flower_name 
  11. THEN 
  12. SET @message = 'Permission Granted'; 
  13. END IF; END;// 
Any assitance would be amazing and good karma to you and yours :)

Reply


Similar MySQL Database bytes