Redgate Test Data Manager

Verifying Foreign Keys for Subsetting

Before running subsetting, verify your database has foreign key constraints defined.

Why Foreign Keys Matter

  • Subsetting maintains referential integrity by following foreign key relationships
  • Without FKs, the tool cannot determine which related records to include
  • Missing FKs may result in orphaned records or incomplete data sets

Checking Your Database

Use these queries to verify your database structure:

SQL Server Relationships

-- Check foreign key count by table
SELECT
  SCHEMA_NAME(t.schema_id) AS SchemaName,
  t.name AS TableName,
  COUNT(fk.object_id) AS ForeignKeyCount
FROM sys.tables t
LEFT JOIN sys.foreign_keys fk
  ON t.object_id = fk.parent_object_id
GROUP BY t.schema_id, t.name
ORDER BY ForeignKeyCount DESC, SchemaName ASC, TableName ASC

PostgreSQL Relationships

-- Check foreign key count by table
select
  tc.table_schema as SchemaName,
  tc.table_name AS TableName,
  COUNT(ccu.constraint_name) AS ForeignKeyCount
FROM information_schema.table_constraints tc
LEFT JOIN information_schema.constraint_column_usage ccu 
  ON tc.constraint_name = ccu.constraint_name
WHERE tc.constraint_type = 'FOREIGN KEY'
GROUP by tc.table_schema, tc.table_name
ORDER BY ForeignKeyCount DESC, SchemaName ASC, TableName ASC

Interpreting Results

Good: Most tables show ForeignKeyCount > 0

  • Indicates relationships are defined
  • Subsetting can maintain referential integrity

Warning: Many tables show ForeignKeyCount = 0

  • Tables may be isolated with no relationships
  • Subsetting may not include related data
  • Consider defining manual relationships

If Foreign Keys Are Missing

If your database lacks foreign key constraints but tables are related, you can define manual relationships in the options file.

Manual relationships tell the subsetter how tables connect, even without database-level foreign keys.

See manualRelationships in the subsetting options file reference.

Next Steps

Once you've verified foreign keys:


Didn't find what you were looking for?