473,387 Members | 1,897 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Trigger for preventing duplicates.

5
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 21626
Atli
5,058 Expert 4TB
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
@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 Expert 4TB
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
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
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
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
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
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
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
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
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
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
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
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...

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.