<?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>INCLUDING ALL &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/including-all/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>Mon, 20 Nov 2017 11:17:16 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.6</generator>
	<item>
		<title>PostgreSQL: Duplicate or Copy Table with Data, Constraint, Index</title>
		<link>https://www.dbrnd.com/2017/12/postgresql-duplicate-or-copy-table-with-data-constraint-index-including-all/</link>
					<comments>https://www.dbrnd.com/2017/12/postgresql-duplicate-or-copy-table-with-data-constraint-index-including-all/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Wed, 13 Dec 2017 16:16:40 +0000</pubDate>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[INCLUDING ALL]]></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 Monitoring]]></category>
		<category><![CDATA[PostgreSQL Performance Tuning]]></category>
		<category><![CDATA[PostgreSQL Programming]]></category>
		<category><![CDATA[PostgreSQL Tips and Tricks]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=6796</guid>

					<description><![CDATA[In this post, I am sharing a script for creating a copy of table including all data, constraints, indexes of a PostgreSQL source table. I have seen that people are using simple CREATE TABLE AS SELECT&#8230; for creating a duplicate table. But it will create a table with data and column structure only. If we [&#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%2F12%2Fpostgresql-duplicate-or-copy-table-with-data-constraint-index-including-all%2F&amp;linkname=PostgreSQL%3A%20Duplicate%20or%20Copy%20Table%20with%20Data%2C%20Constraint%2C%20Index" 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%2F12%2Fpostgresql-duplicate-or-copy-table-with-data-constraint-index-including-all%2F&amp;linkname=PostgreSQL%3A%20Duplicate%20or%20Copy%20Table%20with%20Data%2C%20Constraint%2C%20Index" 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%2F12%2Fpostgresql-duplicate-or-copy-table-with-data-constraint-index-including-all%2F&amp;linkname=PostgreSQL%3A%20Duplicate%20or%20Copy%20Table%20with%20Data%2C%20Constraint%2C%20Index" 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%2F12%2Fpostgresql-duplicate-or-copy-table-with-data-constraint-index-including-all%2F&amp;linkname=PostgreSQL%3A%20Duplicate%20or%20Copy%20Table%20with%20Data%2C%20Constraint%2C%20Index" 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%2F12%2Fpostgresql-duplicate-or-copy-table-with-data-constraint-index-including-all%2F&amp;linkname=PostgreSQL%3A%20Duplicate%20or%20Copy%20Table%20with%20Data%2C%20Constraint%2C%20Index" 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%2F12%2Fpostgresql-duplicate-or-copy-table-with-data-constraint-index-including-all%2F&amp;linkname=PostgreSQL%3A%20Duplicate%20or%20Copy%20Table%20with%20Data%2C%20Constraint%2C%20Index" 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%2F12%2Fpostgresql-duplicate-or-copy-table-with-data-constraint-index-including-all%2F&#038;title=PostgreSQL%3A%20Duplicate%20or%20Copy%20Table%20with%20Data%2C%20Constraint%2C%20Index" data-a2a-url="https://www.dbrnd.com/2017/12/postgresql-duplicate-or-copy-table-with-data-constraint-index-including-all/" data-a2a-title="PostgreSQL: Duplicate or Copy Table with Data, Constraint, Index"></a></p><p>In this post, I am sharing a script for creating a copy of table including all data, constraints, indexes of a PostgreSQL source table.</p>
<blockquote><p>I have seen that people are using simple CREATE TABLE AS SELECT&#8230; for creating a duplicate table.<br />
But it will create a table with data and column structure only.</p>
<p>If we want a duplicate table with all possible structures like column, constraint, index, we should add INCLUDING ALL. </p></blockquote>
<p><strong>Check the below demonstration:</strong></p>
<p><strong>Create sample table with default, constraint, index:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE tbl_A
(
    ID INT
    ,Name CHARACTER VARYING DEFAULT 'dbrnd'
    ,Subject CHARACTER VARYING CONSTRAINT chk_tbl_A_Subject CHECK (Subject IS NOT NULL)
    ,CONSTRAINT pk_tbl_A_ID PRIMARY KEY(ID)
);

CREATE INDEX idx_tbl_A_Subject ON tbl_A USING BTREE (Subject);</pre> </p>
<p><strong>Create another simple copy table:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE tbl_B AS SELECT * FROM tbl_A;</pre> </p>
<p><strong>Check the table definition of tbl_b:</strong><br />
You cannot find any default, primary key, check or index. It just copied the plain columns. </p>
<pre class="crayon-plain-tag">CREATE TABLE public.tbl_b
(
    id integer,
    name character varying COLLATE pg_catalog."default",
    subject character varying COLLATE pg_catalog."default"
)
WITH (
    OIDS = FALSE
)
TABLESPACE pg_default;</pre> </p>
<p><strong>Create another copy table using INCLUDING ALL:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE tbl_C (LIKE tbl_A INCLUDING ALL);</pre> </p>
<p><strong>Check the table definition of tbl_c:</strong><br />
You can find the definition of all constraints and index.</p>
<pre class="crayon-plain-tag">CREATE TABLE public.tbl_c
(
    id integer NOT NULL,
    name character varying COLLATE pg_catalog."default" DEFAULT 'dbrnd'::character varying,
    subject character varying COLLATE pg_catalog."default",
    CONSTRAINT tbl_c_pkey PRIMARY KEY (id),
    CONSTRAINT chk_tbl_a_subject CHECK (subject IS NOT NULL)
)
WITH (
    OIDS = FALSE
)
TABLESPACE pg_default;</pre> </p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2017/12/postgresql-duplicate-or-copy-table-with-data-constraint-index-including-all/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
