![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hi, I'm attempting to create a web form to allow an administrative user to "clean up" the records of a (former) user, but I am uncertain about how to phrase my query in SQL. The web form (the back end of which will be speaking to the database via PHP) knows the id of the former user, and needs to carry out the following actions when the form is submitted: The user may have zero or more records in each of the following tables, which can be found as the person_id field in such records will match the user id: researcher publication publication_source qualification profile_record Any matching records found should be deleted. For a simple query on *one* of these tables, this would be (with the known user id instantiated via PHP): DELETE FROM researcher WHERE person_id = $php_person_id; I guess I could just emit several sequential queries, but I feel it's better to make just one query, so that it either all succeeds or all fails, should something go wrong. (This is hopefully all that would have to worry about with this approach, as only a handful of admin users can access this particular form, and as the former user is no longer here, they cannot attempt to edit their record elsewhere.) I have a horrible feeling that this would involve some kind of JOIN, which I (still) don't fully grok properly (JOINs give me enough brain hurt even for just SELECTs :-( ), but I'm not really sure what I should be trying to join on here.. To top it off, at the same time I also need to UPDATE the user's record in the 'person' table, to set most of the fields to NULL (apart from the user's id, name fields, and a few others required for record-keeping purposes as other systems need this data). Is it somehow possible to squeeze this somewhere into the same query? (Hmm, life would be so much easier if MyISAM tables supported transactions..) Thanks for any pointers in the right direction, David. It seems to be possible to delete from multiple comma delimited table |
#3
| |||
| |||
|
|
Hi, I'm attempting to create a web form to allow an administrative user to "clean up" the records of a (former) user, but I am uncertain about how to phrase my query in SQL. The web form (the back end of which will be speaking to the database via PHP) knows the id of the former user, and needs to carry out the following actions when the form is submitted: The user may have zero or more records in each of the following tables, which can be found as the person_id field in such records will match the user id: researcher publication publication_source qualification profile_record Any matching records found should be deleted. For a simple query on *one* of these tables, this would be (with the known user id instantiated via PHP): DELETE FROM researcher WHERE person_id = $php_person_id; I guess I could just emit several sequential queries, but I feel it's better to make just one query, so that it either all succeeds or all fails, should something go wrong. (This is hopefully all that would have to worry about with this approach, as only a handful of admin users can access this particular form, and as the former user is no longer here, they cannot attempt to edit their record elsewhere.) I have a horrible feeling that this would involve some kind of JOIN, which I (still) don't fully grok properly (JOINs give me enough brain hurt even for just SELECTs :-( ), but I'm not really sure what I should be trying to join on here.. To top it off, at the same time I also need to UPDATE the user's record in the 'person' table, to set most of the fields to NULL (apart from the user's id, name fields, and a few others required for record-keeping purposes as other systems need this data). Is it somehow possible to squeeze this somewhere into the same query? (Hmm, life would be so much easier if MyISAM tables supported transactions..) Thanks for any pointers in the right direction, David. |
#4
| |||
| |||
|
|
David wrote: [asking about deleting records from multiple tables] It seems to be possible to delete from multiple comma delimited table names....etc.. so multiple deletes across tables are possible, and the where condition I guess will be adjusted to the same table as the delete criterion is being applied to. So: $query=sprintf("DELETE FROM researcher, kitchen_list,xmas_fund, employees WHERE person_id =%d", $php_person_id); mysql_query($query); |
|
I'd setup up a test database and see what the exact syntax needs to be. |
|
I don't see how you can combine an update and a delete in one sql statement though. |
#5
| |||
| |||
|
|
It helps if your database is properly designed: 1. Use InnoDB as your engine |
|
2. Set up the proper foreign keys with ON DELETE CASCADE |
|
3. Delete the row in the person table |
#6
| |||
| |||
|
|
$query=sprintf("DELETE FROM researcher, kitchen_list,xmas_fund, employees WHERE person_id =%d", $php_person_id); mysql_query($query); Should work.. http://dev.mysql.com/doc/refman/5.1/en/delete.html |
#7
| |||
| |||
|
|
I'm fast coming to the conclusion myself that that's really the only sensible choice, given all the limitations in MyISAM that I seem to discover, the more I read. Unfortunately, I "inherited" this database, and change is not a luxury that I've really had the time to investigate. Are there any gotchas in converting a database from MyISAM to InnoDB that I would need to be aware of (assuming that an existing database can be converted, of course)? |
|
2. Set up the proper foreign keys with ON DELETE CASCADE I'm becoming vaguely aware of this as I read.. |
|
3. Delete the row in the person table That wouldn't work in this case, however, as the record in the person table is not to be deleted entirely (as some of the data is required for ongoing record-keeping) but only have the data in no-longer-needed fields removed, so I wouldn't be able to trigger the deletion of the records in the other tables this way. :-( |
#8
| |||
| |||
|
|
Hi, I'm attempting to create a web form to allow an administrative user to "clean up" the records of a (former) user, but I am uncertain about how to phrase my query in SQL. The web form (the back end of which will be speaking to the database via PHP) knows the id of the former user, and needs to carry out the following actions when the form is submitted: The user may have zero or more records in each of the following tables, which can be found as the person_id field in such records will match the user id: researcher publication publication_source qualification profile_record Any matching records found should be deleted. For a simple query on *one* of these tables, this would be (with the known user id instantiated via PHP): *DELETE FROM researcher WHERE person_id = $php_person_id; I guess I could just emit several sequential queries, but I feel it's better to make just one query, so that it either all succeeds or all fails, should something go wrong. (This is hopefully all that would have to worry about with this approach, as only a handful of admin users can access this particular form, and as the former user is no longer here, they cannot attempt to edit their record elsewhere.) I have a horrible feeling that this would involve some kind of JOIN, which I (still) don't fully grok properly (JOINs give me enough brain hurt even for just SELECTs :-( ), but I'm not really sure what I should be trying to join on here.. To top it off, at the same time I also need to UPDATE the user's record in the 'person' table, to set most of the fields to NULL (apart from the user's id, name fields, and a few others required for record-keeping purposes as other systems need this data). Is it somehow possible to squeeze this somewhere into the same query? (Hmm, life would be so much easier if MyISAM tables supported transactions..) Thanks for any pointers in the right direction, David. |
![]() |
| Thread Tools | |
| Display Modes | |
| |