If I have two tables ‘users’ and ‘orders’ with the same user_id KEY, and I’d like to know if I update the user_id in one table, how do I get it to update in the other table automatically?
CREATE TABLE users(
id SMALLINT AUTO_INCREMENT,
user_id VARCHAR(15),
PRIMARY KEY(id),
KEY(user_id)
) ENGINE=MyISAM;
CREATE TABLE orders(
user_id VARCHAR(15),
//otherStuff,
KEY(user_id)
) ENGINE=MyISAM;
First, i recomend use InnoDB engine, not MyIsam.
InnoDB support foreign keys, in the create table stament include “ENGINE=InnoDB;”
Foreign keys enforce referential integrity between tables i.e. if you try to
INSERTan order with a non-existent user id the system will throw an error.The foreign key can be a reference case on change, ex, for change id in the reference table can be:
This ON UPDATE CASCADE, change the value of user_id in case of change.