list_foreign_keys
Discover foreign key relationships in SQL Server schemas to understand table dependencies and maintain data integrity.
Instructions
List all foreign key relationships in a schema
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| database | No | Database name (optional) | |
| schema | No | Schema name (optional, defaults to dbo) |
Implementation Reference
- Core handler function that executes SQL query to list foreign key relationships using SQL Server system views.async listForeignKeys(database = null, schema = 'dbo') { let query; if (database) { query = ` SELECT fk.name as foreign_key_name, tp.name as parent_table, cp.name as parent_column, tr.name as referenced_table, cr.name as referenced_column FROM [${database}].sys.foreign_keys fk INNER JOIN [${database}].sys.foreign_key_columns fkc ON fk.object_id = fkc.constraint_object_id INNER JOIN [${database}].sys.tables tp ON fkc.parent_object_id = tp.object_id INNER JOIN [${database}].sys.columns cp ON fkc.parent_object_id = cp.object_id AND fkc.parent_column_id = cp.column_id INNER JOIN [${database}].sys.tables tr ON fkc.referenced_object_id = tr.object_id INNER JOIN [${database}].sys.columns cr ON fkc.referenced_object_id = cr.object_id AND fkc.referenced_column_id = cr.column_id INNER JOIN [${database}].sys.schemas s ON tp.schema_id = s.schema_id WHERE s.name = '${schema}' ORDER BY tp.name, fk.name `; } else { query = ` SELECT fk.name as foreign_key_name, tp.name as parent_table, cp.name as parent_column, tr.name as referenced_table, cr.name as referenced_column FROM sys.foreign_keys fk INNER JOIN sys.foreign_key_columns fkc ON fk.object_id = fkc.constraint_object_id INNER JOIN sys.tables tp ON fkc.parent_object_id = tp.object_id INNER JOIN sys.columns cp ON fkc.parent_object_id = cp.object_id AND fkc.parent_column_id = cp.column_id INNER JOIN sys.tables tr ON fkc.referenced_object_id = tr.object_id INNER JOIN sys.columns cr ON fkc.referenced_object_id = cr.object_id AND fkc.referenced_column_id = cr.column_id INNER JOIN sys.schemas s ON tp.schema_id = s.schema_id WHERE s.name = '${schema}' ORDER BY tp.name, fk.name `; } const result = await this.executeQuery(query, 'list_foreign_keys'); return this.formatResults(result); }
- lib/tools/tool-registry.js:52-61 (registration)Tool registration including name, description, and input schema definition.name: 'list_foreign_keys', description: 'List all foreign key relationships in a schema', inputSchema: { type: 'object', properties: { database: { type: 'string', description: 'Database name (optional)' }, schema: { type: 'string', description: 'Schema name (optional, defaults to dbo)' } } } }
- index.js:275-278 (registration)Server dispatch handler that routes list_foreign_keys tool calls to the DatabaseToolsHandler.case 'list_foreign_keys': return { content: await this.databaseTools.listForeignKeys(args.database, args.schema) };