How do I enforce data integrity rules in my database?

前端 未结 11 1309
滥情空心
滥情空心 2021-01-20 04:07

I\'m designing this collection of classes and abstract (MustInherit) classes…

相关标签:
11条回答
  • 2021-01-20 04:46

    Stephen's answer is the best. But if you MUST, you could add a check constraint the HtmlOrCode column and the other columns which need to change.

    0 讨论(0)
  • 2021-01-20 04:50

    Personally I always insist on putting data integrity code on the table itself either via a trigger or a check constraint. The reason why is that you cannot guarantee that only the user interface will update insert or delete records. Nor can you guarantee that someone might not write a second sp to get around the constraints in the orginal sp without understanding the actual data integrity rules or even write it because he or she is unaware of the existence of the sp with the rules. Tables are often affected by DTS or SSIS packages, dynamic queries from the user interface or through Query analyzer or the query window, or even by scheduled jobs that run code. If you do not put the data integrity code at the table level, sooner or later your data will not have integrity.

    0 讨论(0)
  • 2021-01-20 04:50

    It's probably not the answer you want to hear, but the best way to avoid logical inconsistencies, you really want to look at database normalisation

    0 讨论(0)
  • 2021-01-20 04:51

    SQL Server doesn't know anything about your classes. I think that you'll have to enforce this by using a Factory class that constructs/deconstructs all these for you and makes sure that you're passing the right values depending upon the type.

    Technically this is not "enforcing the rules in the database" but I don't think that this can be done in a single table. Fields either accept nulls or they don't.

    Another idea could be to explore SQL Functions and Stored Procedures that do the same thing. BUt you cannot enforce a field to be NOT NULL for one record and NULL for the next one. That's your Business Layer / Factory job.

    0 讨论(0)
  • 2021-01-20 04:53

    Enforcing Data Integrity in Databases Basically, there are four primary types of data integrity: entity, domain, referential and user-defined.

    Entity integrity applies at the row level; domain integrity applies at the column level, and referential integrity applies at the table level.

    1. Entity Integrity ensures a table does not have any duplicate rows and is uniquely identified.

    2. Domain Integrity requires that a set of data values fall within a specific range (domain) in order to be valid. In other words, domain integrity defines the permissible entries for a given column by restricting the data type, format, or range of possible values.

    3. Referential Integrity is concerned with keeping the relationships between tables synchronized.

    @Zack: You can also check out this blog to read more details about data integrity enforcement, here- https://www.bugraptors.com/what-is-data-integrity/

    0 讨论(0)
  • 2021-01-20 04:54

    It looks like you are attempting the Single Table Inheritance pattern, this is a pattern covered by the Object-Relational Structural Patterns section of the book Patterns of Enterprise Application Architecture.

    I would recommend the Class Table Inheritance or Concrete Table Inheritance patterns if you wish to enforce data integrity via SQL table constraints.

    Though it wouldn't be my first suggestion, you could still use Single Table Inheritance and just enforce the constraints via a Stored Procedure.

    0 讨论(0)
提交回复
热议问题