0

Thank you for your earlier answers, but based on their feedback I have reformed the question.

If the answer to the question is no, then can the data's integrity for enforced by any other means. I do not consider the use of stored procedures sufficient as they may be circumvented. Would triggers be required?

Duncan3142
  • 371
  • 2
  • 12
  • 3
    Unclear. Do you mean "is there any business logic that can be enforced by RI?" The answer is yes, such business logic exists. Or do you mean "Can all business logic be enforced by RI?" The answer is no, that's an unbounded requirement that could include anything. – Larry Lustig Nov 22 '11 at 13:22
  • I mean can all business logic be enforced by RI? – Duncan3142 Nov 22 '11 at 16:44

5 Answers5

3

No. Some business logic involves calculations. DBMSs can do calculations, of course but that would be 'by use of built-in functions', not referential integrity.

onedaywhen
  • 55,269
  • 12
  • 100
  • 138
David
  • 72,686
  • 18
  • 132
  • 173
  • Thank you. Does this mean that the use of triggers would be required? – Duncan3142 Nov 22 '11 at 17:17
  • Not necessarily. It simply means that foreign keys and check restraints can enforce ***some*** business logic rules, but others may require calculations (such as SUM, AVG), comparisons (WHERE Title= 'Sr. Developer') or code outside of the database compeltely. – David Nov 22 '11 at 17:21
2

A footnote in Date's most recent book, "Database Explorations", answers this question :

"It's worth noting in passing that this fact implies that all possible database constraints can be expressed as INDs".

INDs are "inclusion dependencies", and they are basically the same thing as SQL's foreign keys, but with the limitations imposed by SQL left out.

EDIT

In response to "would triggers be required" : "Applied mathematics for Database Professionals" has a complete dedicated chapter on how to program triggers to enforce really just any arbitrary business rule. That chapter alone makes the book worth the buck.

BTW, sprocs are not circumventable if you use the security system to block out all "direct" access to the tables. Of course you then have to depend on your security rules being defined and managed properly ...

Erwin Smout
  • 18,113
  • 4
  • 33
  • 52
  • Interesting, can you specify what these limitations are? It would seem to me that allowing a foreign key to reference a view would make ensuring data integrity much easier. I appreciate however that this may impose unacceptable limitations on database performance. – Duncan3142 Nov 22 '11 at 17:08
  • 1
    The limitations are that the referencing expression must be a table name (SQL), instead of any arbitrary expression (relationland), that the referenced expression must be a table name (SQL) instead of (for this particular case) a relation literal denoting an empty relation (relationland), and that the set of columns that constitutes the reference, is required to be >= 1 (SQL), instead of possibly also being empty (relationland). – Erwin Smout Nov 22 '11 at 19:04
  • So the thing is, if you interpret the term "RI" in its classical and very strict/rigid SQL sense, then the answer is "no", but if you interpret the term more in the (broadened) sense of modern relational theory, then the answer is "yes". – Erwin Smout Nov 22 '11 at 19:08
  • Was that SQL server tag there when I first answered this question ? No, it wasn't. I hate this. My answer was extremely to-the-point, at least regarding the question as it was asked, which was very clearly not product-oriented, but now the question has changed to being oriented towards a very specific product, making my answer seem completely OT. – Erwin Smout Nov 22 '11 at 19:18
  • My apologies. I've noted that I reformed the question in the brief. That said I appreciate the rigour and depth of your answer. I will investigate DBMSs with richer feature sets based on your answer. – Duncan3142 Nov 23 '11 at 10:03
  • 1
    PS there is a fourth limitation, which is that the set of attributes that constitutes the reference, must be a (super)key in the referenced table. The IND's of relationland do not require this. For example, "courses can only be organized in cities where we have a department" : 'COURSES {CITY} SUBSETOF DEPARTMENT {CITY}' in relationland, lots of pesky trigger code in SQLland, assuming that CITY is not a key to the DEPARTMENT table. – Erwin Smout Nov 23 '11 at 12:59
  • Thanks for the book recommendation. – Duncan3142 Nov 23 '11 at 16:59
2

No. There are lots of business rules that cannot be represented by CHECK constraints and FOREIGN KEY constraints alone. In practice even referential integrity constraint support in SQL is extremely limited.

For example, given two tables called Employee and Department, I can easily enforce a rule that every Employee must be assigned to exactly one Department but I cannot also enforce a rule that every Department must be referenced by at least one Employee. Technically I can create constraints to that effect but then SQL won't allow me to update the tables!

ISO Standard SQL does have a CREATE ASSERTION feature that is supposed to be for general purpose constraint enforcement but most DBMSs don't support it. Even if it were available, the CREATE ASSERTION feature is crippled by SQL's lack of basic support for multiple assignment - you can only update one table at a time. Effective business rule enforcement requires a database model that allows multiple assignment.

nvogel
  • 24,981
  • 1
  • 44
  • 82
1

It would be possible to have all logic at the database level, but not with simply database schema. You can implement it all logic at the database level via stored procedures and triggers, but this can be:

  1. difficult to implement
  2. difficult to maintain
  3. slow as all processing would run on the server and would not be utilizing the client machines' power.

I think you would be far better off implementing business logic via stored procedure, but again, you could be placing a large load on your server (depends on number of clients, transactions, etc).

UnhandledExcepSean
  • 12,504
  • 2
  • 35
  • 51
1

In SQL terms, referential integrity constraints usually means foreign keys.

Perhaps you meant data integrity constraints? If so, we should probably extend your definition to include CREATE ASSERTION and perhaps CREATE DOMAIN. This would allow constraints of arbitrary complexity to be enforced. However, "Any given business logic" is an unreasonable requirement in practice and enforcing every business rule at the DBMS level may be undesirable.

Thank you for your input. I was however specifically concerned with foreign keys and check constraints.

May I ask why? On the face it, it seems arbitrary an arbitrary classification.

It will depend on whether the CHECK constraints you have in mind support subqueries. If yes then this will still allow constraints of arbitrary complexity but will only be triggered on the table being updated i.e. if a CHECK constraints definition involves two tables then a complementary CHECK constraint may be required on the second table.

That said, I don’t know of an industrial-strength SQL product that supports subqueries (the Access Database Engine does but I don't consider it to be industrial-strength). However, many SQL products provide as a workaround that support subqueries (and procedural code plus much more). Perhaps your definition should allow tiggers.

onedaywhen
  • 55,269
  • 12
  • 100
  • 138
  • Thank you for your input. I was however specifically concerned with foreign keys and check constraints. I have clarified this in the question. – Duncan3142 Nov 22 '11 at 17:12
  • Using SQL Server I am unfamiliar with assertions and domains, they seem to be rather desirable. – Duncan3142 Nov 22 '11 at 17:32
  • @Duncan3142: see update to my answer. – onedaywhen Nov 23 '11 at 09:07
  • I appreciate it is arbitrary. I think I was guilty of not being specific enough when I originally asked the question. I would like to be able to use sub-quires in my `CHECK`, but as you say this does not seem to be an option. I wanted to know if it was possible to avoid using triggers as the use of them adds another dimension to the database's complexity. – Duncan3142 Nov 23 '11 at 10:02