Search code examples
sqlsql-serverdatabase-designreferential-integrity

Can a SQL Server database schema always enforce its application's business logic by the use of foreign keys and check constraints alone?


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?


Solution

  • 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.