{"id":657,"date":"2010-10-07T09:40:00","date_gmt":"2010-10-07T09:40:00","guid":{"rendered":"\/blogs\/paul\/post\/Programmatically-picking-apart-foreign-key-constraints.aspx"},"modified":"2010-10-07T09:40:00","modified_gmt":"2010-10-07T09:40:00","slug":"programmatically-picking-apart-foreign-key-constraints","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/paul\/programmatically-picking-apart-foreign-key-constraints\/","title":{"rendered":"Programmatically picking apart foreign-key constraints"},"content":{"rendered":"<p>\n<font face=\"verdana,geneva\" size=\"2\">Another quickie today. Don&#39;t ask why, but this morning I was trying to programmatically work out which tables have foreign-key references to table <font face=\"courier new,courier\">Sales<\/font>, because they&#39;re preventing fast partition switching on table <font face=\"courier new,courier\">Sales<\/font>.<\/font>\n<\/p>\n<p>\n<font face=\"verdana,geneva\" size=\"2\">After more spelunking around I thought I had it with this code:<\/font>\n<\/p>\n<blockquote>\n<p>\n\t<font face=\"courier new,courier\" size=\"2\"><font color=\"#0000ff\">SELECT <\/font><font color=\"#808080\">* <\/font><font color=\"#0000ff\">FROM <\/font><font color=\"#008000\">sys<\/font><font color=\"#808080\">.<\/font><font color=\"#008000\">all_objects<\/font><br \/>\n\t<font color=\"#0000ff\">WHERE<\/font><font color=\"#000000\"> [type] <\/font><font color=\"#808080\">= <\/font><font color=\"#ff0000\">&#39;F&#39; <\/font><font color=\"#808080\">AND<\/font><font color=\"#000000\"> [parent_object_id] <\/font><font color=\"#808080\">= <\/font><font color=\"#ff00ff\">OBJECT_ID <\/font><font color=\"#808080\">(<\/font><font color=\"#ff0000\">&#39;Sales&#39;);<\/font><br \/>\n\t<font color=\"#0000ff\">GO<\/font> <\/font>\n\t<\/p>\n<\/blockquote>\n<p>\n<font face=\"verdana,geneva\" size=\"2\">But that gives me the foreign-key references FROM the <font face=\"courier new,courier\">Sales<\/font> table, not TO the <font face=\"courier new,courier\">Sales<\/font> table.<\/font>\n<\/p>\n<p>\n<font face=\"verdana,geneva\" size=\"2\">Eventually I found the <font face=\"courier new,courier\">sys.foreign_key_columns<\/font> table which does the trick, using the following code:<\/font>\n<\/p>\n<blockquote>\n<p>\n\t<font face=\"courier new,courier\" size=\"2\"><font color=\"#0000ff\">SELECT <\/font><font color=\"#ff00ff\">OBJECT_NAME <\/font><font color=\"#808080\">(<\/font><font color=\"#000000\">[parent_object_id]<\/font><font color=\"#808080\">)<\/font> <font color=\"#0000ff\">AS<\/font> <font color=\"#ff0000\">&#39;Referencing Table&#39;<\/font><font color=\"#808080\">, * <\/font><font color=\"#0000ff\">FROM <\/font><font color=\"#008000\">sys<\/font><font color=\"#808080\">.<\/font><font color=\"#008000\">foreign_key_columns<\/font><br \/>\n\t<font color=\"#0000ff\">WHERE <\/font><font color=\"#000000\">[referenced_object_id] <\/font><font color=\"#808080\">= <\/font><font color=\"#ff00ff\">OBJECT_ID <\/font><font color=\"#808080\">(<\/font><font color=\"#ff0000\">&#39;Sales&#39;<\/font><font color=\"#808080\">);<\/font><br \/>\n\t<font color=\"#0000ff\">GO<\/font> <\/font>\n\t<\/p>\n<\/blockquote>\n<p>\n<font face=\"verdana,geneva\" size=\"2\">And&nbsp;it also gives me the column IDs in both tables involved in the constraint.&nbsp;<\/font>\n<\/p>\n<p>\n<font face=\"verdana,geneva\" size=\"2\">Enjoy!<\/font><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Another quickie today. Don&#39;t ask why, but this morning I was trying to programmatically work out which tables have foreign-key references to table Sales, because they&#39;re preventing fast partition switching on table Sales. After more spelunking around I thought I had it with this code: SELECT * FROM sys.all_objects WHERE [type] = &#39;F&#39; AND [parent_object_id] [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28],"tags":[],"class_list":["post-657","post","type-post","status-publish","format-standard","hentry","category-constraints"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Programmatically picking apart foreign-key constraints - Paul S. Randal<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqlskills.com\/blogs\/paul\/programmatically-picking-apart-foreign-key-constraints\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Programmatically picking apart foreign-key constraints - Paul S. Randal\" \/>\n<meta property=\"og:description\" content=\"Another quickie today. Don&#039;t ask why, but this morning I was trying to programmatically work out which tables have foreign-key references to table Sales, because they&#039;re preventing fast partition switching on table Sales. After more spelunking around I thought I had it with this code: SELECT * FROM sys.all_objects WHERE [type] = &#039;F&#039; AND [parent_object_id] [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/paul\/programmatically-picking-apart-foreign-key-constraints\/\" \/>\n<meta property=\"og:site_name\" content=\"Paul S. Randal\" \/>\n<meta property=\"article:published_time\" content=\"2010-10-07T09:40:00+00:00\" \/>\n<meta name=\"author\" content=\"Paul Randal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Paul Randal\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/programmatically-picking-apart-foreign-key-constraints\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/programmatically-picking-apart-foreign-key-constraints\/\",\"name\":\"Programmatically picking apart foreign-key constraints - Paul S. Randal\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#website\"},\"datePublished\":\"2010-10-07T09:40:00+00:00\",\"author\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/programmatically-picking-apart-foreign-key-constraints\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/paul\/programmatically-picking-apart-foreign-key-constraints\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/programmatically-picking-apart-foreign-key-constraints\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Programmatically picking apart foreign-key constraints\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#website\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/\",\"name\":\"Paul S. Randal\",\"description\":\"In Recovery...\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce\",\"name\":\"Paul Randal\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/0b6a266bba2f088f2551ef529293001bd73bf026bc1908b9866728c062beeeb6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/0b6a266bba2f088f2551ef529293001bd73bf026bc1908b9866728c062beeeb6?s=96&d=mm&r=g\",\"caption\":\"Paul Randal\"},\"sameAs\":[\"http:\/\/3.209.169.194\/blogs\/paul\"],\"url\":\"https:\/\/www.sqlskills.com\/blogs\/paul\/author\/paul\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Programmatically picking apart foreign-key constraints - Paul S. Randal","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.sqlskills.com\/blogs\/paul\/programmatically-picking-apart-foreign-key-constraints\/","og_locale":"en_US","og_type":"article","og_title":"Programmatically picking apart foreign-key constraints - Paul S. Randal","og_description":"Another quickie today. Don&#39;t ask why, but this morning I was trying to programmatically work out which tables have foreign-key references to table Sales, because they&#39;re preventing fast partition switching on table Sales. After more spelunking around I thought I had it with this code: SELECT * FROM sys.all_objects WHERE [type] = &#39;F&#39; AND [parent_object_id] [&hellip;]","og_url":"https:\/\/www.sqlskills.com\/blogs\/paul\/programmatically-picking-apart-foreign-key-constraints\/","og_site_name":"Paul S. Randal","article_published_time":"2010-10-07T09:40:00+00:00","author":"Paul Randal","twitter_misc":{"Written by":"Paul Randal","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/programmatically-picking-apart-foreign-key-constraints\/","url":"https:\/\/www.sqlskills.com\/blogs\/paul\/programmatically-picking-apart-foreign-key-constraints\/","name":"Programmatically picking apart foreign-key constraints - Paul S. Randal","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#website"},"datePublished":"2010-10-07T09:40:00+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce"},"breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/programmatically-picking-apart-foreign-key-constraints\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/paul\/programmatically-picking-apart-foreign-key-constraints\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/programmatically-picking-apart-foreign-key-constraints\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlskills.com\/blogs\/paul\/"},{"@type":"ListItem","position":2,"name":"Programmatically picking apart foreign-key constraints"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#website","url":"https:\/\/www.sqlskills.com\/blogs\/paul\/","name":"Paul S. Randal","description":"In Recovery...","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlskills.com\/blogs\/paul\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/ffcec826c18782e1e0adf173826a7fce","name":"Paul Randal","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlskills.com\/blogs\/paul\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/0b6a266bba2f088f2551ef529293001bd73bf026bc1908b9866728c062beeeb6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/0b6a266bba2f088f2551ef529293001bd73bf026bc1908b9866728c062beeeb6?s=96&d=mm&r=g","caption":"Paul Randal"},"sameAs":["http:\/\/3.209.169.194\/blogs\/paul"],"url":"https:\/\/www.sqlskills.com\/blogs\/paul\/author\/paul\/"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/posts\/657","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/comments?post=657"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/posts\/657\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/media?parent=657"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/categories?post=657"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/paul\/wp-json\/wp\/v2\/tags?post=657"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}