How do I delete database rows?

From BRTT

Jump to: navigation, search
Question
Question:

How do I delete database rows?

Somehow, the wfdisc table became corrupted and there are records which I want to remove. How should this be done?


Answer:
Use dbe(1) or dbdelete(1) to delete records, after carefully shutting down programs which are using the database


1) First, shut down processes that access the wfdisc. Never delete records from the active tables like wfdisc, arrival, assoc, origin, and detection, but especially wfdisc, while the real time system is running. (orb2db and cdorb2db would complain and you would likely corrupt your wfdisc). Analysts using dbpick and dbloc2 should be notified and asked to quit those processes as well.

You can either shutdown your real-time system entirely with rtexec -k, or instead use the orb2db_msg program that will target just the real-time processes orb2dbt, cdorb2db and orb2db.


2) As an additional precaution, you might make a copy of the unmodified table before doing anything more. You may feel confident that you know what you are doing, but as someone who has been bitten by that overconfidence in the past, take the time to make sure you have a backup.

3) You can either use dbe with the "Allow Edits" option turned on to carefully select and delete the offending records, or use a command line pipe of dbsubset and dbdelete to remove the records.

Using dbe

If you use dbe, first bring the wfdisc window. Then select Options->Allow_edits (If there is no Allow_edits option, you don't have write permission to the table). Then, find one or more rows which you wish to delete, and select a field in those rows. Then use the Edit->Delete menu to delete the selected rows. Rather than vanishing, they should turn blank. After deleting all the rows you want, select Edit->Crunch_table. This will cause the blank rows to disappear. It is important that you do not crunch an actively updating table, which is why you should have paused the processes that write to this database before starting the cleanup.

Using dbsubset and dbdelete

To do this from the command line, create an expression which selects the records you wish to delete. This may take a few attempts, so use dbselect to look at the results before using dbdelete, something like this:

% dbsubset -v my.wfdisc "nsamp==0 && time < now() - 86400" |

dbselect - sta chan 'strtime(time)' nsamp

When you're satisfied the expression is correct, substitute dbdelete for the dbselect command:

dbsubset -v my.wfdisc "nsamp==0 && time < now()-86400" |

dbdelete -v -

The truly paranoid (or perhaps that's obsessive) will specify the -c option to dbdelete to be absolutely sure the right rows are getting the axe. Of course, you already have a copy of the original file, or the -s option would also be of interest.