Data Integrity — the subtle bugs from schema changes

Understanding data integrity can prevent costly bugs in production and help you excel in technical interviews.

When developers change database schemas or application configurations, subtle bugs can stealthily emerge if data integrity isn't preserved. For instance, consider a scenario in which you alter a table to add a column for user preferences without properly managing the existing rows. What happens when the application tries to read from this column on older records? This is where data integrity comes in, and it’s critical for both interviews and production work.

The Core of Data Integrity

Data integrity refers to the accuracy, consistency, and reliability of data throughout its lifecycle. It encompasses various paradigms including:

  • Entity Integrity: Ensures that each row in a table is unique, typically enforced through primary keys.
  • Referential Integrity: Ensures that relationships between tables remain consistent, often enforced through foreign keys.
  • Domain Integrity: Ensures that values in a column meet certain criteria or constraints.

Consider the following SQL code snippet that illustrates the key concepts:

CREATE TABLE Users (
    UserID INT PRIMARY KEY,
    Username VARCHAR(255) UNIQUE NOT NULL,
    Email VARCHAR(255) NOT NULL,
    CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    UpdatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

CREATE TABLE Posts (
    PostID INT PRIMARY KEY,
    UserID INT,
    Content TEXT,
    FOREIGN KEY (UserID) REFERENCES Users(UserID) ON DELETE CASCADE
);

In this example:

  • Each user must have a unique UserID (Entity Integrity).
  • The Posts table relies on UserID to ensure that posts are associated with existing users (Referential Integrity).

Common Interview Traps

During interviews, candidates might struggle with the following points concerning data integrity:

  • Misunderstanding Database Types: Candidates may confuse when to use relational versus NoSQL databases, not realizing that relational databases offer strong schema enforcement and constraints that ensure data integrity, especially useful in applications where transactions occur.
  • Ignoring Constraints: Interviewers might present scenarios where failures occur due to missing constraints. Candidates should articulate the implications of not using foreign keys, unique constraints, etc., leading to orphan records or duplicate entries.
  • Overlooking Data Validation: There is often a lack of emphasis on input sanitation and validation within applications. This can lead to broken integrity when applications assume data is in a valid state.

Worked Example

Imagine you’re working for a startup developing a new blogging platform. Your team decides to switch to a new relational database as user engagement grows. As part of the migration, an engineer added a new AuthorEmail column to the Posts table for better tracking.

Step-by-step Approach:

  1. Designing the Schema: The engineer adds AuthorEmail without ensuring that it is tied to existing entries. When new posts are created, the application expects this field to be filled out.
  2. Data Migration: As posts are migrated over, existing data has NULL in AuthorEmail because this was not captured in legacy systems.
  3. Application Logic Failure: Later, when the application tries to fetch posts for emails sent out in user notifications, it hits a NULL value for many entries causing a runtime error.
  4. Inadequate Testing: Automated tests might not capture this edge case when validating the application’s behavior with the new schema.
  5. Resolution: Implement a migration script that accurately fills in default values or refactors the application logic to handle NULLs gracefully, reinforcing the use of constraints to ensure future integrity.

Through this iterative process, the team also learned to implement robust migrations for future schema changes to prevent integrity issues.

On the Job: Real-life Implications of Data Integrity

In production settings, maintaining data integrity is not merely an architectural concern; it’s a business necessity. Every time a developer introduces a new feature that interacts with data, they run the risk of breaking data integrity if they do not respect existing schemas and relationships.

  • Error Handling: Teams need to develop a habit of applying validation checks right at the application layer and enforcing these checks persistently.
  • Documentation: Poorly documented schemas lead to misunderstanding among team members, increasing the chances of data integrity issues.
  • Testing Frameworks: Many organizations have adopted continuous integration (CI) that includes testing for data integrity issues as part of their deployment pipelines, which helps catch potential problems before they reach production.

Being well-versed in data integrity helps candidates not only to answer questions during interviews but also to architect systems that are robust and dependable in the long run.

References

Practice

Ready to practice Data Integrity?

Answer real questions, get instant feedback, and watch your skill score climb — free. Practice is in English, like real tech interviews.

Try one 👇

Data IntegrityMid
0 XP
Which of the following is an advantage of using `collections.defaultdict` over a standard dictionary in Python?

↑ Go ahead — pick an answer. This is Skillpato.

Keep learning