Phase 4: Data Quality & Governance

Schema validation, null checks & referential integrity

Intermediate ~2 min read
Think of it this way A friendly analogy. Read this if the technical version feels dense. Show Hide

Imagine you're a master builder with super cool LEGO sets. You've got tons of different bricks, figures, and instructions. Your job isn't just to build, but to make sure every single creation is perfect and works exactly how it’s supposed to, whether it's a spaceship or a castle. You want to avoid any wobbly parts or missing pieces that would make your finished model not quite right.

One big part of being a master builder is following the blueprint. Every LEGO set comes with a design, right? It shows you exactly what goes where: 'a red 2x4 brick here,' 'a yellow round piece there.' This is like "schema validation" for your LEGOs. It means checking that when you pick up a piece to put in a specific spot, it’s the right kind of piece. If the blueprint says 'put a small blue square here,' you can’t just put a big green triangle instead. And if it says it should be a blue square, it really has to be a blue square, not a sticker that looks like one. This stops you from building wobbly towers or spaceships that fall apart because you used the wrong parts.

Another super important thing you do is make sure no critical pieces are missing. Think about building a car. The blueprint might say 'put a steering wheel here.' If you just leave that spot empty, the car won't be much fun to drive, right? Or if you're building a figure and the head is supposed to go on top, you can’t just leave the neck empty! This is like a "null check". It means identifying those absolutely necessary spots that must have a piece. If you find an empty spot where a steering wheel or a head must be, you immediately know something is wrong and you need to fix it before the whole model is complete. It prevents your awesome creations from being incomplete or totally useless.

So, by always checking your blueprints and making sure no key pieces are ever missing, you guarantee that everything you build is strong, complete, and exactly as it should be. This means when you’re building your biggest, most complex LEGO city, you can be sure that all the buildings stand tall, all the cars drive, and all the figures are complete and ready for adventure! It’s all about making sure your creations are reliable and ready for anything.

As a data engineer, ensuring data quality is paramount, and a core part of this is through robust data testing and validation. Three fundamental techniques you'll frequently apply are schema validation, null checks, and referential integrity. Schema validation is like having a blueprint for your data; it ensures that the incoming or processed data adheres to a predefined structure—correct column names, expected data types (e.g., an 'age' column should be an integer, not text), and proper formatting. This prevents downstream processing errors, ensures consistency, and helps maintain the reliability of your data pipelines and analytics.

Null checks focus on preventing missing critical information. Many columns in your datasets, such as user_id in a users table or order_date in an orders table, simply cannot be empty (NULL). A missing value in a critical field can break unique constraints, distort aggregations (e.g., counting NULLs as zero or omitting them entirely), or cause application failures. Implementing null checks means identifying these non-nullable columns and actively verifying that they always contain a value, flagging any record where they are missing as a data quality issue.

Finally, referential integrity ensures that relationships between different datasets are maintained accurately. For example, if your orders table has a customer_id column, every customer_id in orders must correspond to an existing customer_id in your customers table. Without referential integrity, you end up with "orphaned" records (e.g., an order for a non-existent customer), leading to inconsistent analytical results and a broken understanding of your business data. Validating referential integrity involves checking these foreign key relationships to ensure that all referenced parent records exist.

Key Takeaways

  • Schema validation guarantees data structure and type consistency.
  • Null checks identify and prevent critical missing data values.
  • Referential integrity maintains accurate relationships between datasets.
  • These checks are foundational for building reliable and trustworthy data pipelines.

Code Example

sql
-- Example: Check for referential integrity violations (orphaned orders)
SELECT
  COUNT(*) AS orphaned_orders_count
FROM
  orders o
LEFT JOIN
  customers c ON o.customer_id = c.customer_id
WHERE
  c.customer_id IS NULL;

-- Example: Check for NULL values in a critical column (product_name)
SELECT
  COUNT(*) AS null_product_names_count
FROM
  products
WHERE
  product_name IS NULL;

How this code works

This code demonstrates two fundamental data validation checks: ensuring referential integrity between related tables and performing NULL checks to identify missing critical data. The first section helps prevent data inconsistencies by finding "orphaned" records, such as an order that refers to a non-existent customer. The second section identifies direct data quality issues where important information, like a product's name, is absent.

The first query tackles referential integrity by using a LEFT JOIN orders o customers c ON o.customer_id = c.customer_id. This statement attempts to link every row from the orders table to a matching row in the customers table. The crucial part for validation is WHERE c.customer_id IS NULL. A LEFT JOIN includes all records from the left table (orders); if an order's customer_id doesn't have a corresponding customer_id in the customers table, then the c.customer_id value for that row will be NULL. This pattern effectively counts orphaned orders. The second query is simpler, directly using WHERE product_name IS NULL on the products table to count any records where the product_name column is empty, highlighting missing essential product details.