pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Xiao, Bing (Benny) <bing.xiao@dxc.com>
To: pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>
Subject: Null option and Default value Lost when use CREATE TABLE AS to backup a table.
Date: Thu, 21 Oct 2021 05:47:18 +0000
Message-ID: <DM5PR0101MB3002C39B637255E03599448AF9BF9@DM5PR0101MB3002.prod.exchangelabs.com> (raw)

Hi Team,

Null option(NOT NULL) and Default value lost when I try to copy a table with CREATE TABLE AS statement.

List the test step as below.

  1.  PostgreSQL version.



[cid:image005.jpg@01D7C682.25FBB520]



  1.  Create test table as below.



CREATE TABLE contact(

    id SERIAL PRIMARY KEY,

    first_name VARCHAR NOT NULL DEFAULT 'Benny',

    last_name VARCHAR NOT NULL,

    email VARCHAR NOT NULL UNIQUE

);



  1.  Insert the test data to table Contact.



INSERT INTO contact(first_name, last_name, email)

VALUES('John','Doe','john.doe@postgresqltutorial.com'),

      ('David','William','david.william@postgresqltutorial.com');



  1.  Create back up table.


               create table contact_bckp as table contacts with data;


  1.  Test to insert data with null in column last_name. Failed in table contact.



INSERT INTO contact(first_name, last_name, email) VALUES('benny',null,'john.doe@postgresqltutorial.com');



[cid:image011.jpg@01D7C682.25FBB520]


  1.  Test to insert data with null in column last_name. Succeed  in table contact_bckp. And even the PK column been set to null.

       INSERT INTO contact_bckp(first_name, last_name, email)  VALUES('benny',null,'john.doe@postgresqltutorial.com');

[cid:image012.jpg@01D7C682.25FBB520]

[cid:image013.jpg@01D7C682.25FBB520]

  1.  Check the table definition as below. contact_bckp lost null option and default value compare to table contact.



[cid:image014.jpg@01D7C682.25FBB520]

[cid:image018.jpg@01D7C682.25FBB520]

Best Regards.
Benny
Email:  bing.xiao@dxc.com<mailto:bing.xiao@dxc.com>

Attachments:

image005.jpg (image/jpeg, 16.7K) download view image
image011.jpg (image/jpeg, 32.3K) download view image
image012.jpg (image/jpeg, 23.0K) download view image
image013.jpg (image/jpeg, 31.4K) download view image
image014.jpg (image/jpeg, 57.3K) download view image
image018.jpg (image/jpeg, 39.7K) download view image

view thread (3+ messages)  latest in thread

Message-ID: <DM5PR0101MB3002C39B637255E03599448AF9BF9@DM5PR0101MB3002.prod.exchangelabs.com>
Permalink:  ../DM5PR0101MB3002C39B637255E03599448AF9BF9@DM5PR0101MB3002.prod.exchangelabs.com/
Also on:    postgresql.org/message-id/DM5PR0101MB3002C39B637255E03599448AF9BF9@DM5PR0101MB3002.prod.exchangelabs.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: bing.xiao@dxc.com
  Subject: Re: Null option and Default value Lost when use CREATE TABLE AS to backup a table.
  In-Reply-To: <DM5PR0101MB3002C39B637255E03599448AF9BF9@DM5PR0101MB3002.prod.exchangelabs.com>
  References: <DM5PR0101MB3002C39B637255E03599448AF9BF9@DM5PR0101MB3002.prod.exchangelabs.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox