How do I enforce data integrity rules in my database?

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

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

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

    Have a unique table for each type of node.

    Why not just make the class you're building enforce the data integrity for its own type?


    EDIT

    In that case, you can either a) use logical constraints (see below) or b) stored procedures to do inserts/edits (a good idea regardless) or c) again, just make the class enforce data integrity.

    A mixture of C & B would be the course of events I take. I would have unique stored procedures for add/edits for each node type (i.e. Insert_Update_NodeType) as well as make the class perform data validation before saving data.

    0 讨论(0)
  • 2021-01-20 05:02

    I am not that familiar with SQL Server, but I know with Oracle you can specify Constraints that you could use to do what you are looking for. I am pretty sure you can define constraints in SQL server also though.

    EDIT: I found this link that seems to have a lot information, kind of long but may be worth a read.

    0 讨论(0)
  • 2021-01-20 05:07

    You can set up some insert/update triggers. Just check if these fields are null or notnull, and reject insert/update operation if needed. This is a good solution if you want to store all the data in the same table.

    You can create also create a unique table for each classes as well.

    0 讨论(0)
  • 2021-01-20 05:07

    Have you tried NHibernate? It's much more matured product than Entity Framework. It's free.

    0 讨论(0)
  • 2021-01-20 05:11

    Use CHECK constraints on the table. These allow you to use any kind of boolean logic (including on other values in the table) to allow/reject the data.

    From the Books Online site:

    You can create a CHECK constraint with any logical (Boolean) expression that returns TRUE or FALSE based on the logical operators. For the previous example, the logical expression is: salary >= 15000 AND salary <= 100000.

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