Null option and Default value Lost when use CREATE TABLE AS to backup a table.

From: "Xiao, Bing (Benny)" <bing(dot)xiao(at)dxc(dot)com>
To: "pgsql-bugs(at)lists(dot)postgresql(dot)org" <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Subject: Null option and Default value Lost when use CREATE TABLE AS to backup a table.
Date: 2021-10-21 05:47:18
Message-ID: DM5PR0101MB3002C39B637255E03599448AF9BF9@DM5PR0101MB3002.prod.exchangelabs.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

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(dot)jpg(at)01D7C682(dot)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(dot)doe(at)postgresqltutorial(dot)com'),

('David','William','david(dot)william(at)postgresqltutorial(dot)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(dot)doe(at)postgresqltutorial(dot)com');

[cid:image011(dot)jpg(at)01D7C682(dot)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(dot)doe(at)postgresqltutorial(dot)com');

[cid:image012(dot)jpg(at)01D7C682(dot)25FBB520]

[cid:image013(dot)jpg(at)01D7C682(dot)25FBB520]

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

[cid:image014(dot)jpg(at)01D7C682(dot)25FBB520]

[cid:image018(dot)jpg(at)01D7C682(dot)25FBB520]

Best Regards.
Benny
Email: bing(dot)xiao(at)dxc(dot)com<mailto:bing(dot)xiao(at)dxc(dot)com>

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message PG Bug reporting form 2021-10-21 09:48:39 BUG #17240: <timestamptz> at time zone ... ; wrong result
Previous Message David G. Johnston 2021-10-20 20:42:23 Re: BUG #17235: PQsendQuery (with two sql) after PQenterPipelineMode cause ERROR