<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Disable Foreign Key Constraint &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/disable-foreign-key-constraint/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.dbrnd.com</link>
	<description>BIGData &#124; NoSQL &#124; MSSQL &#124; MySQL &#124; PostgreSQL</description>
	<lastBuildDate>Fri, 03 Feb 2017 02:54:23 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.4</generator>
	<item>
		<title>PostgreSQL: How to Disable or Enable Foreign Key Constraint of a Table</title>
		<link>https://www.dbrnd.com/2017/02/postgresql-how-to-disable-or-enable-foreign-key-constraint-of-a-table-data-migration-bulk-operation/</link>
					<comments>https://www.dbrnd.com/2017/02/postgresql-how-to-disable-or-enable-foreign-key-constraint-of-a-table-data-migration-bulk-operation/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Thu, 02 Feb 2017 16:16:49 +0000</pubDate>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[bulk operation]]></category>
		<category><![CDATA[data migration]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[Disable Foreign Key Constraint]]></category>
		<category><![CDATA[Foreign Key constraint]]></category>
		<category><![CDATA[plpgsql]]></category>
		<category><![CDATA[Postgres Query]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[PostgreSQL Administrator]]></category>
		<category><![CDATA[PostgreSQL Error]]></category>
		<category><![CDATA[PostgreSQL Performance Tuning]]></category>
		<category><![CDATA[PostgreSQL Programming]]></category>
		<category><![CDATA[PostgreSQL Tips and Tricks]]></category>
		<category><![CDATA[trigger]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=3939</guid>

					<description><![CDATA[In this post, I am sharing one option to Disable / Enable the Foreign Key Constraint in PostgreSQL. During data migration and testing purpose, Database Developer requires to disable Foreign key constraint of a Table. Once you disable constraint, then later you might need to enable again, but during this exercise make sure that all [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F02%2Fpostgresql-how-to-disable-or-enable-foreign-key-constraint-of-a-table-data-migration-bulk-operation%2F&amp;linkname=PostgreSQL%3A%20How%20to%20Disable%20or%20Enable%20Foreign%20Key%20Constraint%20of%20a%20Table" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F02%2Fpostgresql-how-to-disable-or-enable-foreign-key-constraint-of-a-table-data-migration-bulk-operation%2F&amp;linkname=PostgreSQL%3A%20How%20to%20Disable%20or%20Enable%20Foreign%20Key%20Constraint%20of%20a%20Table" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F02%2Fpostgresql-how-to-disable-or-enable-foreign-key-constraint-of-a-table-data-migration-bulk-operation%2F&amp;linkname=PostgreSQL%3A%20How%20to%20Disable%20or%20Enable%20Foreign%20Key%20Constraint%20of%20a%20Table" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F02%2Fpostgresql-how-to-disable-or-enable-foreign-key-constraint-of-a-table-data-migration-bulk-operation%2F&amp;linkname=PostgreSQL%3A%20How%20to%20Disable%20or%20Enable%20Foreign%20Key%20Constraint%20of%20a%20Table" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F02%2Fpostgresql-how-to-disable-or-enable-foreign-key-constraint-of-a-table-data-migration-bulk-operation%2F&amp;linkname=PostgreSQL%3A%20How%20to%20Disable%20or%20Enable%20Foreign%20Key%20Constraint%20of%20a%20Table" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F02%2Fpostgresql-how-to-disable-or-enable-foreign-key-constraint-of-a-table-data-migration-bulk-operation%2F&amp;linkname=PostgreSQL%3A%20How%20to%20Disable%20or%20Enable%20Foreign%20Key%20Constraint%20of%20a%20Table" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F02%2Fpostgresql-how-to-disable-or-enable-foreign-key-constraint-of-a-table-data-migration-bulk-operation%2F&#038;title=PostgreSQL%3A%20How%20to%20Disable%20or%20Enable%20Foreign%20Key%20Constraint%20of%20a%20Table" data-a2a-url="https://www.dbrnd.com/2017/02/postgresql-how-to-disable-or-enable-foreign-key-constraint-of-a-table-data-migration-bulk-operation/" data-a2a-title="PostgreSQL: How to Disable or Enable Foreign Key Constraint of a Table"></a></p><p>In this post, I am sharing one option to Disable / Enable the Foreign Key Constraint in PostgreSQL.</p>
<blockquote><p>During data migration and testing purpose, Database Developer requires to disable Foreign key constraint of a Table.</p></blockquote>
<blockquote><p>Once you disable constraint, then later you might need to enable again, but during this exercise make sure that all your data changes are correct.</p></blockquote>
<blockquote><p>PostgreSQL does not provide any direct command or function to disable / enable the Foreign key constraints. </p></blockquote>
<blockquote><p>When you create any Foreign Key on the table, internally It creates a hidden trigger for check data integrity.<br />
You should enable/disable the trigger for achieving disable foreign key constraint.</p></blockquote>
<p><strong>Create a sample Student Master table:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE tbl_Students 
(
	StudID INTEGER PRIMARY KEY
	,StudName CHARACTER VARYING
	,StudClass CHAR(1)	
);</pre> </p>
<p><strong>Insert few sample records in Student Master table:</strong></p>
<pre class="crayon-plain-tag">INSERT INTO tbl_Students 
VALUES (1,'Anvesh','A')
,(2,'Neevan','B'),(3,'Jenny','C')
,(4,'Roy','C'),(5,'Martin','C');</pre> </p>
<p><strong>Create a sample Student Marks table:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE tbl_StudentMarks
(
	StudentMarkID INTEGER PRIMARY KEY
	,StudID INTEGER 
	,Sub1Mark SMALLINT
	,Sub2Mark SMALLINT
	,Sub3Mark SMALLINT	
	,CONSTRAINT fk_tbl_StudentMarks_StudID FOREIGN KEY (StudID) REFERENCES tbl_Students (StudID)
);</pre> </p>
<p><strong>Try to insert data with StudID which does not belong to Student Master table:</strong></p>
<pre class="crayon-plain-tag">INSERT INTO tbl_StudentMarks 
VALUES (1,6,50,60,90);</pre> </p>
<p><strong>You will get a below error message:</strong></p>
<pre class="crayon-plain-tag">ERROR:  insert or update on table "tbl_studentmarks" violates foreign key constraint "fk_tbl_studentmarks_studid"
DETAIL:  Key (studid)=(6) is not present in table "tbl_students".
********** Error **********</pre> </p>
<p><strong>Disable all hidden triggers of a Student Mark table:</strong></p>
<pre class="crayon-plain-tag">ALTER TABLE tbl_StudentMarks DISABLE TRIGGER ALL;</pre> </p>
<p><strong>Now Again, try to insert same record:</strong></p>
<pre class="crayon-plain-tag">INSERT INTO tbl_StudentMarks 
VALUES (1,6,50,60,90);</pre> </p>
<p><strong>Once you did with your task, please enable all trigger of a table:</strong></p>
<pre class="crayon-plain-tag">ALTER TABLE tbl_StudentMarks ENABLE TRIGGER ALL;</pre> </p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2017/02/postgresql-how-to-disable-or-enable-foreign-key-constraint-of-a-table-data-migration-bulk-operation/feed/</wfw:commentRss>
			<slash:comments>11</slash:comments>
		
		
			</item>
	</channel>
</rss>
