简体   繁体   中英

Update all the tables in a database

When a user changes his username, I need all the tables to updated to replace his old name with his new one.

I have tried:

mysqli_query($con,"UPDATE * SET UserName='".$username2."'
                 WHERE UserName='".$username."'"); 

But this appears to have only changed one table ( I don't know why it picked this specific table; it is not the first or the last in alphabetical sorting order). I tried more than once and the same table kept changing.

First of all, Why would you have 'usename' stored at multiple places ? Its a bad design.

If you still had to do it, then use Primary Key - Foreign Key relation. And on update cascade option. See Here: http://dev.mysql.com/doc/refman/5.6/en/innodb-foreign-key-constraints.html

Another option is to have Triggers. So that when you update a table in question, the trigger fires and it can perform whatever task you have assigned. See Here: http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM