News for hackers. Stuff that matters.
Re:SQL is 43 years old – Here’s why we still use it today by ransom1538 on Tuesday May 02, 2017 @07:58AM (#14245622)
SQL has the great side effect of creating structures that future employees can understand. Its a set of tables with relationships. Given these you can quickly inspect the structures independent of the programming language dejour. With a few commands a new employee can understand: business logic, hr, billing, reporting, and other major backbone principals in a matter of hours. On the other hand, I have been at companies that jumped on the noSQL / ORM / wrap it until you can't wrap it / hide sql (rails). When new employees show up... well... its a bunch of semi/no structured stuff spread across thousands of lines of specific logic.
Re:SQL is 43 years old – Here’s why we still use it today by pjmorris on Tuesday May 02, 2017 @08:10AM (#14245671)
"Show me your flowcharts and conceal your tables, and I shall continue to be mystified. Show me your tables, and I won’t usually need your flowcharts; they’ll be obvious." - Fred Brooks, The Mythical Man Month.
Re:SQL is 43 years old – Here’s why we still use it today by vog on Tuesday May 02, 2017 @08:28AM (#14245781)
Note that "tables" in this statement had a different meaning than today. That one wasn't about relational databases, it was about tables as in-memory data structures.

In today's words, "flowcharts" means "code" and "tables" means "data structures".

I don't care if it is:

- tables in a relational database

- nested structs (records) and lists

- nested dictionaries (hash tables) and lists

- JSON

- XML

- ... whatever

What I do care is that I can see the data structures, and not just the code. Static typing often gets that job done fairly well. If you don't use that, please use at least type annotations. This is the most important part of your documetation, and the compiler ensures it remains correct over time. Most of the time, this (+ the function name) is the only documentation I really need.

Re:SQL is 43 years old – Here’s why we still use it today by scarface74 on Tuesday May 02, 2017 @08:37AM (#14245830)
Show me your well structured code and well named unit tests and I won't need your flow charts.

Show me your well defined normalized tables and I have all I need.

But for the love of $entity please don't show me tons of business logic in stored procedures. I actually won't work for a company that expects me to build or maintain a product based on stored procs.

Re:SQL is 43 years old – Here’s why we still use it today by cafard on Tuesday May 02, 2017 @09:00AM (#14245957)
Show me your well-defined normalized tables, and I will know the rules for a valid state of the system. How, though, will I know the rules for a valid transition from one state to another? Have I seen some nasty stored procedures? Yes, a number of which I have written. But I have seen well thought out and useful ones also.
Re:SQL is 43 years old – Here’s why we still use it today by scarface74 on Tuesday May 02, 2017 @09:21AM (#14246102)
With logic being in code:

* it is easily unit tested

* all source code is one place that can be branched, versioned, merged, deployed, rolled back, diffed, code reviewed, approved with pull request etc.

Re:SQL is 43 years old – Here’s why we still use it today by duozerk on Tuesday May 02, 2017 @12:05PM (#14247571)
Or you can simply implement it so that when your code runs it first checks that the stored procedures exist and have the same definition as the one stored in the source code; and to drop/recreate them if that's not the case.

In which case the stored procedures are in your "main" code repo and "deploying" them simply means running the software. Though your point about unit tests stands.

Re:SQL is 43 years old – Here’s why we still use it today by adaml_623 on Tuesday May 02, 2017 @09:35AM (#14246178)
Why are your stored procedures not in the code repo?
Re:SQL is 43 years old – Here’s why we still use it today by lmm on Tuesday May 02, 2017 @10:56AM (#14246834)
Because deployment tooling for stored procedures sucks?
Re:SQL is 43 years old – Here’s why we still use it today by jamra on Tuesday May 02, 2017 @01:15PM (#14248317)
Stored procedures are easier to deploy than table schema. You just need to drop if exists and then create them.

Table schema is the hard part since there is overhead associated with creating an index on an existing table and with renaming a column.

Re:SQL is 43 years old – Here’s why we still use it today by spacemanmatt on Tuesday May 02, 2017 @09:44AM (#14246229)
SQL is code. Not sure how that escapes people.
Re:SQL is 43 years old – Here’s why we still use it today by scarface74 on Tuesday May 02, 2017 @10:54AM (#14246823)
Harder to unit test, harder to merge, harder to roll back...
Re:SQL is 43 years old – Here’s why we still use it today by self-diversity on Tuesday May 02, 2017 @12:59PM (#14248148)
That was true a decade ago. The tooling has caught up in most languages, so it's trivial to embed your SQL in application code and use it in an environmentally independent matter (eg to invoke in unit tests), provided you take the few minutes initially to set things up correctly instead of the all-too-common "fuck it we're a startup" manner people seem to love.
Re:SQL is 43 years old – Here’s why we still use it today by agentultra on Tuesday May 02, 2017 @09:55AM (#14246307)
And you can unit test it too.

http://pgtap.org

Re:SQL is 43 years old – Here’s why we still use it today by scarface74 on Tuesday May 02, 2017 @10:12AM (#14246446)
Guess which is faster for continuous testing - being able to mock out your Entity Framework context and use an in memory representation of your table via Lists or constantly running an ungodly stored proc against the database?
Re:SQL is 43 years old – Here’s why we still use it today by agentultra on Tuesday May 02, 2017 @12:09PM (#14247606)
I can't say that our tests are slow. We can test our database continuously and independently from our various projects that utilize it. Our integration and smoke tests run continuously as you would expect.
Re:SQL is 43 years old – Here’s why we still use it today by scarface74 on Tuesday May 02, 2017 @12:40PM (#14247947)
You can't tell me that you can run a full suite of database dependent tests as you can run a bunch of in memory tests that mock the database queries.

How do you handle 5 - 10 devs working concurrently if they are all running tests?

Re:SQL is 43 years old – Here’s why we still use it today by jamra on Tuesday May 02, 2017 @01:17PM (#14248339)
You work from local databases. You keep a migration script that sets up the schema and another migration script that sets up the data. If you create a feature, you must add to each script when merging.

You can run unit tests by creating transactions, running the test, and then rolling back at the end.

I do get your point, though. It's more convenient to be able to mock your database from your code.

Re:SQL is 43 years old – Here’s why we still use it today by scarface74 on Tuesday May 02, 2017 @01:37PM (#14248567)
That's much more complicated than just mocking your context in unit tests....

https://msdn.microsoft.com/en-us/library/dn314429(v=vs.113)....

Go down to "Testing Query Scenerios". I've wrapped the four "mockset.As" lines into one extension method to make it a lot less verbose.

Re:SQL is 43 years old – Here’s why we still use it today by jamra on Tuesday May 02, 2017 @02:08PM (#14248909)
Yes it is, but it's much less complicated than trying to guess what your generated SQL code will look like.
Re:SQL is 43 years old – Here’s why we still use it today by scarface74 on Tuesday May 02, 2017 @02:49PM (#14249347)
No need to guess. Run all of your unit tests without mocking out your DbContext and add the following ....

context.Database.Log = log.Info

Where Log.info is just a method that accepts a string and take a look at the log file generated from your unit tests.

Re:SQL is 43 years old – Here’s why we still use it today by agentultra on Tuesday May 02, 2017 @01:15PM (#14248318)
Isolation I guess? I don't know what your experience is and why you think it cannot be anything but slow but... it's not?

pgtap lets us unit test our SQL functions, triggers, views, etc. It's integrated with our change management system. It's fast enough and works well.

Re:SQL is 43 years old – Here’s why we still use it today by spacemanmatt on Tuesday May 02, 2017 @09:47AM (#14246250)
I like to use table-driven transition constraints. The basic design is, you have a table of valid transitions (begin state, end state) and a function that you can write a constraint with.

It's the same as other code, but with better data locality.

Re:SQL is 43 years old – Here’s why we still use it today by vog on Tuesday May 02, 2017 @09:26AM (#14246130)
> Show me your well structured code and well named unit tests and I won't need your flow charts.

Please note that the quote uses old language. With "flowcharts" they actually meant what we know call "structured code".

More precisely, the flowcharts were the hand-written program which was then translated into machine code or some kind of assembly. In other words, back then the flowcharts were the highest level code.

Re:SQL is 43 years old – Here’s why we still use it today by mannykannot on Tuesday May 02, 2017 @08:28AM (#14245780)
I have seen at least one case where I could ascribe no meaning to several of the tables in a database. To be fair, seeing the code did not help either, until it became apparent that it was all an attempt to work around a fundamental error. The creator of this mess was blaming the users for it not working, and I was happy to refute that claim and exonerate the users.
Re:SQL is 43 years old – Here’s why we still use it today by forgotpwtomain on Tuesday May 02, 2017 @08:06AM (#14245661)
Rails tried to hide SQL so much that one of the consequences was not supporting proper table level constraints (FK) till AR ~4.2 or so. Just take a look at the DB of a company using Rails in production: hundreds of keys to non-existent relations.
Re:SQL is 43 years old – Here’s why we still use it today by geebee on Tuesday May 02, 2017 @11:10AM (#14246968)
I regularly use raw sql in rails. ORMs are fine for convenience, but for me, they never make or break an app. They can save you some typing on very basic stuff, but if you find you're writing complex ORM code, I think it's a good idea to just use sql directly.
Re:SQL is 43 years old – Here’s why we still use it today by jmfurlott on Tuesday May 02, 2017 @09:00AM (#14245956)
This fact alone, after several years of changing schema and migrations, led to us having major rewrites and porting over to much more hands off approach. It's an understatement how much AR has led to just confusion especially with new employees!
Re:SQL is 43 years old – Here’s why we still use it today by jgeraert on Tuesday May 02, 2017 @08:14AM (#14245693)
Seeing this as well in proprietary software sold by one of the biggest erp software vendors... And it's still not fixed and likely won't be fixed anytime soon.
Re:SQL is 43 years old – Here’s why we still use it today by moron4hire on Tuesday May 02, 2017 @08:44AM (#14245882)
There is a perception amongst lower programmers that FKs are only a thing you enable in production, that they are an impediment to rapid iteration.

I mean, it's completely wrong, I know that. But try being the new guy telling a team of 10 people that.

There is also a perverse corruption of "if it ain't broke, don't fix it" that goes on. If you can spend 100 hours manually validating every relationship in your application code, that's 100 hours you can put into your estimate and you know you can complete. If you only have a cursory understanding of SQL, then "learn more about FKs and implement them across the DB" seems like a big, unknowable blob of time that is impossible to estimate. It doesn't matter that it might only be 5 hours of work, at least we can be certain about 100 hours and bill the client for it.

Finally, there are a small set of things that are fundamentally wrong about all modern RDBMS implementations. For example: it's nonsensical to have a foreign key that isn't indexed. You always want an index on foreign keys, there is never a scenario where you don't want them. But while primary keys are indexed by default, foreign keys are not. And those sorts of things give the anti-RDBMS crowd enough of a foot-hold to argue for continued ignorance.

Re:SQL is 43 years old – Here’s why we still use it today by mattbroekhuis on Tuesday May 02, 2017 @09:11AM (#14246039)
I have found out the hard way that foreign keys are indexed by default in some implementations including MySQL innodb. Not in oracle.

My experience has been the opposite of yours -- I've heard of disabling foreign keys in production only for performance reasons, but not the other way around.

I like them turned on all the time as well. Valuable safety rail

Re:SQL is 43 years old – Here’s why we still use it today by aidos on Tuesday May 02, 2017 @09:20AM (#14246094)
Curious, this doesn't fit my mental model: it's nonsensical to have a foreign key that isn't indexed

I guess you normally want indexes on parent - child tree relationships (book -> chapter), but you don't want them all the time. What about when you have an 'article' with a 'status and a 'category' and you only ever find all articles by combination of category and status? In that case you'd be maintaining 3 indexes, category, status and status_category, but the only one you'd need would be status_category.

Indexes are a tool to to allow for optimising lookups while foreign keys are a tool to allow you to keep your data consistent.

Re:SQL is 43 years old – Here’s why we still use it today by moron4hire on Tuesday May 02, 2017 @09:33AM (#14246171)
I don't see how your example doesn't require an index, specifically because, as you said, indexes are a tool to allow for optimizing lookups.

I imagine that maybe you're suggesting you first query the DB for the ID of the combined status_category, and then query the article by that ID. That's not a good idea, for a couple of reasons. First, you're making two round trips to the DB when you could, with no additional effort (just effort in a different place) be doing one. Second, you've introduced a data race condition. If someone deletes that status_category after you've queried for it but before you've queried the articles, you aren't going to get the results you want.

It would be better to do a join across articles to status_category to status and category, then query based on the status and category values you want. Without an index on the FKs between status_category and status and category, a relatively small table can have a big impact on query performance.

Finally, while I know your example is arbitrary, it's a little hard to argue against a design that is probably wrong. I doubt the suggested schema for articles and categories is a good one. If I argue "you should never have to arbitrarily subtract '1' from a result just to get the results you want", it would not be a good counterargument to say, "yes, but sometimes you want to add 2 and 2 and get 5, so then you need to subtract 1". The problem isn't where you see it.

FKs aren't just a consistency tool. Consistency and referential integrity are features that results from having an FK, but the FK is a signal that data can be searched in a certain way.

Re:SQL is 43 years old – Here’s why we still use it today by dragonwriter on Tuesday May 02, 2017 @12:45PM (#14248001)
> I don't see how your example doesn't require an index, specifically because, as you said, indexes are a tool to allow for optimizing lookups.

If you never lookup by a column alone, you don't need a single-column index on that column. An FK need not ever be a lookup target (the target column[s] it references are necessarily a lookup target, but not necessarily vice versa.)

An FK is probably usually going to want some kind of index, but it's not nonsensical to have a non-indexed FK.

Re:SQL is 43 years old – Here’s why we still use it today by aidos on Tuesday May 02, 2017 @10:23AM (#14246556)
Touché! An index signals the data should be searched in a certain way!

I don't think I explained my arbitrary scenario particularly well :-) I'm definitely not suggesting 2 queries.

If I have articles that could be category: math|science and source: website_a|website_b|... and I only ever query for source and category together then the other indexes aren't used.

It's a contrived example, but in my mind the existence of a foreign key doesn't imply an index is required.

Re:SQL is 43 years old – Here’s why we still use it today by irishsultan on Tuesday May 02, 2017 @10:32AM (#14246636)
I assumed they meant an index on multiple columns (status, category), not a separate table for status_category with an index on that.
Re:SQL is 43 years old – Here’s why we still use it today by jrochkind1 on Tuesday May 02, 2017 @09:50AM (#14246270)
I think in general most rdbms require indexes to properly enforce foreign key constraints in a reasonable way.
Re:SQL is 43 years old – Here’s why we still use it today by dspillett on Tuesday May 02, 2017 @10:20AM (#14246529)
If the parent entities are immutable, or are at least immortal and have primary key values that are immutable, then the DB engine will only need the index on the target primary/unique key/index to maintain the constraint.

Though in general this is not the case, as you say, it is the case often enough that enforcing "FK means an index" could be an annoyance.

Re:SQL is 43 years old – Here’s why we still use it today by jrochkind1 on Tuesday May 02, 2017 @09:49AM (#14246267)
The number and kind of bad ideas a bad development team can have is virtually limitless.
Re:SQL is 43 years old – Here’s why we still use it today by scriptkiddy on Tuesday May 02, 2017 @12:57PM (#14248119)
> There is a perception amongst lower programmers that FKs are only a thing you enable in production, that they are an impediment to rapid iteration.

Really? I've been working as a web programmer for about 5 years now. I use the term Web Programmer because I feel like the term Web Developer carries with it the connotation that you only work with Javascript.

My specialty is definitely with Python and more specifically with Django. Django's ORM is the first ORM I ever used. Maybe it's because I self taught, but I never had the inclination that FKs were an impediment to rapid iteration. In fact, quite the opposite. FKs are a fantastic way to enforce relationship constraints between tables/objects/models/whatever you want to call them.

Whenever I start on a new project, the first thing I do is start defining my data structure. In Django, this mostly involves using the ORM layer to define Model Classes. I usually define the core sets of models necessary for the application. As an example, if I were building a simple blog, I'd start by overriding Django's built in user model so I have the flexibility to add columns or place constraints on existing columns. Then I would define the post model which includes an `author` and a `category` FK column. Then I would define the `Category` model. I generate a migration script and run it. Every time I need to make a change to the Schema, I simply add or change whatever I need to and generate a new migration script alongside the old one. These migrations are dependent upon previous migrations and have version numbers. If I need to, I can roll back to a previous schema. Django makes this all very simple and actually separates the migrations out by which app the model definitions reside in. This means that I can make changes to multiple model definitions and only apply the changes to the Db for one of those model classes. It's very flexible.

So, after my long-winded explanation above, I don't see how people could find FKs to be restrictive when there are so many tools like Django's ORM and migration system that makes altering your schema so simple.

Re:SQL is 43 years old – Here’s why we still use it today by TimJYoung on Tuesday May 02, 2017 @12:36PM (#14247896)
Re: FKs being indexed. This isn't always the case. Our embedded/workgroup-level database engine, ElevateDB, always uses a system-generated index for all PK, FK, and UNIQUE constraints. They're needed for constraint enforcement, anyways, and offer good "default" performance improvements by ensuring that the query optimizer can flip (most) inner joins in order to improve nested-loop join performance.
Re:SQL is 43 years old – Here’s why we still use it today by dspillett on Tuesday May 02, 2017 @10:08AM (#14246399)
> it's nonsensical to have a foreign key that isn't indexed. You always want an index on foreign keys

Incorrect, though cases where you don't want the index are rarer than those where you do especially when thinking about simple examples.

> there is never a scenario where you don't want them.

If the parent entities never have their primary key values changed and are never deleted, then the database engine itself will never use the index to enforce the key. If you never need to join from the parent entities to the child entities then your queries are unlikely to make use of it either.

The extra index takes space (maybe a fair amount of space if the key is wide and/or you have a [bad] design with a wide clustering key), potentially space in your in-memory page pool, and processing time & IO during inserts, updates and deletes. If you are unlikely to need the index then why take that hit?

> But while primary keys are indexed by default, foreign keys are not.

Primary keys need to be indexed to avoid a full table scan on every insert (or update of a key value) to that table or any table that refers to the key via a foreign key constraint. Foreign keys will only cause a scan with no index present if a key value is changed (or a row deleted) in the parent table. As some entities shouldn't be deleted and primary key values should be immutable, it follows that this sort of situation can happen.

It would be possible to make the index optional by other means then requiring you to declare it if you want it, but SQL's modelling language tends towards declaring what you want not what you don't want so it fits better with the syntax to have you add the index if you want it rather than deleting it if you don't or having something in the syntax like "ADD CONSTRAINT fk_key_name ON (<field(s)>) REFERENCES <target>(<field(s)>) WITHOUT INDEX".

> And those sorts of things give the anti-RDBMS crowd enough of a foot-hold to argue for continued ignorance.

This isn't purely a relational problem. noSQL data stores have indexes too, and not having an index on a referencing key that might be needed to check on referential integrity (looking for orphan records) can be a problem there too. We shouldn't change the behaviour of relational stores because some people who use noSQL don't understand good data modelling. Many people using noSQL do understand good data modelling of course, but some use noSQL because they don't want to try understand SQL rather than because it is the wrong tool for the job at hand, and those people probably don't understand noSQL either but get away with not doing so in the short term).

Re:SQL is 43 years old – Here’s why we still use it today by moron4hire on Tuesday May 02, 2017 @10:59AM (#14246870)
> If the parent entities never have their primary key values changed and are never deleted, then the database engine itself will never use the index to enforce the key.

Enforcing referential integrity is not the only thing we can do with foreign keys. Once you have an FK, you're going to want to query against it. To do that without data races requires a join. The join can be optimized better if the FK is indexed.

Come up with an example of an FK you never want to join on and then maybe we talk about not wanting an FK indexed. I've never seen anyone make a convincing argument that this is even as much as a 1% case. It should be so incredibly, almost inconceivably rare to not want an FK indexed that yes, I'm saying it should go against the traditional grain and just be the default. Traditions are not sacred.

Re:SQL is 43 years old – Here’s why we still use it today by dragonwriter on Tuesday May 02, 2017 @12:52PM (#14248081)
> Once you have an FK, you're going to want to query against it.

Not necessarily. You are definitely (at a minimum, implicitly for referential integrity) going to want to query from the table using the FK to the table it references, but you may or may not want to query by the FK column.

> Come up with an example of an FK you never want to join on and then maybe we talk about not wanting an FK indexed.

Joining on an FK doesn't require the FK to be indexed, it requires the target to be indexed. You need an index on the FK of the FK is used in a simple equality or inequality filter criteria other than a join to it's target, or if it's used in a join where the other criteria are filtering it's target table rather than the table with the FK. But if you filter the table with the FK and join to it's target, which is a fairly common case, you don't need an FK index.

Re:SQL is 43 years old – Here’s why we still use it today by dspillett on Wednesday May 03, 2017 @05:44AM (#14254231)
> Once you have an FK, you're going to want to query against it.

Not necessarily, and even where you do there are many cases where an index just on the foreign key is not the best choice because the queries are filtering on other properties too so a wider index is worth defining instead.

> Come up with an example of an FK you never want to join on

Recording tables with many fact dimensions, where you need to enforce a limited range of values in each dimension column but only ever use the data afterwards for aggregating after filtering/grouping by other properties. The storage needed for the unnecessary indexes structures could balloon significantly here. This is most commonly seen in warehousing situations, but the pattern is far from unheard of in OLTP workloads.

> it's nonsensical to have a foreign key that isn't indexed

> and "there is never a scenario where you don't want them

It may be uncommon to prefer not to have an index on a foreign key, but absolute statements like those are simply wrong because there are cases where you don't want (or at least done need) them and there are cases where you want something other than what would be generated automatically.

Useful rules-of-thumb perhaps but only if presented as such ("in general X" or "X is true except when it isn't due to Y or Z") rather than absolutes.

Re:SQL is 43 years old – Here’s why we still use it today by mannykannot on Tuesday May 02, 2017 @08:35AM (#14245815)
The relational model provides a pretty general and unified way to represent information, together with a cohesive and powerful set of primitives for working with it, yet many systems architects insist on hiding it behind an ad-hoc interface that looks like a throwback to pre-relational days.
Re:SQL is 43 years old – Here’s why we still use it today by elsurudo on Tuesday May 02, 2017 @09:19AM (#14246083)
That's true, but you could always either create them yourself in SQL in a migration, or use a gem like foreigner (as I did). Always use FKs...
Re:SQL is 43 years old – Here’s why we still use it today by jrochkind1 on Tuesday May 02, 2017 @09:47AM (#14246248)
You put your business logic in the rdbms? As constraints and triggers and stored procedures and such?

There are advantages to that, but pretty big disadvantages too. For maintaining and developing a non-trivial app, SQL, or a particular vendor's SQL variant, is definitely not my preferred platform. I definitely don't find that something easy to understand coming to an existing project that's been developed like that. I guess it _could_ be a matter of taste and experience, if it is yours!

Re:SQL is 43 years old – Here’s why we still use it today by hackits on Tuesday May 02, 2017 @11:27AM (#14247146)
Most of the time when business logic is put into triggers it turn into a disaster. Even Oracle recommends only putting simple logic within triggers and for the most part should only be used for auditing purposes.

For example too many times I've updated a record with a new column value only to find out that the value I've updated because of some trigger caused the value to be set to null.

Re:SQL is 43 years old – Here’s why we still use it today by cryptonector on Tuesday May 02, 2017 @12:34PM (#14247876)
One advantage: you get all the write concurrency your RDBMS can give you for your transactions. Too many ORMs I've seen just have a Big Lock, leaving you with just one writer at a time.

I can't think of disadvantages to putting your business logic in the RDBMS. Can you elaborate?

Re:SQL is 43 years old – Here’s why we still use it today by jrochkind1 on Tuesday May 02, 2017 @04:33PM (#14250302)
> Too many ORMs I've seen just have a Big Lock, leaving you with just one writer at a time.

Geez, I think you've seen some pretty terrible ORMs. I don't know of any mature and popular ORMs that do that. If they do, I have no idea why they are popular or what makes anyone consider them mature.

ORMs that don't do that include: ActiveRecord, Sequel, Hibernate, Core Data, SQLAlchemy, etc.

Re:SQL is 43 years old – Here’s why we still use it today by pasta on Tuesday May 02, 2017 @01:15PM (#14248316)
I think he meant putting constraint and index logic in the code.

For example: I've seen people rely on a unique index in the code. But if you set a unique index in the DB you know it will be unique always. Even when the code is replaced some day.

Re:SQL is 43 years old – Here’s why we still use it today by Sean1708 on Tuesday May 02, 2017 @01:23PM (#14248409)
I'm not sure I'd call constraints business logic, and I would definitely want to at least double-check them in the database.
Re:SQL is 43 years old – Here’s why we still use it today by cr0sh on Tuesday May 02, 2017 @01:13PM (#14248295)
I could be wrong, but I think the main reason business logic is put into the DB (and provided that the "users" of the DB are also segregated properly), is to present a cohesive view to all users of the DB.

Too often, when you have a "wide open" DB and multiple "users" of that DB (think groups within an org) decide to query it (whether just for reading, or worse, updating), the queries can often turn out to be radically different for the same business logic.

Maybe one development group thinks that a query should be done one way, while another thinks that to get the same data it should be done another, and now you have two or more groups disseminating data to other groups in totally different ways.

You may ask why the organization has multiple development groups, but it occurs. I worked for one company I won't name because it isn't important - as a web developer in their marketing department; our group was considered as separate from the IT group, which handled the main "DB" which was based around AS/400 systems and Apache SOLR - we worked together as best as possible, but also bumped heads, in that they wanted us to only work with their "DB" (I don't really consider SOLR to be a DB, though it can and is used like one by a variety of orgs) through their interface (which at times didn't work like they documented it - and many times changed without us knowing about it until our stuff broke mysteriously - usually around the end-of-year holiday push) - whereas we needed to store a lot of the stuff "nearline" in a MySQL DB (we used MySQL, PHP, and AWS for much of our development) to make things more responsive for our end users (ie - the people buying the products the company made off the websites we were creating). In essence, we a bit of a "split personality" going on - but our main boss was only two levels removed from the CEO of the company, so our stuff generally was tolerated - but it is a similar situation.

Where you have multiple dev groups (whether by design or because "ad-hoc" things occur - like Bob in accounting figuring out how to use ODBC with Excel macros to query the database for data for his and other departments), you can have this kind of chaos. When the DB is heavily controlled by IT, with the only "views" of it through tightly controlled business logic which is part of the DB, this kind of difference in data can be controlled.

But it does have many more downsides, which also means it probably shouldn't be used or done in that manner. Instead, the interface should probably be through a single interface (RESTful or similar), with the business logic in the code of that interface, and only the barest needed other logic in the DB to tie it together. Provided that the security on the DB is tightly controlled, and the only way other groups can access the data is via the exposed interface API, you can achieve the same results I think.

In a way, that's how we had the access at that one company - we had a RESTful interface API to the backend SOLR store; we could send a formatted "query object" and get back one or more "records" as a stream of data (we'd then usually take the data, parse through it and store parts of it into our MySQL DB - because the query/response time of that SOLR DB was horrible from a web development perspective; I don't think this was the fault of IT, but rather the fact that the datastore on the backend was vast, holding information about products dating back 60 or more years - I'm sure there was likely some COBOL in the mix somewhere).

That did have the downside of the fact that if we wanted a particular means to query for something that didn't exist in the existing API, we either had to make due with what we could do "locally" (thru code and/or mysql "buffering"), or we had to put in a request for a change to the IT group (which may or may not get accepted, and might take weeks for the turnaround time before we could use it). Furthermore, as I mentioned before, there were more than a few times that we rolled out a particular feature, only to have the website(s) that relied on that feature break because the backend API changed "behind our backs" (and we usually saw this over a holiday period, when our sales would peak of course). In many cases, we couldn't do anything about this (not even storing the SOLR information - it was too vast, plus there was a nagging idea that if we tried that someone's head would roll for not using the implemented interfaces and data that already existed - we could "buffer" or "cache" things, but we couldn't wholesale transfer the data over).

Re:SQL is 43 years old – Here’s why we still use it today by brudgers on Tuesday May 02, 2017 @08:53AM (#14245925)
Exposing data structures in a human readable form was probably a necessity because SQL slotted itself into a space where COBOL lived...and one of the features of COBOL was its intent to create readable data processing programs. Like SQL, COBOL was generally a very reasonable alternative when practical engineering and business considerations were all on the table (e.g. mature well documented tools and the availability of employable candidates).
Re:SQL is 43 years old – Here’s why we still use it today by mkesper on Tuesday May 02, 2017 @09:20AM (#14246089)
Before databases you had only flat files or VSAM (and before that, ISAM) for your data. Good luck debugging your data in a VSAM cluster. https://en.wikipedia.org/wiki/VSAM
Re:SQL is 43 years old – Here’s why we still use it today by brudgers on Tuesday May 02, 2017 @10:14AM (#14246461)
My apologies for not being clear. By "data structures" I meant data structures and by implication that COBOL provided an abstraction over bytes in memory or on persistent storage media. The premise of my statement is that SQL is also an abstraction over bytes in memory or on persistent storage media in similar use cases.
Re:SQL is 43 years old – Here’s why we still use it today by SmellTheGlove on Tuesday May 02, 2017 @10:01AM (#14246356)
Thankfully, SAS handles VSAM reasonably well. Don't ask me why I know this, or why I need to continue to know this.
Re:SQL is 43 years old – Here’s why we still use it today by 1ris on Tuesday May 02, 2017 @05:27PM (#14250783)
This is not true for any non-trivial database. I had to learn this. Business logic still has to be understood, there is still lot's of domain knowlege how this is indented to be used and how not, and in my case there was still lot's of ambigous, poorly documented VBA-Code to be understood. I'm not aware of a database without a frontend of some kind.
Re:SQL is 43 years old – Here’s why we still use it today by collyw on Tuesday May 02, 2017 @11:19AM (#14247070)
Personally I find SQL to be one of the easiest languages to read (when reading code from other developers).