6 ms·
For updates, and deletes on the other hand I try to start with the where clause. Learnt the lesson after running a update query once before i typed the where to
by yread 7y ago
For updates, and deletes on the other hand I try to start with the where clause. Learnt the lesson after running a update query once before i typed the where to cause something like 40k worth of damage
- brigandish 7y agoGood advice. I also break up the statement so that an incomplete highlight and execute will fail, e.g. DELETE FROM Customers WHERE Name='Gude' AND Area='Tama' ; Much harder to leave off half the clause doing this kind of thing.
- fr1tkot 7y agoI've forced the habit upon myself to first compose a SELECT statement and verify that it will give me exactly the dataset that I want to run my operation on. Then I wrap that statement into a CTE and run the DELETE / UPDATE on the CTE. It doesn't save me time, but a lot of nerves.
- CubicsRube 7y agoI'm used to writing the SELECT query and adding the commented-out DELETE just below the SELECT line. SELECT * -- DELETE FROM Sales WHERE Customer = 1 Next, selecting the whole query and executing it. If results are satisfactory - selecting only the DELETE part.
- piaste 7y agoThis is the one I use too. Tiny difference - I write select * -- delete from sales where customer = 1 so it's even harder to accidentally highlight the 'delete' part. Although I've switched to DBeaver for a year now, and it automatically pops up a warning when it detects a DELETE query without a WHERE, which is very nice.
- samus 7y agoDBeaver has something even better, albeit sometimes annoying: you can define a connection as "Production" (highlighted red) and "Test" (green). On Production systems, DBeaver will ask you for confirmation for every write statement.
- aleksei 7y agoStart every update and delete with BEGIN; and never fret again.
- XJ6 7y agoAuto-rollback transactions is helpful. Everything is non-destructive without an explicit COMMIT.
- C1sc0cat 7y agoFor destructive statements I almost always do a select with the WHERE statement that I will use in the delete first so I can see I am not doing anything stupid.
- nimrody 7y agoStart all your sessions with "BEGIN TRANSACTION". Then if you make an error you can always ROLLBACK. If it looks OK -- COMMIT.
- scarface74 7y agoAnd then you use MySQL Workbench and forget to turn off autocommmit....
- gonzo41 7y agoYeah i've made a whoops like this before. I usually start a n update with a select statement to make sure my assumptions about the intent of the change is on point.
- cben 7y agoSimilar tip: don't start a command from `rm -rf ...`, type `rm /foo/bar/` first only when finished append ` -rf` at the end. Similarly `git push ... -f` etc.
- samus 7y agoThis saves you only if you delete directories. Better start with `ls`. The one with git force push is spot on though. This is one of the reasons to prefer tools that let you write options at the end of the argument list instead of after the command name.
- h1d 7y agoFor MySQL, run the command with --safe-updates to avoid queries running without WHERE clause.