How do I enforce data integrity rules in my database?
Asked Answered
C

10

3

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

class diagram

This is the database table where I'm going to store all this…

database table

As far as the Microsoft SQL Server database knows, those are all nullable ("Allow Nulls") columns.

But really, that depends on the class stored there: LinkNode, HtmlPageNode, or CodePageNode.

Rules might look like this...

rules table

How do I enforce such data integrity rules within my database?


UPDATE: Regarding this single-table design...

I'm still trying to zero in on a final architecture.

I initially started with many small tables with almost zero nullalbe fields.
Which is the best database schema for my navigation?

And I learned about the LINQ to SQL IsDiscriminator property.
What’s the best way to handle one-to-one relationships in SQL?

But then I learned that LINQ to SQL only supports single table inheritance.
Can a LINQ to SQL IsDiscriminator column NOT inherit?

Now I'm trying to handle it with a collection of classes and abstract classes.
Please help me with my .NET abstract classes.

Charbonnier answered 17/9, 2008 at 18:32 Comment(0)
R
2

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.

Ride answered 17/9, 2008 at 18:46 Comment(0)
P
2

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.

Pelecypod answered 17/9, 2008 at 18:35 Comment(0)
R
2

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.

Ride answered 17/9, 2008 at 18:46 Comment(0)
T
2

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.

Tumblebug answered 17/9, 2008 at 18:47 Comment(0)
C
2

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.

Countervail answered 17/9, 2008 at 18:50 Comment(0)
K
2

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.

Knowling answered 17/9, 2008 at 22:17 Comment(1)
I agree. Enforcing data integrity at the lowest level is a form of future-proofing.Charbonnier
C
1

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

Cullet answered 17/9, 2008 at 18:38 Comment(0)
H
1

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.

Hyalite answered 17/9, 2008 at 18:40 Comment(0)
W
1

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.

Widner answered 17/9, 2008 at 18:46 Comment(0)
P
0

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.

Provence answered 17/9, 2008 at 18:38 Comment(0)
C
0

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

Countervail answered 17/9, 2008 at 19:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.