]> begriffs open source - ai-review/blob - critic/sql-data-integrity.md
frama-c
[ai-review] / critic / sql-data-integrity.md
1 # SQL Data Integrity Critic Framework (ISO/IEC 9075:2023)
2
3 This framework guides the Critic role when evaluating SQL code, database schemas, and data management practices for data integrity, consistency, and reliability. This critic focuses on proper use of constraints, normalization, transactions, and data validation to ensure data correctness and prevent anomalies.
4
5 ## SQL Data Integrity Evaluation Areas
6
7 ### 1. Constraint Management and Enforcement
8 **What to Look For:**
9 - Proper use of primary keys, foreign keys, unique constraints, and check constraints
10 - Correct implementation of referential integrity
11 - Appropriate use of NOT NULL and default constraints
12 - Consistent constraint naming and documentation
13 - Proper constraint validation and error handling
14
15 **Common Problems:**
16 - Missing or incorrect primary/foreign key constraints
17 - Inconsistent constraint enforcement
18 - Lack of appropriate NOT NULL constraints
19 - Poor constraint naming conventions
20 - Missing constraint validation in application code
21
22 **Evaluation Questions:**
23 - Are all tables defined with appropriate primary keys?
24 - Are foreign key relationships enforced and consistent?
25 - Are constraints used to prevent invalid data states?
26 - Are constraint violations handled gracefully?
27 - Are all constraints properly documented and named?
28
29 ### 2. Normalization and Schema Design
30 **What to Look For:**
31 - Appropriate level of normalization for the use case
32 - Prevention of data anomalies (insertion, deletion, update anomalies)
33 - Efficient schema design that balances normalization with performance
34 - Proper handling of denormalization when necessary
35 - Clear documentation of normalization decisions
36
37 **Common Problems:**
38 - Lack of normalization leading to data redundancy
39 - Over-normalization causing performance issues
40 - Inconsistent normalization across related tables
41 - Missing documentation of denormalization rationale
42 - Poor handling of many-to-many relationships
43
44 **Evaluation Questions:**
45 - Is normalization applied appropriately for the use case?
46 - Are data anomalies prevented through proper schema design?
47 - Is the schema design efficient and maintainable?
48 - Are denormalization decisions documented and justified?
49 - Are many-to-many relationships handled correctly?
50
51 ### 3. Transaction Management
52 **What to Look For:**
53 - Proper use of transactions to ensure atomicity and consistency
54 - Appropriate isolation levels for different operations
55 - Consistent transaction boundaries and error handling
56 - Proper handling of long-running transactions
57 - Resource cleanup and rollback on errors
58
59 **Common Problems:**
60 - Inconsistent or missing transaction boundaries
61 - Inappropriate isolation levels causing concurrency issues
62 - Long-running transactions blocking other operations
63 - Missing rollback on error conditions
64 - Resource leaks in transaction handling
65
66 **Evaluation Questions:**
67 - Are transactions used to maintain data integrity?
68 - Are appropriate isolation levels chosen for operations?
69 - Are transaction boundaries clearly defined?
70 - Is error handling and rollback implemented properly?
71 - Are long-running transactions avoided or managed?
72
73 ### 4. Data Validation and Quality
74 **What to Look For:**
75 - Comprehensive input validation and data type checking
76 - Proper handling of NULL values and edge cases
77 - Data quality checks and business rule enforcement
78 - Consistent data format and encoding standards
79 - Regular data integrity audits and monitoring
80
81 **Common Problems:**
82 - Insufficient input validation leading to data corruption
83 - Inconsistent NULL handling across the application
84 - Missing business rule enforcement
85 - Poor data format standardization
86 - Lack of data quality monitoring
87
88 **Evaluation Questions:**
89 - Are all inputs validated before database operations?
90 - Is NULL handling consistent and appropriate?
91 - Are business rules enforced at the database level?
92 - Is data format standardized across the system?
93 - Are data quality issues monitored and addressed?
94
95 ### 5. Referential Integrity
96 **What to Look For:**
97 - Proper foreign key constraint definitions
98 - Appropriate cascade options for updates and deletes
99 - Handling of orphaned records and referential integrity violations
100 - Consistent referential integrity across related tables
101 - Proper documentation of relationship constraints
102
103 **Common Problems:**
104 - Missing or incorrect foreign key constraints
105 - Inappropriate cascade options causing unintended deletions
106 - Orphaned records due to poor referential integrity
107 - Inconsistent relationship definitions
108 - Missing documentation of relationship constraints
109
110 **Evaluation Questions:**
111 - Are all foreign key relationships properly defined?
112 - Are cascade options appropriate for the business logic?
113 - Are orphaned records prevented or handled?
114 - Is referential integrity consistent across the schema?
115 - Are relationship constraints clearly documented?
116
117 ## ISO/IEC Standards-Specific Criticism Process
118
119 ### Step 1: Constraint Analysis
120 1. **Audit Constraints**: Are primary, foreign, and unique keys used appropriately?
121 2. **Check Constraint Enforcement**: Are constraints properly enforced and validated?
122 3. **Evaluate Constraint Naming**: Are constraints named consistently and clearly?
123 4. **Assess Constraint Documentation**: Are constraints properly documented?
124
125 ### Step 2: Normalization Assessment
126 1. **Check Normalization Level**: Is the schema normalized appropriately?
127 2. **Evaluate Data Anomalies**: Are insertion, deletion, and update anomalies prevented?
128 3. **Assess Denormalization**: Are denormalization decisions justified and documented?
129 4. **Review Schema Efficiency**: Is the schema design efficient and maintainable?
130
131 ### Step 3: Transaction Analysis
132 1. **Check Transaction Boundaries**: Are transaction boundaries clearly defined?
133 2. **Evaluate Isolation Levels**: Are appropriate isolation levels chosen?
134 3. **Assess Error Handling**: Is error handling and rollback implemented?
135 4. **Review Resource Management**: Are resources properly managed in transactions?
136
137 ### Step 4: Data Quality Assessment
138 1. **Check Input Validation**: Are all inputs validated before database operations?
139 2. **Evaluate NULL Handling**: Is NULL handling consistent and appropriate?
140 3. **Assess Business Rules**: Are business rules enforced at the database level?
141 4. **Review Data Monitoring**: Is data quality monitored and addressed?
142
143 ## ISO/IEC Standards-Specific Criticism Guidelines
144
145 ### Focus on Data Integrity
146 **Good Criticism:**
147 - "The lack of a foreign key constraint allows orphaned records, violating referential integrity."
148 - "Missing NOT NULL constraint on critical field allows invalid data states."
149 - "The transaction boundaries are not clearly defined, risking data inconsistency."
150 - "The schema is not normalized, leading to data redundancy and update anomalies."
151
152 **Poor Criticism:**
153 - "This might cause data problems."
154 - "The data could be inconsistent."
155 - "This is not safe."
156
157 ### Emphasize Constraint and Transaction Management
158 **Good Criticism:**
159 - "The foreign key constraint is missing, allowing referential integrity violations."
160 - "The transaction does not handle rollback on error, risking partial updates."
161 - "The isolation level is too permissive for this critical operation."
162 - "The constraint naming is inconsistent, making maintenance difficult."
163
164 **Poor Criticism:**
165 - "This is not robust."
166 - "This could fail."
167 - "This is unreliable."
168
169 ## ISO/IEC Standards-Specific Problem Categories
170
171 ### Constraint Problems
172 - **Missing Constraints**: Lack of primary, foreign, or unique keys
173 - **Incorrect Constraints**: Wrong constraint types or definitions
174 - **Poor Constraint Naming**: Inconsistent or unclear constraint names
175 - **Missing Validation**: Lack of constraint validation in application code
176
177 ### Normalization Problems
178 - **Poor Normalization**: Schema design leading to data anomalies
179 - **Over-Normalization**: Excessive normalization causing performance issues
180 - **Inconsistent Normalization**: Different normalization levels across related tables
181 - **Missing Documentation**: Lack of documentation for normalization decisions
182
183 ### Transaction Problems
184 - **Missing Transactions**: Operations not wrapped in transactions
185 - **Inappropriate Isolation**: Wrong isolation levels for operations
186 - **Poor Error Handling**: Missing rollback or error handling in transactions
187 - **Resource Leaks**: Uncommitted transactions or open cursors
188
189 ### Data Quality Problems
190 - **Insufficient Validation**: Lack of input validation leading to data corruption
191 - **Inconsistent NULL Handling**: Different NULL handling across the application
192 - **Missing Business Rules**: Lack of business rule enforcement
193 - **Poor Data Format**: Inconsistent data format and encoding
194
195 ### Referential Integrity Problems
196 - **Missing Foreign Keys**: Lack of foreign key constraints
197 - **Inappropriate Cascades**: Wrong cascade options for updates/deletes
198 - **Orphaned Records**: Records without valid parent references
199 - **Inconsistent Relationships**: Different relationship definitions across tables
200
201 ## ISO/IEC Standards-Specific Criticism Templates
202
203 ### For Constraint Issues
204 ```
205 Constraint Issue: [Specific constraint problem]
206 Problem: [What makes this constraint inadequate or incorrect]
207 Impact: [Potential data integrity violations or inconsistencies]
208 Evidence: [Specific schema or constraint examples]
209 Priority: [Critical/High/Medium/Low]
210 ```
211
212 ### For Normalization Issues
213 ```
214 Normalization Issue: [Specific normalization problem]
215 Problem: [What makes this schema design problematic]
216 Impact: [Potential data anomalies or performance issues]
217 Evidence: [Specific schema design examples]
218 Priority: [Critical/High/Medium/Low]
219 ```
220
221 ### For Transaction Issues
222 ```
223 Transaction Issue: [Specific transaction problem]
224 Problem: [What makes this transaction handling inadequate]
225 Impact: [Potential data inconsistency or resource issues]
226 Evidence: [Specific transaction code examples]
227 Priority: [Critical/High/Medium/Low]
228 ```
229
230 ### For Data Quality Issues
231 ```
232 Data Quality Issue: [Specific data quality problem]
233 Problem: [What makes this data handling inadequate]
234 Impact: [Potential data corruption or inconsistency]
235 Evidence: [Specific data handling examples]
236 Priority: [Critical/High/Medium/Low]
237 ```
238
239 ### For Referential Integrity Issues
240 ```
241 Referential Integrity Issue: [Specific referential integrity problem]
242 Problem: [What makes this relationship definition inadequate]
243 Impact: [Potential orphaned records or data inconsistency]
244 Evidence: [Specific relationship examples]
245 Priority: [Critical/High/Medium/Low]
246 ```
247
248 ## ISO/IEC Standards-Specific Criticism Best Practices
249
250 ### Do's
251 - **Focus on Data Integrity**: Prioritize data correctness and consistency
252 - **Emphasize Constraints**: Ensure proper use of database constraints
253 - **Consider Transactions**: Evaluate transaction boundaries and error handling
254 - **Assess Normalization**: Review schema design for data anomalies
255 - **Document Decisions**: Clearly document constraint and design decisions
256
257 ### Don'ts
258 - **Ignore Constraint Violations**: Don't overlook missing or incorrect constraints
259 - **Accept Data Anomalies**: Don't tolerate poor normalization or data redundancy
260 - **Skip Transaction Management**: Don't ignore transaction boundaries and error handling
261 - **Overlook Data Quality**: Don't accept poor data validation or NULL handling
262 - **Forget Documentation**: Don't skip documentation of integrity decisions
263
264 ## ISO/IEC Standards-Specific Criticism Checklist
265
266 ### Constraint Assessment
267 - [ ] Are all tables defined with appropriate primary keys?
268 - [ ] Are foreign key relationships enforced and consistent?
269 - [ ] Are constraints used to prevent invalid data states?
270 - [ ] Are constraint violations handled gracefully?
271 - [ ] Are all constraints properly documented and named?
272
273 ### Normalization Assessment
274 - [ ] Is normalization applied appropriately for the use case?
275 - [ ] Are data anomalies prevented through proper schema design?
276 - [ ] Is the schema design efficient and maintainable?
277 - [ ] Are denormalization decisions documented and justified?
278 - [ ] Are many-to-many relationships handled correctly?
279
280 ### Transaction Assessment
281 - [ ] Are transactions used to maintain data integrity?
282 - [ ] Are appropriate isolation levels chosen for operations?
283 - [ ] Are transaction boundaries clearly defined?
284 - [ ] Is error handling and rollback implemented properly?
285 - [ ] Are long-running transactions avoided or managed?
286
287 ### Data Quality Assessment
288 - [ ] Are all inputs validated before database operations?
289 - [ ] Is NULL handling consistent and appropriate?
290 - [ ] Are business rules enforced at the database level?
291 - [ ] Is data format standardized across the system?
292 - [ ] Are data quality issues monitored and addressed?
293
294 ### Referential Integrity Assessment
295 - [ ] Are all foreign key relationships properly defined?
296 - [ ] Are cascade options appropriate for the business logic?
297 - [ ] Are orphaned records prevented or handled?
298 - [ ] Is referential integrity consistent across the schema?
299 - [ ] Are relationship constraints clearly documented?
300
301 ## ISO/IEC Standards-Specific Evaluation Questions
302
303 ### For Any SQL Code
304 1. **Are all data integrity constraints properly defined and enforced?**
305 2. **Is the schema normalized appropriately to prevent data anomalies?**
306 3. **Are transactions used to ensure atomicity and consistency?**
307 4. **Is input validation implemented to prevent data corruption?**
308 5. **Are referential integrity relationships properly maintained?**
309 6. **Is NULL handling consistent and appropriate?**
310 7. **Are business rules enforced at the database level?**
311 8. **Is data quality monitored and maintained?**
312 9. **Are constraint violations handled gracefully?**
313 10. **Is the schema design efficient and maintainable?**
314
315 ### For Database Schemas
316 1. **Are all tables defined with appropriate primary keys?**
317 2. **Are foreign key relationships enforced and consistent?**
318 3. **Is normalization applied appropriately for the use case?**
319 4. **Are constraints used to prevent invalid data states?**
320 5. **Are many-to-many relationships handled correctly?**
321
322 ### For Stored Procedures and Triggers
323 1. **Are all inputs validated before database operations?**
324 2. **Are transactions used to ensure atomicity and consistency?**
325 3. **Is error handling and rollback implemented properly?**
326 4. **Are business rules enforced consistently?**
327 5. **Is resource cleanup performed on all code paths?**
328
329 ### For Data Operations
330 1. **Are all operations wrapped in appropriate transactions?**
331 2. **Are isolation levels chosen appropriately for operations?**
332 3. **Is error handling implemented for constraint violations?**
333 4. **Are data quality checks performed before operations?**
334 5. **Is referential integrity maintained during operations?**
335
336 ## SQL Data Integrity Principles Applied
337
338 ### "Ensure Data Consistency and Reliability"
339 - Use constraints to prevent invalid data states
340 - Implement proper normalization to avoid data anomalies
341 - Maintain referential integrity across all relationships
342
343 ### "Maintain Transaction Integrity"
344 - Use transactions to ensure atomicity and consistency
345 - Implement proper error handling and rollback
346 - Choose appropriate isolation levels for operations
347
348 ### "Validate and Monitor Data Quality"
349 - Implement comprehensive input validation
350 - Handle NULL values and edge cases consistently
351 - Monitor and maintain data quality standards
352
353 ### "Document and Enforce Business Rules"
354 - Enforce business rules at the database level
355 - Document constraint and relationship decisions
356 - Maintain consistent data format and encoding