{"id":347,"date":"2013-11-08T20:18:33","date_gmt":"2013-11-08T12:18:33","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=347"},"modified":"2018-09-04T13:15:31","modified_gmt":"2018-09-04T05:15:31","slug":"sql-query-to-find-foreign-key-constraints-that-reference-columns-in-a-table-in-oracle-database","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/sql-query-to-find-foreign-key-constraints-that-reference-columns-in-a-table-in-oracle-database\/","title":{"rendered":"SQL query to find foreign key constraints that reference columns in a table in oracle database"},"content":{"rendered":"<p>I was rather new to a system with hundreds of Oracle database tables. There was once when I realized that I had to delete a redundant row from the <code>SYSTEMS_CONFIGURATION<\/code> table. <\/p>\n<p>As there are foreign key references to the <code>SYSTEMS_CONFIGURATION<\/code> table, I had to first remove all foreign key references to the redundant <code>SYSTEMS_CONFIGURATION<\/code> table row prior to deleting it. <\/p>\n<p>To be able to do so, I had to identify the tables which could have rows referencing that redundant SYSTEMS_CONFIGURATION table. But how?<\/p>\n<p>This post describes the SQL query which I had ran on my Oracle database to find all the tables which had foreign key references to SYSTEMS_CONFIGURATION table.<\/p>\n<h2>The USER_CONSTRAINTS and USER_CONS_COLUMNS tables<\/h2>\n<p>The <code>USER_CONSTRAINTS<\/code> table describes all constraint definitions on tables owned by the current user, while the <code>USER_CONS_COLUMNS<\/code> table describes columns that are owned by the current user and that are specified in constraint definitions. In other words, we can get information about foreign key columns from USER_CONSTRAINTS and the columns being referenced from the USER_CONS_COLUMNS table.<\/p>\n<h2>The SQL query to find foreign key constraints that reference columns in the SYSTEMS_CONFIGURATION table<\/h2>\n<p>After identifying the tables that contain the information which I need, I went on to construct the SQL query.<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nSELECT UC.TABLE_NAME,\r\n\t   UC.R_CONSTRAINT_NAME,\r\n       UCC.TABLE_NAME,\r\n\t   UCC.COLUMN_NAME\r\nFROM USER_CONSTRAINTS  UC,\r\n     USER_CONS_COLUMNS UCC\r\nWHERE UC.R_CONSTRAINT_NAME = UCC.CONSTRAINT_NAME\r\n\t  AND UC.CONSTRAINT_TYPE = 'R'\r\n    AND UCC.TABLE_NAME = 'SYSTEMS_CONFIGURATION';\r\n<\/pre>\n<p>The SQL statement performs a join on rows returned from both the USER_CONSTRAINTS and USER_CONS_COLUMNS tables, on condition that the <code>CONSTRAINT_NAME<\/code> columns from both tables contain the same value. <\/p>\n<p>The SQL statement then filters the result by:<\/p>\n<ul>\n<li>matching the rows having 'R' in the <code>CONSTRAINT_TYPE<\/code> column. A 'R' in the <code>CONSTRAINT_TYPE<\/code> column indicates that the particular constraint is a referential constraint; which enforces foreign key relationship between the tables.<\/li>\n<li>matching the rows having 'SYSTEMS_CONFIGURATION' in the <code>TABLE_NAME<\/code> column.<\/li>\n<\/ul>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-5B\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-5B&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-5B&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F347&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>I was rather new to a system with hundreds of Oracle database tables. There was once when I realized that I had to delete a redundant row from the SYSTEMS_CONFIGURATION table. <\/p>\n<p>As there are foreign key references to the SYSTEMS_CONFIGURATION table, I had to first remove all foreign key references to the redundant SYSTEMS_CONFIGURATION table row prior to deleting it. <\/p>\n<p>This post captures the SQL query which I had ran on my Oracle database to find all the tables which had foreign key references to SYSTEMS_CONFIGURATION table, so that I can write the SQL update statements to delete all child references to the redundant SYSTEMS_CONFIGURATION row prior to deleting it.<\/p>\n","protected":false},"author":1,"featured_media":1226,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[375],"tags":[156,180,26],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Oracle-Database-logo.jpg","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-5B","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/347"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=347"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/347\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1226"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=347"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=347"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=347"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}