skip to Main Content

I have a database where in the accounts table the “username” is the “id”.

Now I need to add to the table the field “name” which will work as the “id”, so if I create a new account, I need the “id” field to also copy it’s value at “name” field.

There’s any way to automate that without modifying the account creation process? So “name” will always copy the “id” value.

I’m running PhpMyAdmin in Windows (MySQL).

I need this because I have to work with 2 apps in the same database and I don’t have the code neither can get any updates. One of them is using “id” field to verify users “usernames” and the other one is using the “name” field to verify users.

I know this is not a good thing, I didn’t develop it. But the only thing I can do is modify the database.

2

Answers


  1. You can do it before storing it in the Database.
    Free advice:
    You have to change the design of the Database because it will produce anomalies.

    Login or Signup to reply.
  2. Use a trigger on insert

    CREATE TRIGGER insert_name AFTER INSERT ON accounts
    FOR EACH ROW SET name = NEW.id

    Of course if the id column can be updated you will also need an update trigger

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search