<p>My firm have a talented and smart operations staff who are working very hard. I'd like to give them a SQL-execution tool that helps them avoid common, easily-detected SQL mistakes that are easy to make when they are in a hurry. Can anyone suggest such a tool? Details follow.</p>

<p>Part of the operations team remit is writing very complex ad-hoc SQL queries. Not surprisingly, operators sometimes make mistakes in the queries they write because they are so busy. </p>

<p>Luckily, their queries are all SELECTs not data-changing SQL, and they are running on a copy of the database anyway. Still, we'd like to prevent errors in the SQL they run. For instance, sometimes the mistakes lead to long-running queries that slow down the duplicate system they're using and inconvenience others until we find the culprit query and kill it. Worse, occasionally the mistakes lead to apparently-correct answers that we don't catch until much later, with consequent embarrassment. </p>

<p>Our developers also make mistakes in complex code that they write, but they have Eclipse and various plugins (such as <a href="http://findbugs.sourceforge.net/" rel="nofollow noreferrer" title="FindBugs">FindBugs</a>) that catch errors as they type. I'd like to give operators something similar - ideally it would see</p>

<pre><code>SELECT U.NAME, C.NAME FROM USER U, COMPANY C WHERE U.NAME = 'ibell';
</code></pre>

<p>and before you executed, it would say "Hey, did you realise that's a Cartesian product? Are you sure you want to do that?" It doesn't have to be very smart - finding obviously missing join conditions and similar evident errors would be fine.</p>

<p>It looks like <a href="http://www.toadsoft.com/" rel="nofollow noreferrer" title="TOAD">TOAD</a> should do this but I can't seem to find anything about such a feature. Are there other tools like TOAD that can provide this kind of semi-intelligent error correction?</p>

<p>Update: I forgot to mention that we're using MySQL.</p>

[{'answer_id': 15405, 'author': 'lomaxx', 'author_id': 493, 'author_profile': 'https://Stackoverflow.com/users/493', 'pm_score': 0, 'selected': False, 'text': '<p>You might find <a href="http://www.red-gate.com/Products/SQL_Prompt/index.htm" rel="nofollow noreferrer">SQL Prompt</a> from redgate useful. I\'m not sure what database engine you\'re using, as it\'s only for MSSQL Server</p>\n'}, {'answer_id': 15587, 'author': 'Eric Z Beard', 'author_id': 1219, 'author_profile': 'https://Stackoverflow.com/users/1219', 'pm_score': 0, 'selected': False, 'text': '<p>I\'m not expecting anything like this to exist.  The tool would have to first implement everything that the SQL parser in your database implements, and then it would have to do a data model analysis to predict "bad" queries.</p>\n\n<p>Your best bet might be to write a plugin for a text editor that did some basic checking for suspicious patterns and highlighted them differently than the standard .sql mode.  But even that would be quite difficult.</p>\n\n<p>I would be happy with a tool that set off alarm bells whenever I typed in an update statement without a where clause.  And perhaps administered a mild electric shock, since it\'s usually about 1 in the morning after a long day when mistakes like that happen.</p>\n'}, {'answer_id': 15600, 'author': 'Robby Slaughter', 'author_id': 1854, 'author_profile': 'https://Stackoverflow.com/users/1854', 'pm_score': 0, 'selected': False, 'text': "<p>It would be pretty easy to build this by setting up a sample database with a extremely small amount of dummy data, which would receive the query first. A couple of things will happen:</p>\n\n<ol>\n<li>You might get a SQL syntax error, which would not load the database much since it's a small database.</li>\n<li>You might get back a response which could clearly be shown to contain every row in one or more tables, which is probably not what they want.</li>\n<li>Things which pass the above conditions are likely to be okay, so you can run them against the copy of the production database.</li>\n</ol>\n\n<p>Assuming your schema doesn't change much and is not particularly weird, writing the above is likely the quickest solution to your problem.</p>\n"}, {'answer_id': 15630, 'author': 'Michael Ratanapintha', 'author_id': 1879, 'author_profile': 'https://Stackoverflow.com/users/1879', 'pm_score': 2, 'selected': False, 'text': '<p>If your people are using the mysql(1) program to run queries, you can use the <a href="http://dev.mysql.com/doc/refman/5.1/en/mysql-tips.html" rel="nofollow noreferrer">safe-updates</a> option (aka i-am-a-dummy) to get you part of what you need.  Its name is somewhat misleading; it not only prevents UPDATE and DELETE without a WHERE (which you\'re not worried about), but also adds an implicit LIMIT 1000 to SELECT statements, and aborts SELECTs that have joins and are estimated to consider over 1,000,000 tuples --- perfect for discouraging Cartesian joins.</p>\n'}, {'answer_id': 19462, 'author': 'AdamGiles', 'author_id': 1264, 'author_profile': 'https://Stackoverflow.com/users/1264', 'pm_score': 1, 'selected': False, 'text': '<p>..."writing very complex ad-hoc SQL queries.... they are so busy"</p>\n\n<p>Danger Will Robinson!</p>\n\n<p>Automate Automate Automate.</p>\n\n<p>Ideally, the ops team should not be put into a position where they have to write queries on the fly in a high stress situation – it’s a recipe for disaster! Better for them to build up a library of pre-written scripts that have undergone the appropriate testing to make sure it a) does what you want b) provides an audit trail c) has a possible ‘undo’ type function.</p>\n\n<p>Failing that, giving them a user ID that only has SELECT premissions might help :-)</p>\n'}, {'answer_id': 414391, 'author': 'HLGEM', 'author_id': 9034, 'author_profile': 'https://Stackoverflow.com/users/9034', 'pm_score': 0, 'selected': False, 'text': "<p>I'd start with some coding standards - for instance never use the type of join in your example - it often results in bad results (especially in SQL Server if you try to do an outer join that way, you will get bad results). require them to do explicit joins.</p>\n\n<p>If you have complex relationships, you might consider putting them in views and then writing the adhoc queries from the views. Then at least they will never make the mistake of getting the joins wrong.</p>\n"}, {'answer_id': 414414, 'author': 'Kibbee', 'author_id': 1862, 'author_profile': 'https://Stackoverflow.com/users/1862', 'pm_score': 0, 'selected': False, 'text': "<p>Can't you just limit the amount of time a query can run for?  I'm not sure about MySQL, but for SQL Server, even just the default query analyzer can restrict how long queries will run before they time out.  Couple that with limited rights so they can only run SELECT queries, and you should be pretty much covered.</p>\n"}]
['https://Stackoverflow.com/questions/15399', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']