CREATE TABLE AS SELECT statement in PostgreSQL is not creating the new table with the columns with their NOT NULL constraint of the original table.
My table tab1 is having col1 and col2 with not null constraints.
create table tab1 (col1 varchar(23) not null, col2 int not null)
and now i am creating a new table tab2 with definition and data of the table tab2 as below:
create table tab2 as select col1,col2 from tab1
But col1 and col2 of tab2 is not having NOT NULL constraint. How to get the exact NOT NULL constraint status in the new table columns as of the original table?
If there is any way to achieve this using CREATE TABLE AS SELECT statement that is greatly appreciated. CREATE TABLE AS SELECT statement in Oracle,DB2 and SELECT .. INTO in SQLServer copy the NOT NULL CONSTRAINT along with the data.
CREATE TABLE tab3 AS SELECT * FROM TAB1 UNION ALL SELECT * FROM TAB2This is the actual requirement for me.NOT NULLconstraint but TAB2 does not, even has NULL values in it?! Can you get what you want with a combination ofCREATE TABLE ... (LIKE ...)andINSERT?