-1

I want a trigger to display a warning Whenever an user tries to create an account with a username that has been used previously. The user details are stored in users table containing (uid,uname,uemail,upass, created_at)

  • As Tim stated, this is best handled using `UNIQUE` constraint. Still, if you want Triggers, here is the doc: https://dev.mysql.com/doc/refman/5.5/en/trigger-syntax.html Go through it; try something and then share your latest code efforts using the same. So that we can put you in right direction. – Madhur Bhaiya Nov 27 '18 at 06:24
  • I gave you 3 duplicate links: the first is the generic answer for preventing duplicates, the second shows you how to clean the data up before adding the unique index, the third shows you how to do this via trigger if you insists on it. – Shadow Nov 27 '18 at 06:29

1 Answers1

0

Honestly, adding a unique index on the uname username field would probably be easier:

ALTER TABLE users ADD CONSTRAINT cnstr UNIQUE KEY (uname);

Assuming part of the role of the trigger would be to block any insert which tries to use a username which has been used before, having this unique index in place would be an overall good idea anyway.

Regarding how you would handle a duplicate insert, the insert would fail at the application level, and you might even be able to figure out the underlying cause of the error, and display an even more focused error message to your users.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360