Received: from makus.postgresql.org ([98.129.198.125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1T5jYI-0004SF-Rd for pgsql-docs@postgresql.org; Sun, 26 Aug 2012 20:35:22 +0000 Received: from momjian.us ([72.94.173.45]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1T5jYH-0003i1-EJ for pgsql-docs@postgresql.org; Sun, 26 Aug 2012 20:35:22 +0000 Received: from bruce by momjian.us with local (Exim 4.72) (envelope-from ) id 1T5jYF-00060a-52; Sun, 26 Aug 2012 16:35:19 -0400 Date: Sun, 26 Aug 2012 16:35:19 -0400 From: Bruce Momjian To: Magnus Hagander Cc: david.sahagian@emc.com, pgsql-docs@postgresql.org Subject: Re: CREATE TABLE LIKE, regarding constraints Message-ID: <20120826203519.GN10814@momjian.us> References: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="cHMo6Wbp1wrKhbfi" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.5.20 (2009-06-14) X-Pg-Spam-Score: -2.1 (--) X-Archive-Number: 201208/44 X-Sequence-Number: 7437 --cHMo6Wbp1wrKhbfi Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit On Mon, Jan 2, 2012 at 08:31:43PM +0100, Magnus Hagander wrote: > On Mon, Jan 2, 2012 at 15:32, wrote: > > On Fri, Dec 30, 2011 at 22:27,   wrote: > >> www.postgresql.org/docs/9.0/static/sql-createtable.html > >> == == == == == > >> LIKE parent_table [ like_option ... ] > >> . . . > >> Not-null constraints are always copied to the new table. > >> CHECK constraints will only be copied if INCLUDING CONSTRAINTS is specified; other types of constraints will never be copied. > >> . . . > >> == == == == == > >> > >> But I do see PK and UNIQUE constraints > >>  CONSTRAINT blah_pkey PRIMARY KEY (id), > >>  CONSTRAINT blah_host_id_key UNIQUE (host_id) > >> in the def of the new table. > > > > Can you provide the commands you ran to make that happen? It doesn't > > happen for me in a trivial test. > > > >> Also, why is there no discussion of what "EXCLUDING CONSTRAINTS" will result in ? > > > > > > Magnus, > > I did some more "testing" of CREATE TABLE LIKE, > > and now see that [INCLUDING INDEXES] also can cause PRIMARY KEY and UNIQUE constraints to become part of the new table. > > Ah, that explains why I couldn't reproduce it. > > > > I have no problem with this behavior, > > but the doc probably deserves some clarification on the "relationship" between > > [INCLUDING CONSTRAINTS] and [INCLUDING INDEXES]. > > That might be a good idea, yes. Feel like cooking up a patch? I have applied the attached patch based on this report. It is confusing that INCLUDING CONSTRAINTS only copies CHECK constraints, and INCLUDING INDEXES is required for PRIMARY KEY and UNIQUE constraints. Is there a reason our logic is so odd here? The SQL standard? -- Bruce Momjian http://momjian.us EnterpriseDB http://enterprisedb.com + It's impossible for everything to be true. + --cHMo6Wbp1wrKhbfi Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="constraints.diff" diff --git a/doc/src/sgml/ref/create_table.sgml b/doc/src/sgml/ref/create_table.sgml new file mode 100644 index 19e6f8e..445ca40 *** a/doc/src/sgml/ref/create_table.sgml --- b/doc/src/sgml/ref/create_table.sgml *************** CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY *** 336,351 **** Not-null constraints are always copied to the new table. ! CHECK constraints will only be copied if ! INCLUDING CONSTRAINTS is specified; other types of ! constraints will never be copied. Also, no distinction is made between ! column constraints and table constraints — when constraints are ! requested, all check constraints are copied. ! ! ! Any indexes on the original table will not be created on the new ! table, unless the INCLUDING INDEXES clause is ! specified. STORAGE settings for the copied column definitions will only be copied if INCLUDING STORAGE is specified. The --- 336,348 ---- Not-null constraints are always copied to the new table. ! CHECK constraints will be copied only if ! INCLUDING CONSTRAINTS is specified. ! Indexes, PRIMARY KEY, and UNIQUE constraints ! on the original table will be created on the new table only if the ! INCLUDING INDEXES clause is specified. ! No distinction is made between column constraints and table ! constraints. STORAGE settings for the copied column definitions will only be copied if INCLUDING STORAGE is specified. The --cHMo6Wbp1wrKhbfi--