473,507 Members | 6,459 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trigger for preventing duplicates.

5 New Member
Im creating an employee database for TAFE. I have an employee table with employee_id and place fields.

The trigger is activated by the Before Insert event.

When there's a duplication of employee_id and place the trigger generates a message. For example if a new inserted row has employee_id 20 place 6, and there is a row with employee_id 20 and place 6 already existed in the Employee table, the trigger generates a message: Duplicate Employee_id 20 Place 6.

I don't want a unique constraint.

The code is below:

Expand|Select|Wrap|Line Numbers
  1. CREATE TRIGGER employee_tg ON employee
  2. BEFORE INSERT ON employee
  3. FOR EACH ROW
  4. BEGIN
  5.  
  6. IF NOT EXISTS (SELECT 1 FROM employee e
  7.            INNER JOIN INSERTED i
  8.            ON i.employee_id=e.employee_id
  9.            AND i.place=e.place)
  10. INSERT INTO employee(employee_id,place)
  11. SELECT employee_id,place
  12. FROM INSERTED
  13. ELSE
  14. RAISEERROR ('Duplicate employee_id 20 Place 6)
  15. END IF;
  16. END
  17.  
  18. DELIMITER;
Im having syntax problem in MYSQL. What could be wrong?

Any help would be appreciated!
Dec 4 '08 #1
5 21641
Atli
5,058 Recognized Expert Expert
Hi.

Are you sure this trigger was written for MySQL?
I ask because there are a number of things in there that don't exist in MySQL.
The RAISEERROR function, for instance, is a MSSQL thing if I am not mistaken and does not exists in MySQL.

You say that you do not want this duplicate error.
What is the purpose of this trigger then?
Could you not simply remove it altogether as well as the unique constraint on the key?
Dec 4 '08 #2
kate s
5 New Member
@Atli
Hi, I was researching through google and I found the RAISEERROR function to display the error message. I thought it will work for MYSQL. How can I display the error message then?

I have an employee table with employee_id and place fields. The trigger is activated by the Before Insert event.

The purpose of my trigger is prevent duplication in my table. When there's a duplication of employee_id and place the trigger generates a message. For example if a new inserted row has employee_id 20 place 6, and there is a row with employee_id 20 and place 6 already existed in the Employee table, the trigger generates a message: Duplicate Employee_id 20 Place 6.
Dec 4 '08 #3
Atli
5,058 Recognized Expert Expert
Ok. So you do want a unique constraint.
Then this makes a whole lot more sense :)

You don't really need a trigger for that.
A UNIQUE key would be far easier to use.

For example:
Expand|Select|Wrap|Line Numbers
  1. mysql> CREATE TABLE uniqueTest(
  2.     ->   employee_id Int Not Null,
  3.     ->   place VarChar(50) Not Null,
  4.     ->   UNIQUE `uk_id_place` (employee_id, place)
  5.     -> );
  6. Query OK, 0 rows affected (0.00 sec)
  7.  
  8. mysql> INSERT INTO uniqueTest VALUES (1, 'hello');
  9. Query OK, 1 row affected (0.00 sec)
  10.  
  11. mysql> INSERT INTO uniqueTest VALUES (1, 'hello');
  12. ERROR 1062 (23000): Duplicate entry '1-hello' for key 1
  13.  
Is that what you need?

If you want to do this using a trigger, that is possible to.
And your code isn't far off.

There are a lot of errors in that code however.

First of all, you don't need to INSERT the data manually. Unless the trigger prevents it, the new data will be inserted automatically once the trigger has been executed.

The first line of your code has an extra ON clause that will cause an error.
Check out the CREATE TRIGGER syntax in the manual.

RAISEERROR is a non-standard function that is not used in MySQL.
Unfortunately the standard equivalent for it, the SIGNAL statement, won't be implemented until MySQL 5.2 (as far as I know).
Until then you will have to trigger an error by executing a statement that will cause an error.
I like using the CALL statement, using the error message as the procedure name.
Like:
Expand|Select|Wrap|Line Numbers
  1. mysql> CALL `Duplicate row inserted.`;
  2. ERROR 1305 (42000): PROCEDURE skjol.Duplicate row inserted. does not exist
  3.  
Using this inside a trigger would cause an error and effectively terminate the insert statement.

You would want your code to be something like:
Expand|Select|Wrap|Line Numbers
  1. CREATE TRIGGER triggerName
  2. BEFORE INSERT ON targetTable
  3. FOR EACH ROW
  4. BEGIN
  5.   IF /* The row already exists */ THEN
  6.     CALL 'Can not insert duplicate row';
  7.   END IF;
  8. END
  9.  
Dec 4 '08 #4
kate s
5 New Member
Thank you for your assistance!

The way you created the UniqueTest table and inserted the values and MYSQL complained with an error is exactly what I need!! But I want this using a Trigger.

I still have problems in the code I provided in post#1. I fixed the errors you stated but it still complains with an error.

Do you know of how to create the trigger after the 'IF' statement and assign the error messages an @message variable instead of using the CALL statement?

Expand|Select|Wrap|Line Numbers
  1. DELIMITER \\
  2.  
  3. CREATE TRIGGER employee_tg
  4. BEFORE INSERT ON employee 
  5. FOR EACH ROW 
  6. BEGIN 
  7.   IF /* The employee_id and place already exist*/ THEN 
  8.     /*DISPLAY ERROR MESSAGE*/ 
  9.   END IF; 
  10. END 
  11.  
Your help would be much appreciated!!
Dec 5 '08 #5
kate s
5 New Member
Hi Atli, I cam up with the following code:

Expand|Select|Wrap|Line Numbers
  1. DELIMITER //
  2.  
  3. CREATE TRIGGER employee_tg 
  4. BEFORE INSERT ON employee
  5. FOR EACH ROW
  6.  
  7. BEGIN
  8.  
  9.  
  10. IF new.employee_id = employee_id
  11. AND new.place = place
  12. THEN
  13. SET @messsage = 'duplicate employee_id and place';
  14.  
  15. ELSEIF new.employee_id = new.employee_id
  16. AND new.place = new.place
  17. THEN
  18. SET @messsage = 'No duplication';
  19.  
  20.  
  21. END IF;
  22. END;//
  23.  
I was just wondering if the code above prevents duplicate records?? If not how can I correct this?

Thanks
Dec 5 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
4188
by: Trevor Fairchild | last post by:
I am trying to create a very minimal auditing system for a series of databases. I am in the process of writing Update triggers for 5 Tablse. I will write a trigger for each table-the trigger's...
1
7227
by: Rebecca Lovelace | last post by:
I have a trigger on a table. I am trying to dynamically log the changed fields on the table to another table, so I am iterating through the bits in COLUMNS_UPDATED() to find what's changed, and...
3
2397
by: Alexander Anderson | last post by:
I have a DELETE statement that deletes duplicate data from a table. It takes a long time to execute, so I thought I'd seek advice here. The structure of the table is little funny. The following is...
4
3604
by: Drew | last post by:
I have a permission tracking app that I am working on, and I have made the insert page for it. I am having issues on how to prevent duplicates from getting entered. Currently the interface for...
3
2197
by: Mae Lim | last post by:
Hi All, I've a problem here, I want to trigger a javascript function after binding the ListBox control. Basically the javascript will remove the duplicates records after recordset is bind in the...
16
3462
by: ARC | last post by:
Hello all, So I'm knee deep in this import utility program, and am coming up with all sorts of "gotcha's!". 1st off. On a "Find Duplicates Query", does anyone have a good solution for...
11
5976
by: richardkreidl | last post by:
I'm trying to check for duplicates before I do an INSERT into the Access database table. I basically want to alert the user if the 'ProjectName' and the 'MileStones' are already in the table.. ...
3
2381
by: Lester | last post by:
I'm driving myself crazy with a problem in trying to translate a query written for Access to that for SQL server. I would think that I would use a trigger, but am not sure how to set it up. We...
0
2070
by: NotepadZ | last post by:
I am getting a frustrating error when i try to insert into the table the trigger i created is on. Any insight would be appreciated Error: db2 => INSERT INTO trstat VALUES ('340758A', ...
0
7314
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7030
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7482
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5041
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1540
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.