Reputation: 94
I am not able to create table like/as with constraints and default values from another table that already exists.
I have tried following
$sql = "CREATE TABLE schemas.a AS TABLE schemas.b";
This creates the table but no constraint or default value is copied.
$sql = "CREATE TABLE schemas.a (LIKE schemas.b INCLUDING CONSTRAINTS)";
This creates the table with not null constraint only. Still the primary key constraint and the default value is not being transferred.
Coding used in php
$sql = "CREATE TABLE schemas.a (LIKE schemas.b INCLUDING CONSTRAINTS)";
pg_query($con, $sql); //This is in the PHP
I want to create the table with all the constraints (primary, unique, not null) and default value that are set.
Upvotes: 1
Views: 815
Reputation: 2349
Try the following sql:
CREATE TABLE schemas.a (
LIKE schemas.b
INCLUDING ALL
);
For further information about the CREATE TABLE
command have a look into the documentation: https://www.postgresql.org/docs/current/sql-createtable.html (Perhaps you need to switch to your version. Search for like_option
to find the relevant information on the page.)
Upvotes: 1