I have the following db schema.
SQL> describe USERS;
Name Null? Type
----------------------------------------- -------- ----------------------------
user_id NOT NULL NUMBER(38)
name NOT NULL VARCHAR2(50)
password NOT NULL VARCHAR2(50)
score NUMBER(38)
notify_before_expiration NUMBER(38)
is_admin NOT NULL NUMBER(1)
SQL> describe EMAIL;
Name Null? Type
----------------------------------------- -------- ----------------------------
email_id NOT NULL NUMBER(38)
email NOT NULL VARCHAR2(50)
user_id NOT NULL NUMBER(38)
So one user has many emails. Users may access a form where they can add/delete theirs email. The question is: Which is the better way to update the db having the list of mails gotten from this form?
I was thinking something like: (java pseudo code)
//List of mails in db.
ArrayList<String> emailsInDB = getAllMailsFromDB(user);
//List of mails from the form
ArrayList<String> emailsInForm = form.getAllMails();
//Iterates all emails gotten from the form.
for (String email : emailsInForm) {
if (emailsInDB.contains(email) {
//Already in the db
emailsInDB.remove(email, user);
} else {
//New mail! Add it in the db!
db.insertMail(email, user);
}
//All emails that were in the db, but not in the form,
//were deleted. Delete them from the db.
for (String email : emailsInDB) {
db.deleteMail(email);
}
Better ideas are welcomed! Thanks for reading.