Data Loader

 

Testing and Verification of Data After Database Migration

database testing

Database migration is a critical process that requires thorough testing and verification to ensure the accuracy and integrity of the migrated data. After successfully migrating your database from one system to another, it is essential to validate the data to guarantee a seamless transition. This article provides a step-by-step guide on how to test and verify data after a database migration.

Step 1: Define Testing Scope and Objectives

Clearly define the scope of your testing, including the tables, views, stored procedures, and any other relevant database objects that need to be verified. Set specific objectives to ensure that the migrated data meets your expectations in terms of accuracy, completeness, and consistency.

Step 2: Select Sample Data

Identify a representative sample of data from your source database. Choose data from various tables, including different data types, primary key constraints, and relationships. Selecting a diverse sample helps uncover potential issues across different aspects of the database.

Step 3: Compare Data Counts

Start by comparing the total row counts between the source and target databases for each selected table. A mismatch in row counts could indicate missing or duplicated data during the migration process. Use simple SQL queries to retrieve the row counts from both databases and compare them side by side.

dataloader

Step 4: Validate Primary Keys and Unique Constraints

Verify the integrity of primary keys and unique constraints in the migrated data. Run queries to identify duplicate or missing primary key values. Ensure that primary key columns have unique values in the target database and match the corresponding values in the source database.

Here's a detailed instructions on how to validate primary keys and unique constraints in MSSQL and MySQL:

Validating Primary Keys and Unique Constraints in MSSQL: Primary keys and unique constraints ensure the integrity and uniqueness of data in MSSQL databases. To validate them, you can perform the following steps:

Primary Keys:

Check Primary Key Existence: Run the following query to verify that primary keys exist in the tables you want to validate:

SQL
  SELECT t.name AS TableName, c.name AS ColumnName FROM sys.tables AS tINNER
    JOIN sys.columns AS c ON t.object_id = c.object_id
      INNER JOIN sys.key_constraints AS pk ON pk.parent_object_id = t.object_id
                 AND pk.type = 'PK' WHERE t.name = 'YourTableName'   			
				

 Check Duplicate Primary Key Values: Execute the following query to identify duplicate primary key values:

SQL
  SELECT PK_Column, COUNT(*) AS 
	DuplicateCount FROM YourTableName GROUP BY PK_Column HAVING COUNT(*) > 1
				

Validate Referential Integrity: Ensure that foreign keys referencing the primary keys are valid and refer to existing records. You can query the foreign key constraints using system views like sys.foreign_keys and validate the relationships.

 Unique Constraints:

 Check Unique Constraints Existence: Run the following query to verify the existence of unique constraints in the tables you want to validate:

SQL
   SELECT t.name AS TableName, c.name AS ColumnName FROM sys.tables AS 
	tINNER JOIN sys.columns AS c ON t.object_id = c.object_id INNER JOIN 
	sys.index_columns AS ic ON ic.object_id = c.object_id AND 
				      ic.column_id = c.column_id INNER JOIN sys.indexes AS i 
				            ON ic.object_id = i.object_id AND 
	ic.index_id = i.index_id 
            WHERE t.name = 'YourTableName' AND i.is_unique_constraint = 1			

;

Check Duplicate Unique Constraint Values: Execute the following query to identify duplicate values violating unique constraints:

SQL
 	SELECT UniqueColumns,COUNT(*) AS DuplicateCount FROM YourTableName 
               GROUP BY UniqueColumns HAVING COUNT(*) > 1

Validating Primary Keys and Unique Constraints in MySQL: MySQL follows similar principles for validating primary keys and unique constraints. Here's how you can do it:

Primary Keys:

Check Primary Key Existence: Run the following query to verify that primary keys exist in the tables you want to validate:

SQL
 
	 SELECT TABLE_NAME, COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE 
	CONSTRAINT_NAME = 'PRIMARY' AND TABLE_SCHEMA = 'YourDatabaseName'
		

 Check Duplicate Primary Key Values: Execute the following query to identify duplicate primary key values:

SQL
 
	SELECT PK_Column, 
	  COUNT(*) AS DuplicateCount FROM YourTableName GROUP BY PK_Column HAVING 
	COUNT(*) > 1

 Unique Constraints:

 Check Unique Constraints Existence: Run the following query to verify the existence of unique constraints in the tables you want to validate:

SQL
 
     SELECT TABLE_NAME, COLUMN_NAME FROM 
	INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE CONSTRAINT_NAME <> 'PRIMARY' AND 
	  TABLE_SCHEMA = 'YourDatabaseName'

	

 Check Duplicate Unique Constraint Values: Execute the following query to identify duplicate values violating unique constraints:

SQL
 
    SELECT UniqueColumns, 
	COUNT(*) AS DuplicateCount FROM YourTableName GROUP BY UniqueColumns HAVING 
	COUNT(*) > 1



		

 

Remember to replace 'YourTableName' and 'YourDatabaseName' with the appropriate table and database names in the queries above.
By executing these queries and analyzing the results, you can validate primary keys and unique constraints in both MSSQL and MySQL databases, ensuring the integrity and uniqueness of your data.

Step 5: Check Data Consistency

Perform data consistency checks by comparing specific columns or values between the source and target databases. Execute queries to retrieve sample data from both databases and compare them. Look for discrepancies in critical fields such as dates, amounts, or other business-critical attributes.

Step 6: Test Referential Integrity

If your database includes foreign key relationships, validate the referential integrity after migration. Check that the relationships between tables are maintained correctly in the target database. Verify that foreign keys reference valid primary key values and cascading updates or deletes work as expected.

Step 7: Execute Sample Queries and Reports

execute report

Run a set of sample queries and reports that your applications or users typically use. Ensure that the results from these queries match between the source and target databases. Pay attention to complex queries involving joins, aggregations, and calculations.

Step 8: Perform Functionality Testing

Validate the functionality of your applications or systems that rely on the migrated database. Conduct end-to-end testing to ensure that the migrated data works seamlessly with the application. Test common use cases and workflows to verify that the application behaves as expected.

Step 9: Perform Performance Testing

Assess the performance of the migrated database by executing stress tests, load tests, or any other performance testing techniques relevant to your environment. Monitor query response times, transaction throughput, and system resource utilization. Compare the performance metrics against the baseline performance of the source database.

Step 10: Document and Resolve Issues

Document any issues, discrepancies, or anomalies encountered during the testing and verification process. Categorize the issues based on severity and impact. Collaborate with the migration team and application owners to resolve the identified issues promptly. Retest the fixed issues to ensure they have been resolved satisfactorily.

Conclusion:

Thorough testing and verification of data after a database migration are essential to ensure the accuracy and integrity of the migrated data. By following the step-by-step guide outlined in this article, you can systematically validate the migrated data, identify any discrepancies, and address them promptly. Proper testing and verification minimize the risk of data corruption, improve application performance, and provide confidence in the success of your database migration process.

 

Back to : Step by Step Guide on Migrating MSSQL to MySQL

You might also be interested in:

Top 3 Tools for migrating MS SQL Server to MySQL