<p>I have been developing web/desktop applications for about 6 years now. During the course of my career, I have come across application that were heavily written in the database using stored procedures whereas a lot of application just had only a few basic stored procedures (to read, insert, edit and delete entity records) for each entity. </p>

<p>I have seen people argue saying that if you have paid for an enterprise database use its features extensively. Whereas a lot of "object oriented architects" told me its absolute crime to put anything more than necessary in the database and you should be able to drive the application using the methods on those classes? </p>

<p>Where do you think is the balance?</p>

<p>Thanks,
Krunal</p>

[{'answer_id': 22823, 'author': 'TheSmurf', 'author_id': 1975282, 'author_profile': 'https://Stackoverflow.com/users/1975282', 'pm_score': 3, 'selected': False, 'text': "<p>I'm in the object oriented architects camp. It's not necessarily a crime to put code in the database, as long as you understand the caveats that go along with that. Here are some:</p>\n\n<ol>\n<li>It's not debuggable</li>\n<li>It's not subject to source control</li>\n<li>Permissions on your two sets of code will be different</li>\n<li>It will make it more difficult to track where an error in the data came from if you're accessing info in the database from both places</li>\n</ol>\n"}, {'answer_id': 22825, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 3, 'selected': False, 'text': "<p>I think it's a business logic vs. data logic thing. If there is logic that ensures the consistency of your data, put it in a stored procedure. Same for convenience functions for data retrieval/update.</p>\n\n<p>Everything else should go into the code.</p>\n\n<p>A friend of mine is developing a host of stored procedures for data analysis algorithms in bioinformatics. I think his approach is quite interesting, but not the right way in the long run. My main objections are maintainability and lacking adaptability.</p>\n"}, {'answer_id': 22843, 'author': 'Martin Klinke', 'author_id': 1793, 'author_profile': 'https://Stackoverflow.com/users/1793', 'pm_score': 2, 'selected': False, 'text': "<p>I'm coming from almost the same background and have heard the same arguments. I do understand that there are very valid reasons to put logic into the database. However, it depends on the type of application and the way it handles data which approach you should choose.</p>\n\n<p>In my experience, a typical data entry app like some customer (or xyz) management will massively benefit from using an ORM layer as there are not so many different views at the data and you can reduce the boilerplate CRUD code to a minimum.</p>\n\n<p>On the other hand, assume you have an application with a lot of concurrency and calculations that span a lot of tables and that has a fine-grained column-level security concept with locking and so on, you're probably better off doing stuff like that directly in the database.</p>\n\n<p>As mentioned before, it also depends on the variety of views you anticipate for your data. If there are many different combinations of columns and tables that need to be presented to the user, you may also be better off just handing back different result sets rather than map your objects one-by-one to another representation.</p>\n\n<p>After all, the database is good at dealing with sets, whereas OO code is good at dealing with single entities.</p>\n"}, {'answer_id': 22846, 'author': 'dguaraglia', 'author_id': 2384, 'author_profile': 'https://Stackoverflow.com/users/2384', 'pm_score': -1, 'selected': False, 'text': '<p>Well, this one is difficult. As a programmer, you\'ll want to avoid TSQL and such "Database languages" as much as possible, because they are horrendous, difficult to debug, not extensible and there\'s nothing you can do with them that you won\'t be able to do using code on your application.</p>\n\n<p>The only reasons I see <em>for</em> writing stored procedures are:</p>\n\n<ol>\n<li>Your database isn\'t great (think how SQL Server doesn\'t implement LIMIT and you have to work around that using a procedure.</li>\n<li>You want to be able to change a behaviour by changing code in just one place without re-deploying your client applications.</li>\n<li>The client machines have <strong>big</strong> calculation-power constraints (think small embedded devices).</li>\n</ol>\n\n<p>For most applications though, you should try to keep your code in the application where you can debug it, keep it under version control and fix it using all the tools provided to you by your language.</p>\n'}, {'answer_id': 22851, 'author': 'Thomas Owens', 'author_id': 572, 'author_profile': 'https://Stackoverflow.com/users/572', 'pm_score': 1, 'selected': False, 'text': '<p>@DannySmurf:</p>\n\n<p><em>It\'s not debuggable</em></p>\n\n<p>Depending on your server, yes, they are debuggable. <a href="http://www.15seconds.com/issue/050106.htm" rel="nofollow noreferrer">This provides an example for SQL Server 2000</a>. I\'m guessing the newer ones also have this. However, the free MySQL server does not have this (as far as I know).</p>\n\n<p><em>It\'s not subject to source control</em></p>\n\n<p>Yes, it is. Kind of. Database backups should include stored procedures. Those backup files might or might not be in your version control repository. But either way, you have backups of your stored procedures.</p>\n'}, {'answer_id': 22856, 'author': 'Brian Matthews', 'author_id': 1969, 'author_profile': 'https://Stackoverflow.com/users/1969', 'pm_score': 1, 'selected': False, 'text': '<p>My personal preference is to try and keep as much logic and configuration out of the database as possible. I am heavily dependent on Spring and Hibernate these days so that makes it a lot easier. I tend to use Hibernate named queries instead of stored procedures and the static configuration information in Spring application context XML files. Anything that needs to go into the database has to be loaded using a script and I keep those scripts in version control.</p>\n'}, {'answer_id': 22904, 'author': 'TheSmurf', 'author_id': 1975282, 'author_profile': 'https://Stackoverflow.com/users/1975282', 'pm_score': 0, 'selected': False, 'text': "<p>@Thomas Owens: (re source control) Yes, but that's not source control in the same sense that I can check in a .cs file (or .cpp file or whatever) and go and pick out any revision I want. To do that with database code requires a potentially-significant amount of effort to either retrieve the procedure from the database and transfer it to somewhere in the source tree, or to do a database backup every time a minor change is made. In either case (and regardless of the amount of effort), it's not intuitive; and for many shops, it's not a good enough solution either. There is also the potential here for developers who may not be as studious at that as others to forget to retrieve and check in a revision. It's technically possible to put ANYTHING in source control; the disconnect here is what I would take issue with.</p>\n\n<p>(re debuggable) Fair enough, though that doesn't provide much integration with the rest of the application (where the majority of the code could live). That may or may not be important.</p>\n"}, {'answer_id': 43495, 'author': 'stevechol', 'author_id': 2981, 'author_profile': 'https://Stackoverflow.com/users/2981', 'pm_score': 2, 'selected': False, 'text': "<p>Anything that relates to Referential Integrity or Consistency should be in the database as a bare minimum.  If it's in your application and someone wants to write an application against the database they are going to have to duplicate your code in their code to ensure that the data remains consistent.</p>\n\n<p>PLSQL for Oracle is a pretty good language for accessing the database and it can also give performance improvements.  Your application can also be much 'neater' as it can treat the database stored procedures as a 'black box'.</p>\n\n<p>The sprocs themselves can also be tuned and modified without you having to go near your compiled application, this is also useful if the supplier of your application has gone out of business or is unavailable.</p>\n\n<p>I'm not advocating 'everything' should be in database, far from it.  Treat each case seperately and logically and you will see which makes more sense, put it in the app or put it in the database.</p>\n"}, {'answer_id': 77000, 'author': 'user13550', 'author_id': 13550, 'author_profile': 'https://Stackoverflow.com/users/13550', 'pm_score': 0, 'selected': False, 'text': "<p>Well, if you care about the consistency of your data, there are reasons to implement code within the database. As others have said, placing code (and/or RI/constraints) inside the database acts to enforce business logic, close to the data itself.  And, it provides a common, encapsulated interface, so that your new developer doesn't accidentally create orphan records or inconsistent data.  </p>\n"}, {'answer_id': 7151253, 'author': 'Lonnie', 'author_id': 906369, 'author_profile': 'https://Stackoverflow.com/users/906369', 'pm_score': 2, 'selected': False, 'text': "<p>Reading these answers, I'm quite confused by the lack of understanding of database programming. I am an Oracle Pl/sql developer, we source control for every bit of code that goes into the database. Many of the IDEs provide addins for most of the major source control products. From ClearCase to SourceSafe. The Oracle tools we use allow us to debug the code, so debugging isn't an issue. The issue is more of logic and accessibility. </p>\n\n<p>As a manager of support for about 5000 users, the less places i have to look for the logic, the better. If I want to make sure the logic is applied for ALL applications that use the data , even business logic, i put it in the DB. If the logic is different depending on the application, they can be responsible for it.</p>\n"}]
['https://Stackoverflow.com/questions/22818', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2528/']